-
Notifications
You must be signed in to change notification settings - Fork 11.1k
fix: openrouter claude cache usage #2811
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Calcium-Ion
merged 2 commits into
QuantumNous:main
from
seefs001:fix/openrouter-claude-cache-usage
Feb 2, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: QuantumNous/new-api
Length of output: 3206
🏁 Script executed:
Repository: QuantumNous/new-api
Length of output: 5591
🏁 Script executed:
Repository: QuantumNous/new-api
Length of output: 4368
🌐 Web query:
Anthropic Claude API message_delta usage fields streaming: are zero values omitted or explicitly sent?💡 Result:
In Anthropic streaming, the
message_delta.usageobject is sparse: it may include only the usage fields that were added/updated in that delta (often justoutput_tokens), so you cannot rely on “zero” fields being explicitly sent—they may be omitted entirely. This is reflected in Anthropic’s own streaming examples wheremessage_delta.usagecontains only{"output_tokens": ...}. [1]When a field is included, it can legitimately be
0(e.g., server responses have been observed sendingcache_creation_input_tokens: 0). [2]Also, depending on the client/SDK/proxy, you may see fields present but set to
null(an SDK serialization behavior), so robust code should treat missing or null as “not provided” rather than assuming explicit zeros. [3]Practical takeaway: treat
message_delta.usageas a partial patch; maintain your own cumulative usage state and update only the fields that appear. [1]Sources: [1] Anthropic streaming docs example shows
message_deltawithusage: {"output_tokens": ...}only. [2] Captured server streamingmessage_deltashows explicitcache_creation_input_tokens: 0. [3] SDK/proxy issue report showsmessage_delta.usagefields sometimes emitted asnull.🌐 Web query:
Anthropic Claude streaming API documentation message_delta cache_read_input_tokens cache_creation_input_tokens behavior💡 Result:
In Claude’s streaming Messages API, you’ll see one or more
message_deltaSSE events near the end of the stream. These events apply top-level updates to the in-progressMessageobject (not per-text chunks—that’scontent_block_delta). Theusageobject onmessage_deltais cumulative across the whole request/stream, so you should treat the latestmessage_delta.usageyou receive as the final totals. [2]Within that cumulative
usage:cache_creation_input_tokens: how many input tokens were written into the prompt cache (i.e., used to create/extend a cache entry) for this request. These are counted as part of total input usage. [1]cache_read_input_tokens: how many input tokens were served from an existing prompt cache hit (i.e., read from cache) for this request. Also counted as part of total input usage. [1]input_tokens + cache_creation_input_tokens + cache_read_input_tokens. [1]So, behaviorally: if caching is enabled and you get a cache hit, you’ll see non-zero
cache_read_input_tokens; if your request creates a new cache entry, you’ll see non-zerocache_creation_input_tokens—and both show up in the cumulativemessage_delta.usageduring streaming. [1][2]Consider using pointer fields or custom JSON unmarshaling to distinguish omitted from explicit zero values in
message_delta.usage.The
> 0checks assume omitted fields are unmarshaled as zero, which aligns with Anthropic's sparsemessage_delta.usageAPI (fields not sent are treated as "not provided"). However, Anthropic can legitimately send fields with value0(e.g.,cache_read_input_tokens: 0), and the current code will skip those updates, preserving stale values frommessage_startinstead of accepting the authoritative cumulative values. While unlikely to manifest in practice for cache fields (rarely present inmessage_delta), this is a subtle defect. To be fully correct, consider using*intpointers or custom JSON unmarshaling so explicit zeros overwrite prior values while truly omitted fields don't.🤖 Prompt for AI Agents