Repository navigation
fix(frontend): don't count a held tool call as reasoning tokens - #18656
indrajit96 merged 3 commits into
Conversation
Regression test for ai-dynamo#18573. Signed-off-by: cipheraxat <[email protected]>
ReasoningUsageEstimator counted every chunk that showed nothing, for a choice still in reasoning, as reasoning. A parser that emits a tool call only once it is complete (glm47) makes every chunk of the call empty, so the whole call was counted. Hold those tokens per choice and settle them by what the parser releases next: reasoning or text keeps them as reasoning (a held </think>), a tool call drops them. Fixes ai-dynamo#18573 Signed-off-by: cipheraxat <[email protected]>
|
👋 Hi cipheraxat! Thank you for contributing to ai-dynamo/dynamo. Just a reminder: The 🚀 |
|
No actionable comments were generated in the recent review. WalkthroughThe estimator now defers token counts for active choices with no visible output. It classifies held counts when reasoning text or visible output arrives, excludes held tool-call tokens, and includes unresolved counts in usage unless the backend provides a positive reasoning-token count. A regression test covers a parser-buffered tool call. ChangesReasoning usage estimation
Estimated code review effort: 3 (Moderate) | ~20 minutes Severity of issue fixed: Medium Merge Risk:
|
… comment Signed-off-by: cipheraxat <[email protected]>
|
/ok to test d4b268e |
✅ Dynamo PR CI passed — run 43894788914 (attempt 1) on
|
External PR triageBlocker: Review requested on 2026-11-12 and idle for more than 72h. Trusted CI is green for the current head (approved 2026-11-16). Next action: @ai-dynamo/dynamo-frontend-codeowners: please review/approve. |
|
LGTM, thanks for the fix! Reviewed at Existing tests
Reasoning-token count by family and Each case: a 4-token thought, then a
All 56 cases count correctly on this PR (base over-counted in 48), and every case still emits the right number of tool calls. Edge cases (glm47)
Other paths
Latency Not changed by this PR (already wrong on base, out of scope)
|
Overview
With
--dyn-tool-call-parser glm47,usage.completion_tokens_details.reasoning_tokenscounted almost a whole tool call as reasoning: 1,309 of 1,311 tokens in the issue's example, where the thought was about 13. This PR makes the frontend estimate count only the thought.Root cause:
ReasoningUsageEstimatorcounted every chunk that showed nothing, for a choice still in reasoning, as reasoning, to cover a parser holding back a partial</think>. Theglm47unified parser emits a tool call only once it is complete, so every chunk of the call after</think>is empty and was counted.Key Changes
About 35 core-logic lines, all in
preprocessor.rs:ReasoningUsageEstimatorholds the tokens of those empty chunks per choice instead of counting them at once, and settles them by what the parser releases next: reasoning or text keeps them as reasoning (a held</think>), a tool call drops them.Streaming parsers (
qwen3,hermes), text answers and the legacy parser path behave as before.Validation
glm47: 4 thought chunks, then a tool call held until completereasoning_tokens33</think>), then textNew tests:
reasoning_usage_excludes_a_held_tool_call(the issue's reproducer) runs the realglm47unified parser throughapply_unified_response_policies. It also asserts that the call is emitted, so it can't pass by dropping it.reasoning_usage_settles_held_tokens_per_choicecovers two choices: a token still held at the usage chunk stays reasoning, and a tool call drops only its own choice's held tokens.Run on a Colab L4 VM (12 vCPU, Linux x86_64; CPU-only test):
cargo test -p dynamo-llm --no-default-features --lib -- reasoning_usage3e84909a19with only the new tests: both fail,Some(33)vsSome(4)andSome(4)vsSome(3).dynamo-llmlib suite passes (3,754 passed, 0 failed).Known limits:
</think>is held together with the following tool call, its one or two marker tokens are dropped with the call. The estimate can be slightly low in that case, never high.tool_choice: none),apply_tool_call_response_policystrips the call before the estimator sees it, so the held call still counts as reasoning, as onmain.Issues and Ongoing Work
reasoning_tokenscounts a held tool call as reasoning (glm47unified parser) #18573/v1/messagesstream (anthropic/stream_converter.rs). It does not touch reasoning-token estimation. No other open PR touchesReasoningUsageEstimator.Pull Request Checklist
/pr-checklistskill.AI assistance was used to prepare this change. Every result above was run and checked.