Skip to content

feat(adk): emit cached token series on gen_ai token usage metric (#2669) - #2677

Open
MartinForReal wants to merge 2 commits into
kagent-dev:mainfrom
MartinForReal:feat/prompt-cache-cached-token-metric
Open

feat(adk): emit cached token series on gen_ai token usage metric (#2669)#2677
MartinForReal wants to merge 2 commits into
kagent-dev:mainfrom
MartinForReal:feat/prompt-cache-cached-token-metric

Conversation

@MartinForReal

Copy link
Copy Markdown

Step 2 of #2669: emit a cached token-type series on the existing gen_ai.client.token.usage metric so prompt-cache hits become aggregatable (gen_ai_token_type="cached").

This change stacks on top of the token-usage histogram introduced in (#2673) — "feat(adk): expose gen_ai token usage Prometheus metric" — which must merge first.

What changes

  • Adds a cached series (gen_ai_token_type="cached") to the gen_ai_client_token_usage histogram.
  • RecordTokenUsage records the cached prompt-token count when positive; zero/negative counts are skipped, matching the existing input/output behavior.
  • recordTokenUsage in the A2A executor wires UsageMetadata.CachedContentTokenCount through as the cached token count.

Testing

  • Unit tests cover the cached series (recorded when enabled and > 0, skipped when disabled or non-positive) and the executor wiring (a non-partial event with CachedContentTokenCount emits a gen_ai_token_type="cached" series valued at the cached count; partial events and absent/zero cached counts emit none).
  • gofmt -l clean on all changed files; go build ./..., go vet ./adk/..., go test ./adk/pkg/a2a/... and go test ./adk/pkg/telemetry/... pass.

DCO

Signed-off-by: MartinForReal fanshangxiang@gmail.com

Refs: #2669, #2673

Record the OpenTelemetry GenAI-semconv gen_ai.client.token.usage metric from the Go ADK runtime using the native Prometheus client, served at /metrics. Labels follow the upstream Google ADK Python runtime so one dashboard works across both runtimes (gen_ai.token.type, gen_ai.request.model, gen_ai.response.model, gen_ai.provider.name, gen_ai.agent.name, gen_ai.operation.name, error.type). Recording and the scrape endpoint are gated default-OFF behind OTEL_METRICS_ENABLED, matching the existing OTEL_TRACING_ENABLED / OTEL_LOGGING_ENABLED gates.

Signed-off-by: MartinForReal <fanshangxiang@gmail.com>
Signed-off-by: MartinForReal <fanshangxiang@gmail.com>
@github-actions github-actions Bot added the enhancement New feature or request label Sep 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant