feat(a2a): emit aggregated turn token usage on the terminal status update - #2461
Open
QuentinBisson wants to merge 1 commit into
Open
feat(a2a): emit aggregated turn token usage on the terminal status update#2461QuentinBisson wants to merge 1 commit into
QuentinBisson wants to merge 1 commit into
Conversation
QuentinBisson
force-pushed
the
feat/a2a-usage-total-v2
branch
from
August 17, 2026 13:40
90d57f0 to
c8981c7
Compare
QuentinBisson
marked this pull request as ready for review
August 17, 2026 14:00
Copilot stopped reviewing on behalf of
QuentinBisson due to an error
August 17, 2026 14:01
…date Both runtimes accumulate non-partial per-call token usage across an execution and stamp it on the terminal status update (completed, input-required, failed) under kagent_usage_total. Resumed tasks seed the accumulator from the task metadata, so the total covers the whole task rather than the last segment. The value uses the same genai UsageMetadata serialization as the per-event adk_usage_metadata / kagent_usage_metadata entries, plus modelVersion. Signed-off-by: QuentinBisson <quentin@giantswarm.io>
QuentinBisson
force-pushed
the
feat/a2a-usage-total-v2
branch
from
August 17, 2026 19:01
c8981c7 to
a8d0c31
Compare
Contributor
Author
|
Rebased onto The three remaining red checks are not from this branch, they reproduce identically on
|
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.
Supersedes #2332, which the stale bot closed and GitHub refuses to reopen. This is a reimplementation, not a rebase: both executors were rewritten to delegate to the ADK library's own A2A executor, so the manual event loop the original PR hooked into no longer exists.
What this adds
Every terminal status update (
completed,input-required,failed) carries akagent_usage_totalmetadata entry with the token usage summed across the task:{ "promptTokenCount": 300, "candidatesTokenCount": 50, "totalTokenCount": 350, "modelVersion": "gpt-4o-2024-11-20" }Partial (streaming chunk) events are skipped, since each LLM call reports its usage on its final non-partial event. Resumed tasks seed the accumulator from
Task.metadata, so a task that spans several executions (HITL input-required cycles, follow-up messages) reports a task-lifetime total instead of the last segment only. The value is serialized with the same genaiUsageMetadatamapping as the existing per-eventadk_usage_metadata/kagent_usage_metadataentries, plusmodelVersion, so consumers can share one parser. A Go test asserts that equality directly.a2a-gomerges terminal status-update metadata into the stored task, which is both how the total is persisted and how the next execution reads it back.How it differs from #2332
RunnerProviderthat wraps the ADK runner instead ofAfterEventCallback. The callback only fires for ADK events that convert to an A2A artifact, and the input-required processor consumes the paused tool-call event, which is exactly the event carrying the usage of the call that paused. Wrapping the runner keeps the original vantage point: every ADK event, before conversion. The provider mirrors the ADK default provider (it has to, sinceRunnerProviderreplacesRunnerConfig). Stamping uses the newAfterExecuteCallbackhook, which covers all three terminal states in one place._ExecutionStatewith an after-event interceptor and stamps metadata onto the final status update in_after_agent, so the accumulator plugs straight into those. One ADK event can be converted into several A2A events there, and the interceptor runs per A2A event, so events already counted are tracked by id.Verification
go build ./adk/... ./core/...,go test ./adk/... ./core/...(1843 passed),go vet,golangci-lintuv run pytest packages/kagent-adk/tests/unittests— the 9 failures inmodels/test_tls_e2e.pyare pre-existing in this environment (verified against a clean checkout) and unrelated