Skip to content

feat(telemetry): count what a turn cost per Agent and creator - #697

Draft
teemow wants to merge 2 commits into
giantswarmfrom
gs-38073-cost
Draft

teemow wants to merge 2 commits into
giantswarmfrom
gs-38073-cost

Conversation

@teemow

@teemow teemow commented Oct 18, 2026

Copy link
Copy Markdown
Member

Problem

The platform wants to see and alert on what one person's coding agent Sessions spend. Each turn's runtime already reports its cost on the turn's final status message (costUsd of kagent.dev/a2a/usage, Claude Code's total_cost_usd, one process per turn), but nothing counted it where Prometheus scrapes, and nothing tied it to the Session's creator.

Proposed solution

  • go/api/a2a: TurnCostUSDOf reads the reported cost from a status message; absent means not reported (Codex), a non-object usage or a cost that is not a finite non-negative number is malformed.
  • The task store counts it when it publishes the boundary, at the same once-per-boundary hook as the forwarder refusals, so a retried settlement counts nothing twice. A malformed usage is logged and counts nothing.
  • Metric on the controller's scraped endpoint: kagent.session.turn.cost.usd (unit {USD}) → kagent_session_turn_cost_usd_total{k8s_namespace_name, gen_ai_agent_name, enduser_id}, enduser.id being the Session's creator.

The creator as a label is deliberate: spend is bounded per person, and the label's cardinality is the number of people who create Sessions. It puts a person's identity into the controller's metrics; that trade-off is open for review and changes forward if decided otherwise. Documented in docs/architecture/runtime-and-lifecycle.md ("Turn cost metrics").

Acceptance criteria

  • Unit tests: TestTurnCostUSDOf (reported, absent, malformed), TestSettleTaskCountsTheTurnCostOfAPublishedBoundaryOncePerCreator (through the Prometheus exporter: the scraped name, the labels, counted once over a retried settlement), TestSettleTaskCountsNoCostARuntimeDoesNotReport; make -C go lint 0 issues; scripts/fork/carried.sh --check clean.
  • agentlab: a claude coding turn on a controller built from this branch exports kagent_session_turn_cost_usd_total with the turn's cost and creator, and the spend alert fires on it: pending.

A runtime that reports a turn's cost (Claude Code's total_cost_usd, one
process per turn) puts it on the turn's final status message as costUsd
of kagent.dev/a2a/usage. The task store counts it when it publishes that
boundary, once per boundary, in kagent.session.turn.cost.usd
(kagent_session_turn_cost_usd_total) with k8s.namespace.name and
gen_ai.agent.name of the Session's Agent and enduser.id, the Session's
creator, so spend can be bounded and alerted per person. A runtime that
reports no cost (Codex) counts nothing; a malformed usage is logged.

Signed-off-by: Timo Derstappen <teemow@gmail.com>
Signed-off-by: Timo Derstappen <teemow@gmail.com>
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