Skip to content

Fix: preserve LLM metadata (_chat_usage) in StructuredOutputHook#1125

Open
EIHEI2 wants to merge 1 commit intoagentscope-ai:mainfrom
EIHEI2:fix-issue-22
Open

Fix: preserve LLM metadata (_chat_usage) in StructuredOutputHook#1125
EIHEI2 wants to merge 1 commit intoagentscope-ai:mainfrom
EIHEI2:fix-issue-22

Conversation

@EIHEI2
Copy link
Copy Markdown

@EIHEI2 EIHEI2 commented Apr 4, 2026

Description

This PR fixes the metadata loss issue in StructuredOutputHook where LLM call metadata (such as _chat_usage containing token usage information) was lost during the structured output generation flow.

Root Cause

In handlePostReasoning(), when the model called generate_response tool, the reasoning message metadata was not being captured. This caused the final ASSISTANT message to lose metadata fields like _chat_usage.

Solution

Modified handlePostReasoning() to capture and aggregate ChatUsage and ThinkingBlock from the reasoning message when tools are called. This ensures metadata is properly propagated through the entire structured output flow.

Changes

  • agentscope-core/src/main/java/io/agentscope/core/agent/StructuredOutputHook.java: Added metadata capture logic in handlePostReasoning() to aggregate ChatUsage and ThinkingBlock from reasoning messages.

Testing

The fix ensures that:

  1. Memory path: The final message saved to session/memory preserves metadata
  2. API return path: The Msg returned by agent.call() preserves metadata
  3. When both reasoning and response messages contain metadata, they are properly merged instead of overwritten.

@EIHEI2 EIHEI2 requested a review from a team April 4, 2026 02:01
@cla-assistant
Copy link
Copy Markdown

cla-assistant bot commented Apr 4, 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.


OpenClaw Dev seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

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.

1 participant