Evals & Observability·Question 33 of 83Intermediate

How do you instrument cost and latency for an agentic feature, and what actually drives cost creep?

Type it the way you would explain it to an interviewer. A few sentences is enough; you can't break anything.

Model answer: the part to lead with

Instrument at the span level and aggregate at the task level. Every model call span carries input tokens, output tokens, cached tokens, model and version, and a computed cost. Every span also carries the dimensions you will want to group by later: route or feature, prompt template version, customer tier, and a task ID that ties the whole trace together. Watch cardinality; per-user labels on metrics will destroy your time series database, so keep high-cardinality attributes on traces and keep metrics dimensions coarse.

Full worked answer

Instrument at the span level and aggregate at the task level. Every model call span carries input tokens, output tokens, cached tokens, model and version, and a computed cost. Every span also carries the dimensions you will want to group by later: route or feature, prompt template version, customer tier, and a task ID that ties the whole trace together. Watch cardinality; per-user labels on metrics will destroy your time series database, so keep high-cardinality attributes on traces and keep metrics dimensions coarse.

The headline metric is cost per resolved task, not cost per call. This is the distinction that separates people who have operated these systems from people who have not. A cheaper model that needs three extra tool-calling loops and still escalates to a human is more expensive on every axis, and cost per call says it got cheaper.

What actually creeps, roughly in order: prompt template growth, because every incident adds a paragraph and nobody removes one; context accumulation in multi-turn sessions, where turn 20 costs 10x turn 1; retrieval top-k raised during a recall panic and never lowered; agent step count, which has the fattest tail; and reasoning token budgets, which are billed and invisible to the user.

For latency, track time-to-first-token and total separately, at p50, p95 and p99. Streaming means TTFT is what users feel and total is what your timeouts hit. Also track prompt cache hit rate, since a prompt edit that busts the cache prefix can multiply cost overnight with no visible change.

Alert on distribution shape, not means. The mean step count is stable while a runaway loop burns thousands; p99 step count and a hard max-steps guard catch it. Track truncation and budget-exhaustion rates as first-class failures.

What a good answer covers

  • Instrument at the span level and aggregate at the task level
  • Every model call span carries input tokens, output tokens, cached tokens, model and version, and a…
  • Every span also carries the dimensions you will want to group by later
  • Watch cardinality; per-user labels on metrics will destroy your time series database, so keep…
  • The headline metric is cost per resolved task, not cost per call

Interviewer follow-ups to expect

  1. Your cost per task doubled but every per-call metric is flat. Where do you look?

  2. How would you attribute cost to a specific customer without high-cardinality metrics?

Worked answer from AI-Engineer-Interview-Questions by Om (MIT), reformatted for LLM Prep · answer text last updated 6 November 2026 · scoring and review schedule by LLM Prep