Agents, Tool Use & MCP·Question 38 of 90Intermediate

Your agent platform's bill jumped from $8k to $40k in a month. Nobody knows why. How do you find out, and how do you make sure this never happens blind again?

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

The honest first answer: if I did not instrument per-call attribution, I am reconstructing from provider invoices and guessing. Then I fix that permanently.

Full worked answer

The honest first answer: if I did not instrument per-call attribution, I am reconstructing from provider invoices and guessing. Then I fix that permanently.

Finding it now. Provider dashboards give totals by API key and model at best. So I slice what I have by model, by day, and by cache-hit ratio. A jump with flat request volume points at tokens per request: context growth, a cache regression, a prompt change, reasoning effort turned up. A jump with rising request count points at loop behaviour: retries, agents failing to terminate, a tool erroring and triggering re-attempts. The signature of a runaway loop is high steps-per-task with a flat success rate.

The instrumentation that should have existed. Every LLM and tool call is a span carrying tenant_id, run_id, agent_version, step_index, model, input tokens split into cached and uncached, output tokens including reasoning tokens, and computed cost. Cost goes on the span at write time from a price table, because reconstructing it later from token counts and changing prices is misery.

Then the aggregations that matter:

  • Cost per successful task, segmented by task type. Not cost per call. An agent costing 3x that succeeds instead of failing is cheaper.
  • Cost per tenant. In any multi-tenant system a few tenants generate most of the spend, and some are unprofitable at your pricing.
  • The p99 of cost per run. The mean hides everything. Your $40k is probably a handful of runs that spiralled.

The controls. A per-run hard budget that terminates the loop. Per-tenant daily quotas. Alert on cost-per-task leaving a band rather than on total spend, since total spend rising with usage is fine and cost-per-task rising is a regression. Tag spans with prompt and agent version so you can attribute a cost delta to a specific deploy.

The cultural point: cost is a product metric the team owns, not a finance surprise.

What a good answer covers

  • The honest first answer: if I did not instrument per-call attribution, I am reconstructing from provider…
  • Provider dashboards give totals by API key and model at best
  • So I slice what I have by model, by day, and by cache-hit ratio
  • A jump with flat request volume points at tokens per request
  • A jump with rising request count points at loop behaviour

Key terms: Finding it now · Cost per successful task · Cost per tenant · The controls

Interviewer follow-ups to expect

  1. One tenant is 60% of your spend and 2% of revenue. What do you do technically?

  2. How do you attribute the cost of a shared prompt cache across tenants?

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