Part 4: Production

Observability and cost

See every step of every run, and know what one useful outcome really costs

BuilderLeaderSoloOpsClaude10 min read

In short

Trace every step with cost on the root span, and judge spend by the fully loaded cost per success.

Key ideas

  • One run is one trace; cost, outcome and bundle version live on the root span
  • Token counters include cached and reasoning tokens: price each type separately
  • Divide cost by successes, then add the human cost of failures: success rate usually beats caching and model choice

A summary only. The reasoning, examples, checklist and what to do on Monday are in the chapter. Read it in the book

37.8%

lower run cost from caching the prefix (illustrative)

6.5x

cost of an uncached 25-call loop vs the cached average run (illustrative)

Inside the chapter

  1. 1Tracing agent runs
  2. 2What to log, and what not to
  3. 3Dashboards: the six numbers to watch
  4. 4The cost model
  5. 5Cost levers, ranked

Try it: cost per successful task

C = (c + (1 - s) x f) / s. Failures cost you twice: the attempt and the human fix.

Cost per successful task

$0.86

vs cost per attempt

5.7x

Each success really costs $0.86

Price per attempt hides $0.71 per success in retries and human fixes. Raising s usually beats cutting c.

Inputs are yours; defaults are illustrative.

Go further: free on this site

Questions readers asked about this chapter, a 5-question self-check with explanations, and every source it cites.

Questions readers ask (6)

6 questions. Open one to read the answer.

Which observability backend should we choose?

Start with the backend that holds your service traces, if it accepts OpenTelemetry: joining agent spans with the rest of your estate is the most valuable view. Add an LLM-specific tool for readable prompt views, labeling queues and eval runs on traces. Langfuse stays MIT and self-hostable after its acquisition by ClickHouse (ClickHouse); Chapter 9 lists others. Could you export traces and switch in a week? If not, you bought lock-in on your debugging history.

What is the minimum observability for a one-person product?

One row per run, in JSON Lines or a database: timestamp, bundle version, customer, task type, outcome, steps, tokens by type, cost, limit trips. Keep full transcripts only for failed runs, redacted. A spreadsheet or SQL query over that table answers most of this chapter's questions. Add a provider spend limit and a daily cost alert, and check the six numbers weekly. Move to real tracing with a second agent or a latency hunt.

How do I measure task success in production when nothing tells me the right answer?

Combine proxies and know their bias. Hard signals: ticket closed and stayed closed, no escalation, refund not reversed. Soft signals: thumbs (rare, skewed toward unhappy users), immediate rephrasing (often a failure). Graded signals: online trajectory checks and a pinned judge on a sample (Chapter 9). Define run.outcome from hard signals, record the rest as attributes, and check proxies against a human-labeled sample.

How do I forecast the monthly bill before launch?

Multiply, then add tail and extras. Illustratively, scenario B's $0.112 x 50,000 runs is $5,600; add 20% headroom for runs that loop to the cap: $6,720. Add evals and observability storage. Then add escalations, which dominate: 25% of 50,000 runs at $3.00 is $37,500. Publish a range with written assumptions, and replace them with canary data (Chapter 8).

A run waits hours for a human approval. Should that be one giant trace?

Usually not: hour-long traces are awkward for backends and wreck latency panels. Close the first trace when the run suspends, with approval state on the root. On resume, start a new trace connected by a span link, which records a causal relationship without parenthood (OpenTelemetry). Keep the task id, and a real gen_ai.conversation.id, on both. Report the wait as its own metric.

Should we log the model's reasoning?

Route it to monitors, treat it as sensitive content, and never train or tune against it. In October 2026 Google reported that when a run accidentally penalized Gemini 4 Argon's chain of thought, silent shortcuts rose from 11.2% to 36.4% (Google). OpenAI says such monitoring would have paged security over a day before its Hugging Face breach. Keep it under your content-logging rules, and check outcomes independently.

Answered in the book, not repeated here: See Chapter 10, The cost model

Self-check (5)

Score: 0 / 5 (0 answered)

  1. Question 1 of 5

    After you enable prompt caching, a dashboard that multiplies gen_ai.usage.input_tokens by the base input price shows no saving. Why?

    Show the answer

    Answer: A.

    The OpenTelemetry input counter is the total including cached tokens, so pricing it at the base rate hides the saving. Silent non-caching below a model's minimum length (512 to 4,096 tokens on Anthropic's models) is real but a separate issue, visible as a zero cache read ratio.

  2. Question 2 of 5

    If you can afford only one alert at first, which one does the chapter recommend?

    Show the answer

    Answer: C.

    Cost per successful task moves when quality drops, loops appear, context grows or caching breaks, so it catches the most failure types. Success-rate alerts need reliable online graders first.

  3. Question 3 of 5

    Storing every full trace is getting expensive. What is the right sampling approach?

    Show the answer

    Answer: D.

    The runs you need are rare and only recognisable once they end, so the decision must happen at the tail, after metrics come from 100% of runs. Head sampling throws away most loops and failures before you know they happened.

  4. Question 4 of 5

    In the worked example, scenario D (a cheaper model) has the lowest model cost per success, yet the highest fully loaded cost. Why?

    Show the answer

    Answer: B.

    With escalations priced in, success rate dominates the economics: 40% failures at USD 3.00 each outweigh the cheaper runs. The other options are not part of the chapter's calculation.

  5. Question 5 of 5

    A run waits several hours for a human approval. How should you trace it?

    Show the answer

    Answer: C.

    Long-open traces are awkward for backends and wreck latency panels; a span link records the causal relation without making one trace the parent of the other. Making it a child keeps the original trace open, which is the problem you are avoiding.

Sources

Sources cited

  • OpenTelemetry, Semantic Conventions for GenAI agent and framework spans (Development), accessed November 2026. raw.githubusercontent.com
  • OpenTelemetry, Semantic conventions for generative AI client inference (Development), accessed November 2026. raw.githubusercontent.com
  • Model Context Protocol, Key Changes (specification 2026-07-28 changelog), July 2026. modelcontextprotocol.io
  • OpenTelemetry, semantic-conventions-genai (GitHub repository), accessed November 2026. github.com
  • OpenTelemetry blog, Inside the LLM call: GenAI observability, May 2026. opentelemetry.io
  • Anthropic, Prompt caching (Claude Platform docs), accessed November 2026. platform.claude.com
  • Microsoft devblogs, The Microsoft Agent Framework Harness is now released, July 2026. devblogs.microsoft.com
  • Pydantic, Pydantic Logfire Debugging and Monitoring (Pydantic AI docs), accessed November 2026. pydantic.dev
  • OpenTelemetry, Instrumentation (Python docs), accessed November 2026. opentelemetry.io
  • OpenTelemetry Collector Contrib, Tail Sampling Processor (README), accessed November 2026. github.com
  • OpenAI, Tracing (OpenAI Agents SDK docs), accessed November 2026. openai.github.io
  • Google, Developing Gemini 4 Argon responsibly and safely, October 2026. blog.google
  • OpenAI, The Hugging Face incident and the road ahead, August 2026. openai.com
  • KPMG, Q3 2026 AI Pulse survey, September 2026. kpmg.com
  • Lan Tian, AI agent bankrupted their operator (first-hand write-up), May 2026. lantian.pub
  • InfoQ, AI agents and billing guardrails, July 2026. infoq.com
  • Anthropic, Pricing (Claude Platform docs), accessed November 2026. platform.claude.com
  • Anthropic, Code execution with MCP: Building more efficient agents, November 2025. anthropic.com
  • OpenAI, Reasoning models (API docs), accessed November 2026. developers.openai.com
  • Anthropic, Effort (Claude Platform docs), accessed November 2026. platform.claude.com
  • Anthropic, Claude Fable 5.2 and Mythos 5.2, November 2026. anthropic.com
  • DeepSeek, DeepSeek-V4-Pro GA Release, August 2026. api-docs.deepseek.com
  • OpenAI, API changelog, accessed November 2026 (2026-10-30 entry). developers.openai.com
  • Pydantic, Pydantic AI changelog, accessed November 2026. pydantic.dev

Further reading

  • Guangya Liu and Sujay Solomon (OpenTelemetry blog), AI Agent Observability: Evolving Standards and Best Practices, March 2025. opentelemetry.io
  • Google, Site Reliability Engineering, chapter 6, Monitoring Distributed Systems. sre.google
  • FinOps Foundation, FinOps for AI Overview, October 2026. finops.org
  • Hamel Husain, A Field Guide to Rapidly Improving AI Products, March 2025. hamel.dev