fix(conversation): count tokens from length-truncated responses - #38460
Open
liuhao1024 wants to merge 1 commit into
Open
liuhao1024 wants to merge 1 commit into
liuhao1024 wants to merge 1 commit into
Conversation
When finish_reason is 'length' and the conversation loop retries with a continuation prompt, the tokens consumed by the truncated response were never tracked. This caused underreported usage in /insights and the session DB. Track tokens in the restart_with_length_continuation handler before boosting the output cap and continuing the loop. Fixes NousResearch#38458
1 task
teknium1
reviewed
Jul 13, 2026
teknium1
left a comment
Collaborator
There was a problem hiding this comment.
Thanks for identifying the continuation-accounting gap. The premise remains valid on current main: a truncated text response sets _retry.restart_with_length_continuation at agent/conversation_loop.py:1948, and the outer handler continues at agent/conversation_loop.py:4227-4242 before normal accounting runs.
Problems
- The patch predates the current accounting pipeline. Current normal-response processing at
agent/conversation_loop.py:2059-2247normalizes usage, updates the context engine, accumulates all session buckets, prices usage, and writes a per-call SessionDB delta. The proposed inline counter update at the old handler site would not preserve that full contract. - The added test verifies only in-memory aggregates, not the claimed SessionDB/
/insightsresult.
Suggested changes
- Rework the salvage around the current continuation handler to reuse/extract the canonical accounting path.
- Add a temp-SessionDB regression covering truncated plus continuation usage and persisted accounting.
Automated hermes-sweeper review.
| @@ -3512,6 +3512,35 @@ def _perform_api_call(next_api_kwargs): | |||
| continue | |||
|
|
|||
Collaborator
There was a problem hiding this comment.
Current main's normal accounting also updates the context engine and writes a per-call SessionDB delta (agent/conversation_loop.py:2059-2247). Please salvage this through that canonical pipeline rather than maintaining a reduced parallel counter/cost update here.
19 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
Fixes token accounting for length-truncated responses. When
finish_reason == "length"triggers a continuation retry, the tokens consumed by the truncated API call were never tracked — causing underreported usage in/insightsand the session DB.Related Issue
Fixes #38458
Type of Change
Changes Made
agent/conversation_loop.py: Track token usage (prompt, completion, total, cache, reasoning) and estimated cost from the truncated response in therestart_with_length_continuationhandler, before boosting the output cap and continuing the loop.tests/run_agent/test_run_agent.py: Addtest_length_continuation_counts_truncated_tokens— verifies that both the truncated and continuation response tokens are counted in session totals.How to Test
max_output_tokens(triggersfinish_reason="length")/insights— token counts should include both the truncated and continuation callspytest tests/run_agent/test_run_agent.py::TestRunConversation::test_length_continuation_counts_truncated_tokens -xvsChecklist
Code
fix(scope):,feat(scope):, etc.)pytest tests/ -qand all tests passDocumentation & Housekeeping
docs/, docstrings) — or N/Acli-config.yaml.exampleif I added/changed config keys — or N/ACONTRIBUTING.mdorAGENTS.mdif I changed architecture or workflows — or N/ACode Intelligence
agent/conversation_loop.py:run_conversation— therestart_with_length_continuationhandler (line ~3514)AIAgent.run_conversation()→ called by CLI, gateway, batch_runner, cron