Skip to content

fix(agent): count codex_reasoning_items in tail-protection token estimate - #55756

Closed
ms-alan wants to merge 1 commit into
NousResearch:mainfrom
ms-alan:fix/55572-codex-reasoning-tokens
Closed

fix(agent): count codex_reasoning_items in tail-protection token estimate#55756
ms-alan wants to merge 1 commit into
NousResearch:mainfrom
ms-alan:fix/55572-codex-reasoning-tokens

Conversation

@ms-alan

@ms-alan ms-alan commented Jun 30, 2026

Copy link
Copy Markdown
Contributor

Closes #55572

Summary

The tail-protection estimator _estimate_msg_budget_tokens() was counting message content and tool_calls but not codex_reasoning_items. For sessions running on the OpenAI Responses/Codex backend, those items carry the model's encrypted_content reasoning blobs — real wire payload replayed to the provider. The estimator was blind to them, so the protected tail under-measured Responses sessions by 100K+ tokens (27% of session size in observed case), causing compaction to trigger too late.

Root cause

codex_reasoning_items was simply absent from the token-counting loop in _estimate_msg_budget_tokens, despite being counted indirectly via full-dict stringify in the preflight estimator estimate_messages_tokens_rough.

Fix

Add iteration over codex_reasoning_items using the same len(str(it)) // _CHARS_PER_TOKEN formula already used for tool_calls:

Observed impact

A real 214-turn session carried 162 reasoning items totalling ~459 KB (~115K tokens) — none of which the old tail estimator counted. The protected tail (~20K-token budget) silently held ~100K+ extra wire tokens.

Note

Privacy is unaffected: _serialize_for_summary only reads content, tool_calls, and tool_call_id, so encrypted blobs are never sent to the auxiliary summarizer.

…mate

Closes NousResearch#55572

The tail-protection estimator _estimate_msg_budget_tokens() was counting
message content and tool_calls but not codex_reasoning_items.  For sessions
running on the OpenAI Responses/Codex backend, those items carry the model's
encrypted_content reasoning blobs which are real wire payload replayed to the
provider.  The tail estimator was blind to them, so the protected tail
under-measured Responses sessions by 100K+ tokens, causing compaction to
trigger too late.

The fix adds iteration over codex_reasoning_items using the same
len(str(it)) // _CHARS_PER_TOKEN formula used for tool_calls.
@alt-glitch alt-glitch added type/bug Something isn't working comp/agent Core agent runtime: loop, agent_init, prompt builder, context-compression, responses endpoint sweeper:risk-session-state Sweeper risk: may lose/corrupt/mis-associate session or context state P2 Medium — degraded but workaround exists labels Jun 30, 2026
@alt-glitch

Copy link
Copy Markdown
Collaborator

This was generated by AI during triage.

Related to #45485 (the broader OPEN fix in the same _estimate_msg_budget_tokens tail-budget walk in agent/context_compressor.py). This PR adds only codex_reasoning_items to the count; #45485 already counts the fuller Codex/reasoning replay field set (reasoning_content/reasoning_details/codex_reasoning_items/codex_message_items) — a superset. A subset of a broader open PR is related, not a duplicate. Also related to the issue family #55572/#51800 and competing fix #51822. A human should pick the canonical fix (#45485 looks most complete).

@teknium1

Copy link
Copy Markdown
Contributor

Thanks for the focused regression fix. This is already implemented on current main by a broader canonical fix.

  • Automated hermes-sweeper review verified that agent/context_compressor.py:330-365 includes codex_reasoning_items in _REPLAY_BUDGET_KEYS and accounts for its serialized payload in _estimate_msg_budget_tokens().
  • The same implementation also covers reasoning, reasoning_content, reasoning_details, and codex_message_items, matching the broader related work noted in the discussion.
  • Commit 78ee0aa36703dad6b25a33303e5fd19c4b3bc0d7 was merged via PR fix(compression): count Codex replay fields in tail-protection budget (#55572) #59804; tests/agent/test_context_compressor.py:410-515 covers both combined and per-field replay-payload tail budgeting.
  • The fix shipped in v2026.7.7.

Closing as implemented on main.

@teknium1 teknium1 closed this Jul 15, 2026
@teknium1 teknium1 added the sweeper:implemented-on-main Sweeper: behavior already present on current main label Jul 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/agent Core agent runtime: loop, agent_init, prompt builder, context-compression, responses endpoint P2 Medium — degraded but workaround exists sweeper:implemented-on-main Sweeper: behavior already present on current main sweeper:risk-session-state Sweeper risk: may lose/corrupt/mis-associate session or context state type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Tail-protection token estimate ignores codex_reasoning_items, so Codex/Responses sessions compact too late

3 participants