Add provider_cache_read/write_input_tokens to Usage type#7032
Merged
AntoineToussaint merged 7 commits intomainfrom Mar 23, 2026
Merged
Add provider_cache_read/write_input_tokens to Usage type#7032AntoineToussaint merged 7 commits intomainfrom
AntoineToussaint merged 7 commits intomainfrom
Conversation
Add two new Optional<u32> fields to the Usage struct for tracking provider-reported cache token counts. All existing constructors initialized with None. Aggregation helpers in mod.rs and streams.rs updated to propagate cache tokens through lenient summation. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
3 tasks
virajmehta
approved these changes
Mar 23, 2026
virajmehta
previously approved these changes
Mar 23, 2026
The #[serde(default, skip_serializing_if)] on the new cache fields is for backward compatibility with existing serialized data that predates these fields. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Usage is never deserialized from JSON — fields are always constructed in Rust code. Keep cache fields plain like input_tokens/output_tokens. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
virajmehta
previously approved these changes
Mar 23, 2026
Per AGENTS.md convention: omit optional fields from API responses when None. Most responses won't have cache data, so avoid cluttering every response with null fields. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
virajmehta
approved these changes
Mar 23, 2026
GabrielBianconi
approved these changes
Mar 23, 2026
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
Adds
provider_cache_read_input_tokens: Option<u32>andprovider_cache_write_input_tokens: Option<u32>to the coreUsagestruct (crates/tensorzero-core/src/inference/types/usage.rs).Production code changes
usage.rs: Two new fields onUsage, updatedzero(),total_tokens(), and aggregation logic (lenient summation that preservesSomevalues)mod.rs:aggregate_usage_across_model_inferencesupdated to propagate cache tokensstreams.rs: Streaming usage accumulation updated for cache tokensUsagedataclass updated with new optional fieldsUsage.tsregeneratedTest / mechanical changes
All other files (providers, variants, endpoints, evaluations, etc.) are mechanical: adding
provider_cache_read_input_tokens: None, provider_cache_write_input_tokens: Noneto existingUsage {}constructors so the code compiles.What's NOT in this PR
PR Stack
Test plan
cargo check --all-targets --all-featurescargo clippy --all-targets --all-features -- -D warningscargo test-unit-fast(12 pre-existing failures unrelated)🤖 Generated with Claude Code