Repository navigation
trace: keep a Codex session's usage across a context-window overflow - #386
Open
Lanre Shittu (Shizoqua) wants to merge 1 commit into
Open
Lanre Shittu (Shizoqua) wants to merge 1 commit into
Lanre Shittu (Shizoqua) wants to merge 1 commit into
Conversation
When a turn fails with "context window exceeded", Codex sets the session's running totals to the window's size and every count to 0 (TokenUsageInfo::fill_to_context_window), writes that as a token_count event, and adds later calls to it. codex_session_usage read a session's usage off its last running total, so all that was spent before the overflow was lost, and a session that ended on one was priced at nothing. A total whose counts go down is now read as that reset: the total before it is kept and added to what follows, and the reset is not counted as a request.
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.
codex_session_usageread a Codex session's usage off its last cumulativetoken_counttotal. When a turn fails with "context window exceeded", Codex sets that running total to the window's size and every count to 0 (TokenUsageInfo::fill_to_context_window, throughset_total_tokens_full), writes it as atoken_countevent, which the rollout keeps, and adds later calls to it. So everything spent before the overflow was lost from the ledger, a session that ended on one was priced at nothing, and the reset was counted as a request.Fix: a total whose counts (input, cached, cache-write, output, reasoning) go down is read as that reset: the total before it is kept and added to what follows, across any number of resets.
total_tokensis not compared, since the reset sets it to the window's size. A total whose counts are all 0 is not counted as a request, as no model call made it. A session without an overflow reads as before, a re-sent total included.Tests, in a new
tests/test_trace_codex_context_window_reset.py:The skips need Windows, a real agent CLI (Codex, Hermes, opencode) or aider, or a non-root user.
Fixes #385