Skip to content

To retrieve the current token consumption during the reasoning process, assign a value to ChatUsage in ReasoningChunkEvent.accumulated#935

Open
MrYang-Jia wants to merge 10 commits intoagentscope-ai:mainfrom
MrYang-Jia:feature/chatusage-v1.0.10
Open

To retrieve the current token consumption during the reasoning process, assign a value to ChatUsage in ReasoningChunkEvent.accumulated#935
MrYang-Jia wants to merge 10 commits intoagentscope-ai:mainfrom
MrYang-Jia:feature/chatusage-v1.0.10

Conversation

@MrYang-Jia
Copy link

@MrYang-Jia MrYang-Jia commented Mar 12, 2026

To retrieve the current token consumption during the reasoning process, assign a value to ChatUsage in ReasoningChunkEvent.accumulated:

  1. Add the getChatUsage() method to ReasoningContext.
  2. In ReActAgent.notifyReasoningChunk, assign ReasoningContext.getChatUsage() to accumulated.metadata._chat_usage. This allows real-time retrieval of consumed tokens during the reasoning process, facilitating interrupted billing recovery or token replenishment for missing billing records.

chinese:
为了在思考过程中获取到当前的tokens损耗,所以在 ReasoningChunkEvent.accumulated 里为 ChatUsage 赋值 1、ReasoningContext 增加 getChatUsage() 方法
2、ReActAgent.notifyReasoningChunk 将 ReasoningContext.getChatUsage() 赋予 accumulated.metadata._chat_usage ,这样子思考过程就可以实时获取到损耗的tokens,方便中断计费或丢失计费补充tokens

AgentScope-Java Version

AgentScope-Java 1.0.11-SNAPSHOT

Description

To retrieve the current token consumption during the reasoning process, assign a value to ChatUsage in ReasoningChunkEvent.accumulated

Checklist

Please check the following items before code is ready to be reviewed.

  • [ ✔] Code has been formatted with mvn spotless:apply
  • [ ✔] All tests are passing (mvn test)
  • Javadoc comments are complete and follow project conventions
  • Related documentation has been updated (e.g. links, examples, etc.)
  • [ ✔] Code is ready for review

To retrieve the current token consumption during the reasoning process, assign a value to `ChatUsage` in `ReasoningChunkEvent.accumulated`:
1. Add the `getChatUsage()` method to `ReasoningContext`.
2. In `ReActAgent.notifyReasoningChunk`, assign `ReasoningContext.getChatUsage()` to `accumulated.metadata._chat_usage`. This allows real-time retrieval of consumed tokens during the reasoning process, facilitating interrupted billing recovery or token replenishment for missing billing records.

chinese:
为了在思考过程中获取到当前的tokens损耗,所以在 ReasoningChunkEvent.accumulated 里为 ChatUsage 赋值
1、ReasoningContext 增加 getChatUsage() 方法
2、ReActAgent.notifyReasoningChunk 将 ReasoningContext.getChatUsage() 赋予 accumulated.metadata._chat_usage ,这样子思考过程就可以实时获取到损耗的tokens,方便中断计费或丢失计费补充tokens
@MrYang-Jia MrYang-Jia requested a review from a team March 12, 2026 09:57
@cla-assistant
Copy link

cla-assistant bot commented Mar 12, 2026

CLA assistant check
All committers have signed the CLA.

@cla-assistant
Copy link

cla-assistant bot commented Mar 12, 2026

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@codecov
Copy link

codecov bot commented Mar 12, 2026

Codecov Report

❌ Patch coverage is 86.66667% with 2 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
...e/src/main/java/io/agentscope/core/ReActAgent.java 87.50% 0 Missing and 1 partial ⚠️
...scope/core/agent/accumulator/ReasoningContext.java 85.71% 0 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

Add test cases for ReasoningContext.getChatUsage() to cover the partial uncovered line, fix patch coverage from 85.71% to 100%
@MrYang-Jia
Copy link
Author

我已补签了CLA协议,请继续

yzj114477 and others added 6 commits March 13, 2026 10:37
…eve full coverage testing for accumulated message metadata chat usage

test: 新增 testChatUsageInAccumulatedMessageMetadata 测试案例,进行覆盖测试
…Test.testFixedDelayReschedulesAndRunsMultipleTimes to prevent test failures due to slower execution speed in CI environment

test: Modify the timeout configuration in QuartzFixedDelayIntegrationTest.testFixedDelayReschedulesAndRunsMultipleTimes to prevent test failures due to slower execution speed in CI environment
.build();
if (context.getChatUsage() != null) {
accumulated
.getMetadata()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we also add CHAT_USAGE to notifySummaryChunk for consistency?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, thank you very much for your help.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have fixed the issue you mentioned. The results from notifySummaryChunk and notifyReasoningChunk are now consistent, so the Hook system can access token usage when processing SummaryChunkEvent in both modules.

yzj114477 and others added 2 commits March 26, 2026 09:57
…constructing the accumulated message, consistent with notifyReasoningChunk. This allows the Hook system to access statistics such as token usage when processing SummaryChunkEvent.

notifySummaryChunk 方法在构建 accumulated 消息时也会添加 CHAT_USAGE 元数据,与 notifyReasoningChunk 保持一致。这样 Hook 系统在处理 SummaryChunkEvent 时也能访问到 token 使用量等统计信息。
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants