feat(metadata): Add token usage and execution metrics to A2A task responses#127
Merged
feat(metadata): Add token usage and execution metrics to A2A task responses#127
Conversation
…ponses
Implements comprehensive usage tracking for token consumption and execution statistics:
**Token Usage Tracking:**
- Track prompt_tokens, completion_tokens, and total_tokens from LLM responses
- Support both streaming and non-streaming LLM interactions
- Aggregate usage across multiple LLM calls in iterative agent execution
**Execution Statistics:**
- Track iteration count (number of agent execution loops)
- Track message count (total messages in conversation)
- Track tool_calls count (total tool invocations)
- Track failed_tools count (number of failed tool executions)
**Implementation:**
- Created UsageTracker component to aggregate metrics during execution
- Integrated tracker into agent streaming execution (RunWithStream)
- Inject tracker via context in both background and streaming task handlers
- Populate Task.Metadata field with collected metrics on task completion
- Added configuration option AGENT_CLIENT_ENABLE_USAGE_METADATA (default: true)
**Testing:**
- Comprehensive unit tests for UsageTracker with thread-safety verification
- Integration tests for both background and streaming task handlers
- All existing tests pass without modification
**API Changes:**
Task responses now include metadata section with usage statistics:
{
"task": {
"id": "task-123",
"status": { "state": "completed" },
"metadata": {
"usage": {
"prompt_tokens": 156,
"completion_tokens": 89,
"total_tokens": 245
},
"execution_stats": {
"iterations": 2,
"messages": 4,
"tool_calls": 1,
"failed_tools": 0
}
}
}
}
Closes #126
🤖 Generated with [Claude Code](https://claude.ai/code)
Co-authored-by: Eden Reich <edenreich@users.noreply.github.com>
edenreich
commented
Dec 15, 2025
edenreich
commented
Dec 15, 2025
edenreich
commented
Dec 15, 2025
edenreich
commented
Dec 15, 2025
edenreich
commented
Dec 15, 2025
edenreich
commented
Dec 15, 2025
edenreich
commented
Dec 15, 2025
edenreich
commented
Dec 15, 2025
edenreich
commented
Dec 15, 2025
edenreich
commented
Dec 15, 2025
edenreich
commented
Dec 15, 2025
edenreich
commented
Dec 15, 2025
edenreich
commented
Dec 15, 2025
edenreich
commented
Dec 15, 2025
edenreich
commented
Dec 15, 2025
edenreich
commented
Dec 15, 2025
edenreich
commented
Dec 15, 2025
edenreich
commented
Dec 15, 2025
edenreich
commented
Dec 15, 2025
edenreich
commented
Dec 15, 2025
edenreich
commented
Dec 15, 2025
edenreich
commented
Dec 15, 2025
edenreich
commented
Dec 15, 2025
edenreich
commented
Dec 15, 2025
edenreich
commented
Dec 15, 2025
edenreich
commented
Dec 15, 2025
edenreich
commented
Dec 15, 2025
edenreich
commented
Dec 15, 2025
edenreich
commented
Dec 15, 2025
edenreich
commented
Dec 15, 2025
edenreich
commented
Dec 15, 2025
edenreich
commented
Dec 15, 2025
edenreich
commented
Dec 15, 2025
Contributor
Author
|
@claude Please add an example that explains how to use this feature - follow the convention of existing examples. |
Contributor
|
Claude finished @edenreich's task in 5m 16s —— View job I'll analyze this and get back to you. |
…tracking Add comprehensive example showing how to track and access token usage and execution metrics through the Task.Metadata field. This example demonstrates: - Automatic token usage tracking (prompt_tokens, completion_tokens, total_tokens) - Execution statistics collection (iterations, messages, tool_calls, failed_tools) - Configuration via A2A_AGENT_CLIENT_ENABLE_USAGE_METADATA environment variable - Client-side metadata access and display - Integration with default task handlers The example includes: - Server with calculator tool demonstrating tool call tracking - Client with formatted metadata display - Complete Docker Compose setup with Inference Gateway - Comprehensive README with use cases and troubleshooting - Updated examples/README.md to reference the new example 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Eden Reich <edenreich@users.noreply.github.com>
Signed-off-by: Eden Reich <eden.reich@gmail.com>
Contributor
Author
|
@claude fix the CI errors. |
Contributor
|
Claude finished @edenreich's task in 2m 14s —— View job I'll analyze this and get back to you. |
Co-authored-by: Eden Reich <edenreich@users.noreply.github.com>
Signed-off-by: Eden Reich <eden.reich@gmail.com>
Fix type compatibility issues in task metadata handling after A2A proto schema migration. The Metadata field is now a pointer to map[string]any (*Struct), requiring proper pointer initialization and dereferencing. Changes: - Fix task.Metadata initialization to use pointer assignment - Update metadata access to dereference pointer properly - Use types.NewTextPart() utility in tests for message parts - Fix TaskStateCanceled to TaskStateCancelled enum name Signed-off-by: Eden Reich <eden.reich@gmail.com>
Signed-off-by: Eden Reich <eden.reich@gmail.com>
Signed-off-by: Eden Reich <eden.reich@gmail.com>
Signed-off-by: Eden Reich <eden.reich@gmail.com>
ig-semantic-release-bot bot
added a commit
that referenced
this pull request
Dec 30, 2025
## [0.17.0](v0.16.2...v0.17.0) (2025-12-30) ### ✨ Features * **metadata:** Add token usage and execution metrics to A2A task responses ([#127](#127)) ([1f49f97](1f49f97)), closes [#126](#126) ### ♻️ Improvements * Migrate to the latest A2A proto schema ([#129](#129)) ([02c2bc1](02c2bc1)) ### 👷 CI * Bump generator tool to 0.2.2 ([fc58ab9](fc58ab9)) ### 🔧 Miscellaneous * **deps:** Bump claude-code 2.0.65 to 2.0.76 ([47f0a79](47f0a79)) * **deps:** Bump generator tool ([659aec4](659aec4)) * **deps:** Update dependencies and infer configuration ([#128](#128)) ([0fa9d33](0fa9d33)) * **deps:** Update infer config ([76589af](76589af))
Contributor
|
🎉 This PR is included in version 0.17.0 🎉 The release is available on GitHub release Your semantic-release bot 📦🚀 |
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.
Summary
Implements token usage and execution metrics tracking in A2A task responses through the
Task.Metadatafield, enabling clients to monitor resource consumption, costs, and performance.Closes #126
Changes
Core Infrastructure
Task Handler Enhancements
DefaultBackgroundTaskHandlerto track and populate usage metadataDefaultStreamingTaskHandlerto track and populate usage metadatapopulateTaskMetadata()Agent Integration
RunWithStream()to create and inject UsageTracker into contextexecuteToolCallsWithEvents()Configuration
EnableUsageMetadatafield toAgentConfig(default: true)AGENT_CLIENT_ENABLE_USAGE_METADATAMetadata Structure
{ "usage": { "prompt_tokens": 156, "completion_tokens": 89, "total_tokens": 245 }, "execution_stats": { "iterations": 2, "messages": 4, "tool_calls": 1, "failed_tools": 0 } }Testing
Breaking Changes
None - metadata is additive and backward compatible.
🤖 Generated with Claude Code