fix(usage): preserve existing request_usage_entries on Usage.add#3213
Merged
seratch merged 1 commit intoopenai:mainfrom May 8, 2026
Merged
fix(usage): preserve existing request_usage_entries on Usage.add#3213seratch merged 1 commit intoopenai:mainfrom
seratch merged 1 commit intoopenai:mainfrom
Conversation
When `Usage.add(other)` was called with `other.requests == 1` and `other.total_tokens > 0`, the method synthesized a fresh `RequestUsage` entry from `other`'s top-level fields and silently discarded any pre-populated `other.request_usage_entries`. The synthesized entry only carries top-level token details, so nested fields such as `cached_tokens` and `reasoning_tokens` already present on the existing entries were lost. Reorder the branches so that when `other.request_usage_entries` is non-empty we merge those authoritative entries directly, and only fall back to synthesizing one when no per-request breakdown is provided. Add a regression test that exercises this exact shape.
Member
|
@codex review |
|
Codex Review: Didn't find any major issues. Swish! ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
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.
Summary
Usage.add(other)discards any pre-populatedother.request_usage_entrieswhenotheralso hasrequests == 1andtotal_tokens > 0, replacing them with a freshly synthesized entry built from only the top-level fields. Nested details such ascached_tokens/reasoning_tokenscarried by the existing entries are silently lost.The two branches in
add()were ordered so the synthesizer wins over an explicit per-request breakdown. The fix swaps them: ifotheralready has an authoritative breakdown, merge it; otherwise fall back to synthesizing one from the top-level fields.Repro (before the fix)
Changes
src/agents/usage.py— reorder theadd()branches so an explicitrequest_usage_entrieslist is preferred over a synthesized entry built from top-level fields.tests/test_usage.py— regression test (test_usage_add_preserves_existing_entries_when_top_level_also_set).Test plan
main, passes after the fix.tests/test_usage.pypasses (14/14).tests/test_agent_runner_streamed.py::test_streamed_run_preserves_request_usage_entries_after_retryand the conversation-locked variant still pass — these exercise the synthesize path withrequests == 2, which the fix does not touch.ruff checkclean on touched files.