← Back to Pull requests

fix: keep token_usage total_tokens complete across usage conventions #13986

Open
unknown opened this pull request with 466 lines diff across 5 files
View on GitHub
unknown unknown commented on Nov 29, 2026 01:52 UTC
Author

Related Issues

  • fixes: no existing issue; found while testing TokenBudgetHook with a FallbackChatGenerator.
  • Proposed Changes:

    The Agent sums each reply's meta["usage"] into token_usage, and TokenBudgetHook reads token_usage["total_tokens"] when it is present. Chat generators report usage in different conventions: OpenAIChatGenerator and OpenAIResponsesChatGenerator report total_tokens, while AnthropicChatGenerator reports only prompt_tokens and completion_tokens. When a FallbackChatGenerator switches from the first kind to the second, the accumulated total_tokens stops growing, so the hook keeps reading a stale number.

    AS IS

  • The first call to OpenAI uses 40 tokens, then the fallback Anthropic generator uses 60 tokens per call.

  • After four calls, token_usage["total_tokens"] is still 40. The real total is 220.

  • With TokenBudgetHook(max_total_tokens=100), the run never stops on the budget and ends on max_agent_steps.
  • TO BE

  • _record_llm_usage fills in total_tokens from the reply's input and output tokens when the reply reports no total_tokens, before adding the reply to token_usage. A reported total is kept as is, and ChatMessage.meta is not changed.

  • token_usage["total_tokens"] now counts every call, so the same run stops with exit_reason="token_budget_exceeded" after the second call.

  • TokenBudgetHook is unchanged.
  • One visible side effect: for a generator that reports only input and output tokens, the Agent's token_usage output now also contains total_tokens. The Agent docstrings and the Agent docs page say so.

    Before and after, with a real Agent and FallbackChatGenerator (mocked provider responses, budget 100, the fallback reports 60 tokens per call):

    | First generator reports | Before: LLM calls / total_tokens | After: LLM calls / total_tokens |
    |---|---|---|
    | prompt_tokens, completion_tokens, total_tokens (Chat Completions) | 4 / 40 | 2 / 100 |
    | input_tokens, output_tokens, total_tokens (Responses API) | 4 / 40 | 2 / 100 |
    | total_tokens only | 4 / 40 | 2 / 100 |

    How did you test it?

  • New test_counts_every_call_after_a_fallback_to_another_usage_convention in test/hooks/budget/test_hooks.py runs an Agent whose replies switch from Responses-style to Anthropic-style usage. It fails before the change and passes after it.

  • New TestRecordLlmUsage in test/components/agents/test_utils.py checks the summed total across conventions, that a reported total is kept, and that the message's usage is not changed.

  • hatch run test:unit for test/hooks and test/components/agents, hatch run fmt, hatch run test:types.
  • Notes for the reviewer

    The total is filled in per reply, before the sum, because after the sum it can no longer be told which counts came from which reply.

    Checklist

  • I have read the contributors guidelines and the code of conduct.

  • I have updated the related issue with new insights and changes.

  • I have added unit tests and updated the docstrings.

  • I've used one of the conventional commit types for my PR title: fix:, feat:, build:, chore:, ci:, docs:, style:, refactor:, perf:, test: and added ! in case the PR includes breaking changes.

  • I have documented my code.

  • I have added a release note file, following the contributors guidelines.

  • I have run pre-commit hooks and fixed any issue.
  • Diff: 466 lines (+349 / -117) across 5 files Reviewer: 1AbdullahButt Review duration: 6330m
    All Vouch review confidence checks passed
    Residual risk 0.44 = Change Risk (0.74) × (1 − Review Confidence (0.40)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.74 · 466 lines diff across 5 files
    0.74
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    40%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.368
    Shallow Review Depth shallow_review_depth
    0.344
    Sensitive Paths sensitive_paths
    0.074
    Ai Authored ai_authored
    0.050
    Real PR Conversation Timeline
    1 comments
    "Review active on #13986"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: medium confidence: 40.5% reviewed sensitive_module
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 73.8% (HIGH) · Higher = larger blast radius
    73.8%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 40.5% · Lower = rushed or shallow review
    40.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 44.0% · Moderate risk
    44.0%