fix: keep token_usage total_tokens complete across usage conventions #13986
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
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.02 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
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
1AbdullahButt
Related Issues
TokenBudgetHookwith aFallbackChatGenerator.Proposed Changes:
The Agent sums each reply's
meta["usage"]intotoken_usage, andTokenBudgetHookreadstoken_usage["total_tokens"]when it is present. Chat generators report usage in different conventions:OpenAIChatGeneratorandOpenAIResponsesChatGeneratorreporttotal_tokens, whileAnthropicChatGeneratorreports onlyprompt_tokensandcompletion_tokens. When aFallbackChatGeneratorswitches from the first kind to the second, the accumulatedtotal_tokensstops growing, so the hook keeps reading a stale number.AS IS
token_usage["total_tokens"]is still 40. The real total is 220.TokenBudgetHook(max_total_tokens=100), the run never stops on the budget and ends onmax_agent_steps.TO BE
_record_llm_usagefills intotal_tokensfrom the reply's input and output tokens when the reply reports nototal_tokens, before adding the reply totoken_usage. A reported total is kept as is, andChatMessage.metais not changed.token_usage["total_tokens"]now counts every call, so the same run stops withexit_reason="token_budget_exceeded"after the second call.TokenBudgetHookis unchanged.One visible side effect: for a generator that reports only input and output tokens, the Agent's
token_usageoutput now also containstotal_tokens. The Agent docstrings and the Agent docs page say so.Before and after, with a real
AgentandFallbackChatGenerator(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_tokensonly | 4 / 40 | 2 / 100 |How did you test it?
test_counts_every_call_after_a_fallback_to_another_usage_conventionintest/hooks/budget/test_hooks.pyruns an Agent whose replies switch from Responses-style to Anthropic-style usage. It fails before the change and passes after it.TestRecordLlmUsageintest/components/agents/test_utils.pychecks the summed total across conventions, that a reported total is kept, and that the message's usage is not changed.hatch run test:unitfortest/hooksandtest/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
fix:,feat:,build:,chore:,ci:,docs:,style:,refactor:,perf:,test:and added!in case the PR includes breaking changes.