feat(core): support reasoning and cached tokens in ChatUsage#1272
Open
jujn wants to merge 4 commits intoagentscope-ai:mainfrom
Open
feat(core): support reasoning and cached tokens in ChatUsage#1272jujn wants to merge 4 commits intoagentscope-ai:mainfrom
jujn wants to merge 4 commits intoagentscope-ai:mainfrom
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR extends ChatUsage to expose advanced token metrics (reasoning and cached tokens) across model parsers, aggregators, and related tests to support improved cost/observability analysis (closes #1264).
Changes:
- Added optional
reasoningTokensandcachedTokensfields toChatUsage(constructor, builder, getters). - Updated OpenAI/Gemini/DashScope/Anthropic response parsers and streaming/aggregation code to populate the new fields.
- Expanded unit tests to validate parsing and aggregation of the new token metrics.
Reviewed changes
Copilot reviewed 19 out of 19 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| agentscope-extensions/agentscope-extensions-studio/src/main/java/io/agentscope/core/tracing/telemetry/StreamChatResponseAggregator.java | Aggregates streaming ChatUsage including reasoning/cached token totals. |
| agentscope-core/src/main/java/io/agentscope/core/model/ChatUsage.java | Adds optional reasoningTokens/cachedTokens to the usage model (API + builder). |
| agentscope-core/src/main/java/io/agentscope/core/message/Msg.java | Updates getChatUsage() Javadoc example to include advanced token fields. |
| agentscope-core/src/main/java/io/agentscope/core/message/MessageMetadataKeys.java | Updates metadata key documentation to mention advanced token metrics. |
| agentscope-core/src/main/java/io/agentscope/core/formatter/openai/OpenAIResponseParser.java | Extracts OpenAI nested cached_tokens / reasoning_tokens into ChatUsage. |
| agentscope-core/src/main/java/io/agentscope/core/formatter/gemini/GeminiResponseParser.java | Adds Gemini reasoning/cached token extraction and stores it into ChatUsage. |
| agentscope-core/src/main/java/io/agentscope/core/formatter/dashscope/dto/DashScopeUsage.java | Extends DashScope DTO to include reasoning_tokens / cached_tokens. |
| agentscope-core/src/main/java/io/agentscope/core/formatter/dashscope/DashScopeResponseParser.java | Maps new DashScope DTO usage fields into ChatUsage. |
| agentscope-core/src/main/java/io/agentscope/core/formatter/anthropic/AnthropicResponseParser.java | Extracts Anthropic cache read tokens into ChatUsage.cachedTokens. |
| agentscope-core/src/main/java/io/agentscope/core/agent/accumulator/ReasoningContext.java | Propagates reasoning/cached tokens into accumulated usage metadata. |
| agentscope-core/src/main/java/io/agentscope/core/agent/StructuredOutputHook.java | Aggregates reasoning/cached tokens across intermediate structured-output messages. |
| agentscope-core/src/test/java/io/agentscope/core/message/MsgTest.java | Adds test ensuring Msg.getChatUsage() preserves advanced token fields. |
| agentscope-core/src/test/java/io/agentscope/core/formatter/openai/OpenAIResponseParserTest.java | Adds OpenAI parsing test for reasoning/cached tokens. |
| agentscope-core/src/test/java/io/agentscope/core/formatter/gemini/GeminiResponseParserTest.java | Adds Gemini parsing test covering reasoning/cached token handling. |
| agentscope-core/src/test/java/io/agentscope/core/formatter/dashscope/DashScopeResponseParserTest.java | Adds DashScope parsing test for reasoning/cached tokens. |
| agentscope-core/src/test/java/io/agentscope/core/formatter/anthropic/AnthropicResponseParserTest.java | Adds Anthropic parsing test for cached tokens (reasoning unsupported). |
| agentscope-core/src/test/java/io/agentscope/core/agent/accumulator/ReasoningContextTest.java | Updates accumulator tests to include reasoning/cached tokens. |
| agentscope-core/src/test/java/io/agentscope/core/agent/StructuredOutputHookTest.java | Adds aggregation test ensuring advanced tokens are summed correctly. |
| agentscope-core/src/test/java/io/agentscope/core/agent/ReActAgentTest.java | Updates test to validate ChatUsage advanced tokens propagate through agent flow. |
Comments suppressed due to low confidence (1)
agentscope-core/src/main/java/io/agentscope/core/formatter/gemini/GeminiResponseParser.java:105
- In Gemini usage parsing,
outputTokensis computed ascandidatesTokenCount - thoughtsTokenCount, meaning ChatUsage.outputTokens excludes reasoning while ChatUsage.reasoningTokens is set separately. Other parsers (e.g., OpenAIResponseParser) set outputTokens to the full completion tokens and treat reasoningTokens as a breakdown. This inconsistency makes ChatUsage hard to compare across providers and can makegetTotalTokens()misleading for Gemini. Consider aligning semantics (e.g., keep outputTokens as total completion tokens and use reasoningTokens only as a breakdown, or apply the same exclusion rule everywhere and update docs/tests accordingly).
int thinkingTokens = reasoningTokens != null ? reasoningTokens : 0;
// Output tokens exclude thinking tokens (following DashScope behavior)
// In Gemini, candidatesTokenCount includes thinking, so we subtract it
int outputTokens = totalOutputTokens - thinkingTokens;
Codecov Report❌ Patch coverage is 📢 Thoughts on this report? Let us know! |
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.
Description
Close #1264
Checklist
Please check the following items before code is ready to be reviewed.
mvn spotless:applymvn test)