Add unified cache usage metrics to Usage interface#5657
Open
sobychacko wants to merge 1 commit intospring-projects:mainfrom
Open
Add unified cache usage metrics to Usage interface#5657sobychacko wants to merge 1 commit intospring-projects:mainfrom
sobychacko wants to merge 1 commit intospring-projects:mainfrom
Conversation
Prompt caching can save up to 90% on input token costs, but today reading cache metrics requires provider-specific code — casting to native SDK types, navigating nested records, or reading from metadata maps differently for each provider. Add getCacheReadInputTokens() and getCacheWriteInputTokens() as default methods on the Usage interface, returning null for providers that don't support caching. Update DefaultUsage with new fields, constructor, and JSON serialization. Update UsageCalculator to accumulate cache metrics across streaming chunks and tool-calling loops. Wire up cache metrics extraction in four providers: - Anthropic: cacheReadInputTokens and cacheCreationInputTokens (sync and streaming) - Bedrock: cacheReadInputTokens and cacheWriteInputTokens (sync and streaming) - OpenAI SDK: cachedTokens from PromptTokensDetails (read only) - Google GenAI: cachedContentTokenCount override (read only) Add unit tests for DefaultUsage cache fields including serialization round-trips. Add integration test assertions to Anthropic and Bedrock caching tests verifying Usage interface values match native SDK values. Update usage-handling reference documentation with cache metrics table. Signed-off-by: Soby Chacko <soby.chacko@broadcom.com>
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.
Prompt caching can save up to 90% on input token costs, but today reading cache metrics requires provider-specific code — casting to native SDK types, navigating nested records, or reading from metadata maps differently for each provider.
Add getCacheReadInputTokens() and getCacheWriteInputTokens() as default methods on the Usage interface, returning null for providers that don't support caching. Update DefaultUsage with new fields, constructor, and JSON serialization. Update UsageCalculator to accumulate cache metrics across streaming chunks and tool-calling loops.
Wire up cache metrics extraction in four providers:
Add unit tests for DefaultUsage cache fields including serialization round-trips. Add integration test assertions to Anthropic and Bedrock caching tests verifying Usage interface values match native SDK values.
Update usage-handling reference documentation with cache metrics table.