Repository navigation
trace: count a Codex request once per change in its usage total - #303
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
`codex_session_usage` counted a request for every `token_count` event that carried a total. But Codex also re-sends the totals it already has when nothing new was used: a rate-limit snapshot during compaction (`update_rate_limits` -> `send_token_count_event`), the local estimate after it (`recompute_token_usage`, which replaces only `last_token_usage`), and a usage-limit error. Each of those added a request with no model call behind it, so the ledger and `te status` reported more requests than the session made. A request now counts when `total_token_usage` changes; a model call always adds to it. The token breakdown is still read from the last total.
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_usagecounted a request for everytoken_countevent that carried a total. But Codex also re-sends the totals it already has when nothing new was used (codex-rs/core/src/session/mod.rs):update_rate_limitscallssend_token_count_eventon a compaction's rate-limit snapshot and on a usage-limit error, andrecompute_token_usage, the local estimate after compaction, replaces onlylast_token_usageand emits the event again. Those events are persisted in the rollout (codex-rs/rollout/src/policy.rs). Each one added a request with no model call behind it, so the ledger andte statusreported more requests than the session made. In normal operation Codex sends one event per response, deferring the rate-limit one "to avoid duplicate TokenCount events" (session/turn.rs).Fix: a request counts when
total_token_usagechanges; a model call always adds to it. The token breakdown is still read from the last total, so the cost is unchanged.Tests, in a new
tests/test_codex_session_requests.py, on made-up rollouts in Codex's format:last_token_usage, and an event with no info: 3 requests, where main counts 5; the breakdown is the last total;The skips need Windows, a real agent CLI (Codex, Hermes, opencode) or aider, or a non-root user.
Fixes #299