feat(model): add cached_tokens support to ChatUsage for all providers#1343
Open
YingchaoX wants to merge 1 commit intoagentscope-ai:mainfrom
Open
feat(model): add cached_tokens support to ChatUsage for all providers#1343YingchaoX wants to merge 1 commit intoagentscope-ai:mainfrom
YingchaoX wants to merge 1 commit intoagentscope-ai:mainfrom
Conversation
- Add cached_tokens field to ChatUsage dataclass - Implement cached_tokens extraction for OpenAI, Anthropic, Gemini, DashScope - Only use cache_read_input_tokens for Anthropic (not cache_creation) - Preserve 0 values from APIs (don't convert to None) - Support both dict and object formats in API responses - Add tracing attribute AGENTSCOPE_USAGE_CACHED_TOKENS - Update tracing extractor to report cached_tokens - Add comprehensive tests for all providers Fixes agentscope-ai#1324
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.
AgentScope Version
1.0.17Description
Background
Issue #1324 requested support for
cached_tokensinChatUsageto track prompt caching metrics fromOpenAI SDK's
prompt_tokens_details.cached_tokens. This is important for monitoring cache hit rates and optimizing costs.
Changes Made
cached_tokensfield toChatUsagedataclasscached_tokensextraction for OpenAI, Anthropic, Gemini, DashScopecache_read_input_tokensfor Anthropic (notcache_creation) to avoid false positives oncold requests
AGENTSCOPE_USAGE_CACHED_TOKENScached_tokensHow to Test
Testing Status
• ✅ OpenAI: Unit tested⚠️ Gemini: Code implementation complete (needs community testing with actual API)
• ✅ DashScope: Tested with actual API
• ✅ Anthropic: Tested via DashScope Anthropic-compatible endpoint
•
Checklist
• [x] Code has been formatted with pre-commit run --all-files command
• [x] All tests are passing
• [x] Docstrings are in Google style
• [ ] Related documentation has been updated (e.g. links, examples, etc.)
• [x] Code is ready for review
Notes
The Gemini implementation follows the same pattern as other providers and has been code-reviewed, but
could benefit from testing by someone with Gemini API access.
Fixes #1324