You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
fix(caching): preserve usage details on streaming cache hits - #42244
Reasoning counts become zero while completion totals remain unchanged
How it solves it:
Reconstruct complete cached usage in both streaming iterators
Preserve final-chunk placement and legacy usage defaults
User Flow
Before, a streaming response-cache hit loses usage details
POST http://localhost:4000/v1/chat/completions with stream: true and stream_options.include_usage: true
Repeat the identical request. Reasoning tokens become zero even though the completion total is unchanged
After, a streaming response-cache hit preserves usage details
POST http://localhost:4000/v1/chat/completions with stream: true and stream_options.include_usage: true
Repeat the identical request. The full usage object matches the first response, including reasoning tokens
Relevant issues
Related to #32068 and #32096, which concern reasoning text during cache replay. This PR fixes usage counts separately
Pre-Submission checklist
I have added meaningful tests
The handful of test files covering my change pass locally
My PR passes all required CI/CD checks
My PR's scope is isolated to one problem
Automated reviews have completed on the current tip
Screenshots / Proof of Fix
Fresh runs on October 14, 2026 used a local proxy, a real reasoning-model upstream, and a fresh in-memory response cache for each revision. Credentials, private endpoints, upstream model identifiers, and response IDs are omitted
Set REPRO_UPSTREAM_MODEL, REPRO_UPSTREAM_BASE_URL, and REPRO_UPSTREAM_API_KEY locally for an OpenAI-compatible reasoning upstream. The nested cache controls request bypassing the upstream gateway's response cache. Omit them for a direct provider without those controls
{
"model": "cache-test-model",
"messages": [{
"role": "user",
"content": "Find the smallest positive integer n such that n mod 17 = 3 and n mod 19 = 5. Verify the answer briefly."
}],
"temperature": 0,
"max_tokens": 1024,
"stream": true,
"stream_options": {"include_usage": true}
}
Start a fresh proxy at each commit with a generated local-only key
From another terminal with the same local key, run this command twice. Inspect the terminal usage and the presence of x-litellm-cache-key, without publishing private headers
Synced with main at a5d05355b680506890034dc23a1cdfd2513697fd. Resolved the test-import conflict while retaining upstream tests and helper renames. Only the conversion module and its mapped regression test file differ from main
The two focused test files pass on Python 3.12, with 127 tests and no retries. Both changed executable lines have 100% diff coverage. Ruff checks pass for the production file and the test file using the repository's ruff-tests.toml configuration
The current tip uses Usage.model_validate rather than expanding an untyped dictionary into constructor arguments. This preserves the tested defaults and usage fields without adding unknown-argument diagnostics. No type-check ceilings were changed
Against the merge-base implementation, the four details-preservation cases fail as expected. The ten missing, null, empty, partial, and totals-only usage cases pass before and after the fix. The updated branch passes all fourteen cases
The October 14 update corrects only the author and committer identity on the two latest commits, using my GitHub-associated privacy address. Both commit trees are unchanged, and the merge commit keeps both original parents. CLA Assistant now confirms all committers have signed
A previous full local make check BASE_REF=origin/main attempt could not download build dependencies because PyPI DNS resolution timed out. It is not recorded as a full local pass. This update reran the focused tests, repository-scoped Ruff checks, diff coverage, and the live before/after replay above
The previous code-identical revision passed all four required hosted checks and Veria review. CI and automated reviews are rerunning on the current tip. Pending or unavailable results are not counted as passes
The same missing-details behavior was previously reproduced on v1.102.0. The first affected release is not established
Type
Bug Fix
Caveats
Low
Reasoning text replay is separate work
Nonzero prompt-cache counts have synthetic regression coverage
Live proof covers async replay, sync has regression coverage
Final Attestation
Regression tests check preserved fields, final-chunk placement, and legacy usage
⚠️1 benchmark spent significant time in system calls
System calls cannot be consistently instrumented, so they are not included in the measure, which understates the real cost. Please switch to the Walltime instrument to accurately measure system calls.
Comparing yyobject:codex/cache-stream-usage-details (3765999) with main (1aee1a2)1
Footnotes
No successful run was found on main (30b8db3) during the generation of this report, so 1aee1a2 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report. ↩
[Medium risk] Simplifies usage object construction in streaming response conversion.
The PR appears safe to merge; no outstanding findings remain.
Summary
The PR preserves cached usage details in synchronous and asynchronous streaming replay and adds regression tests. Since the previous review, it removed a duplicate CI shard; the existing core-utils shard still runs the replay tests and uploads coverage.
@greptileai Removed the duplicate replay shard. Standard core-utils coverage includes both files. Please review the updated tip and refreshed live evidence
@greptileai Replaced untyped usage expansion with schema validation to satisfy the updated type gate. Regression tests and fresh live replay still pass
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
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.
TLDR
Problem this solves:
How it solves it:
User Flow
Before, a streaming response-cache hit loses usage details
http://localhost:4000/v1/chat/completionswithstream: trueandstream_options.include_usage: trueAfter, a streaming response-cache hit preserves usage details
http://localhost:4000/v1/chat/completionswithstream: trueandstream_options.include_usage: trueRelevant issues
Related to #32068 and #32096, which concern reasoning text during cache replay. This PR fixes usage counts separately
Pre-Submission checklist
Screenshots / Proof of Fix
Fresh runs on October 14, 2026 used a local proxy, a real reasoning-model upstream, and a fresh in-memory response cache for each revision. Credentials, private endpoints, upstream model identifiers, and response IDs are omitted
Set
REPRO_UPSTREAM_MODEL,REPRO_UPSTREAM_BASE_URL, andREPRO_UPSTREAM_API_KEYlocally for an OpenAI-compatible reasoning upstream. The nested cache controls request bypassing the upstream gateway's response cache. Omit them for a direct provider without those controlscache-repro.yamlrequest.json{ "model": "cache-test-model", "messages": [{ "role": "user", "content": "Find the smallest positive integer n such that n mod 17 = 3 and n mod 19 = 5. Verify the answer briefly." }], "temperature": 0, "max_tokens": 1024, "stream": true, "stream_options": {"include_usage": true} }Start a fresh proxy at each commit with a generated local-only key
From another terminal with the same local key, run this command twice. Inspect the terminal usage and the presence of
x-litellm-cache-key, without publishing private headersBefore (a5d0535)
Run the shared curl command. HTTP 200,
[DONE]received, cache-key header absent{"prompt_tokens":59,"completion_tokens":281,"total_tokens":340,"prompt_tokens_details":{"cached_tokens":0},"completion_tokens_details":{"reasoning_tokens":188}}Repeat the command. HTTP 200,
[DONE]received, cache-key header present, same response ID. Reasoning drops from 188 to 0{"prompt_tokens":59,"completion_tokens":281,"total_tokens":340,"prompt_tokens_details":null,"completion_tokens_details":{"reasoning_tokens":0,"text_tokens":281}}After (3765999)
Run the shared curl command against the restarted proxy. HTTP 200,
[DONE]received, cache-key header absent{"prompt_tokens":59,"completion_tokens":306,"total_tokens":365,"prompt_tokens_details":{"cached_tokens":0},"completion_tokens_details":{"reasoning_tokens":231}}Repeat the command. HTTP 200,
[DONE]received, cache-key header present, same response ID. The full usage object matches{"prompt_tokens":59,"completion_tokens":306,"total_tokens":365,"prompt_tokens_details":{"cached_tokens":0},"completion_tokens_details":{"reasoning_tokens":231}}Validation
Synced with
mainata5d05355b680506890034dc23a1cdfd2513697fd. Resolved the test-import conflict while retaining upstream tests and helper renames. Only the conversion module and its mapped regression test file differ from mainThe two focused test files pass on Python 3.12, with 127 tests and no retries. Both changed executable lines have 100% diff coverage. Ruff checks pass for the production file and the test file using the repository's
ruff-tests.tomlconfigurationThe current tip uses
Usage.model_validaterather than expanding an untyped dictionary into constructor arguments. This preserves the tested defaults and usage fields without adding unknown-argument diagnostics. No type-check ceilings were changedAgainst the merge-base implementation, the four details-preservation cases fail as expected. The ten missing, null, empty, partial, and totals-only usage cases pass before and after the fix. The updated branch passes all fourteen cases
The October 14 update corrects only the author and committer identity on the two latest commits, using my GitHub-associated privacy address. Both commit trees are unchanged, and the merge commit keeps both original parents. CLA Assistant now confirms all committers have signed
A previous full local
make check BASE_REF=origin/mainattempt could not download build dependencies because PyPI DNS resolution timed out. It is not recorded as a full local pass. This update reran the focused tests, repository-scoped Ruff checks, diff coverage, and the live before/after replay aboveThe previous code-identical revision passed all four required hosted checks and Veria review. CI and automated reviews are rerunning on the current tip. Pending or unavailable results are not counted as passes
The same missing-details behavior was previously reproduced on v1.102.0. The first affected release is not established
Type
Bug Fix
Caveats
Low
Final Attestation