fix: exclude reasoning_details from preflight token estimate (#73298) - #73306
fix: exclude reasoning_details from preflight token estimate (#73298)#73306JonthanaHanh wants to merge 1 commit into
Conversation
The reasoning_details field (OpenRouter/Anthropic thinking blocks + opaque cryptographic signature blobs) inflates the rough token estimate by ~4x. Providers do not bill these envelope bytes as prompt tokens. In a measured Kimi K3 session, reasoning_details held 2,124K chars vs 281K chars of actual thinking text. The estimator reported ~533K tokens when real prompt_tokens was ~140K — triggering compression at ~27% of the configured threshold. Fix: skip reasoning_details in both _estimate_message_chars and _estimate_message_tokens_without_images, alongside the existing _anthropic_content_blocks exclusion. Fixes NousResearch#73298
Related: #72087 already changes the same estimator hunk with a broader, regression-tested Anthropic replay-field deduplication. This PR is the narrower reasoning_details-only repair for #73298; a maintainer should choose or consolidate the overlapping approaches. |
|
Reviewed against #73298 — this correctly fixes the root cause. Excluding Two notes for a reviewer:
LGTM once a test is added. |
teknium1
left a comment
There was a problem hiding this comment.
Thanks for the focused preflight repair. The current-main premise is verified: agent/model_metadata.py:3005-3027 places reasoning_details into the estimator shadow, and agent/conversation_loop.py:1764 uses that estimate for request pressure.
Problems
- The PR changes both estimator helpers but adds no regression coverage. A future refactor could reintroduce the signed-envelope inflation without failing
tests/agent/test_model_metadata.py.
Suggested changes
- Add a behavioral test comparing otherwise identical messages with and without a large
reasoning_detailspayload. Assert thatestimate_messages_tokens_roughis unchanged; directly test_estimate_message_charstoo, since this diff changes it. - The member comment correctly notes overlap with #72087, which edits the same estimator hunk with an Anthropic-specific deduplication strategy. Consolidate the intended behavior before landing either patch.
Automated hermes-sweeper review.
| @@ -2785,7 +2785,7 @@ def _estimate_message_chars(msg: Dict[str, Any]) -> int: | |||
| return len(str(msg)) | |||
There was a problem hiding this comment.
Please add a regression test showing that a large reasoning_details envelope leaves estimate_messages_tokens_rough unchanged while ordinary counted reasoning content still affects the result. This line changes the production preflight path but currently has no direct coverage.
|
Merged via salvage PR #75884 (#75884) — your preflight estimator fix was cherry-picked with authorship preserved (composed into main's newer _wire_message_shadow() helper during rebase), plus a companion commit covering the second site @israellot documented (_REPLAY_BUDGET_KEYS tail-budget walk), counting thinking text but excluding signature envelopes per the #51800 discussion. Fixes #73298. Thanks! |
Summary
The
reasoning_detailsfield (OpenRouter/Anthropic thinking blocks + opaque cryptographic signature blobs) inflates the rough preflight token estimate by ~4x, causing premature compression on thinking models.Providers do not bill these envelope bytes as prompt tokens — in a measured Kimi K3 session,
reasoning_detailsheld 2,124K chars vs 281K chars of actual thinking text. The estimator reported ~533K tokens when realprompt_tokenswas ~140K, triggering compression at ~27% of the configured threshold.Fix
Skip
reasoning_detailsin both_estimate_message_charsand_estimate_message_tokens_without_images, alongside the existing_anthropic_content_blocksexclusion. This is the same treatment already applied to Anthropic content blocks (which also contain opaque metadata not billed as tokens).Changes
agent/model_metadata.py: Addreasoning_detailsto the skip set in both_estimate_message_chars()(L2788) and_estimate_message_tokens_without_images()(L2817)Test Plan
tests/agent/test_model_metadata.pypassFixes #73298