Skip to content

Courses / Agentic AI / Module 12

Module 12: Observability, cost and step limits: tracing, budgets, loop detection

10 h · theory 3 · hands-on 4 · project 2 · revision 1

You will be able to

  • Trace every step with OpenTelemetry
  • Set budgets per run, user and tenant
  • Detect and stop loops
  • Debug a failed run from its trace

Capstone increment: Tracing, cost budgets and loop detection with a dashboard and alerts.

Job-description skills: Observability and tracing, Cost, latency and reliability · Lab tools: langfuse, arize-phoenix, opentelemetry-sdk

1. Trace every step

When an agent run goes wrong, logs scattered across services don’t tell the story. A trace does: one root span for the run, a child span for every model call and tool call, with inputs, outputs, tokens, cost, latency and errors. OpenTelemetry’s semantic conventions for generative AI define standard attribute names (such as gen_ai.request.model and gen_ai.usage.input_tokens) so any backend can read them.

Record the plan, every replan, every approval and every budget check as spans or events too. A trace you can read top to bottom is the fastest debugging tool you’ll have.

TracePOST /extractSpan: parse120 msSpan: promptextract@v7Span: LLM1.9 s, 2.1k tokSpan: validate1 retryCost₹ per documentLatencyp50 / p95Qualityscores + feedbackVersionsprompt, model, dataOne trace per requestWhat you can answer from traces
Without traces, ‘the model got worse’ is a guess. With them, it’s a query.

2. Langfuse and Phoenix

Langfuse and Arize Phoenix are open-source, self-hostable platforms built for LLM traces: they show nested spans, token and cost totals, sessions, scores from evals and user feedback, and let you turn a production trace into an eval case. Both accept OpenTelemetry data, and most agent frameworks have integrations.

Decide what you store. Traces contain prompts, tool results and sometimes personal data; apply the same redaction and retention rules as your logs, and restrict who can read them.

3. Budgets per run, user and tenant

Set budgets at several levels: per run (tokens, money, steps, wall-clock time), per user per day and per tenant per month. Check before each step, not after the bill arrives. When a run hits its budget, stop cleanly with a partial answer and say why, rather than failing silently.

Alert well before a tenant budget runs out so someone can decide whether to raise it. Report cost per task type: it tells you where optimisation pays.

Common misconception: “We’ll watch the provider dashboard.” It shows totals a day later. You need per-run cost in the trace, right away, to find the one runaway loop.

4. Detect and stop loops

A common failure: the agent has what it needs but keeps calling the same tools, each step resending a longer context. A step limit eventually stops it, but late and expensively. A repeat detector (the same tool with the same arguments seen N times) catches it early; a budget is the backstop.

When the detector fires, try a nudge first (“you already have this result; answer now or explain what is missing”), then stop. Count detector firings per scenario in evals: a rising rate after a change is an early warning.

Stopping a stuck agentA real failure pattern: the agent has what it needs after 7 steps but keeps re-checking. Every step resends a growing context. 0 turns a guard off.

Stopped after 30 steps: ran to the end of the trace · spent ₹218 of ₹218 for the full loop · wasted steps 23

  1. 1. get_alert(checkout) useful ₹0.8
  2. 2. search_logs(checkout, '5xx') useful ₹1.2
  3. 3. get_metrics(checkout, p95) useful ₹1.6
  4. 4. list_deploys(24h) useful ₹2.1
  5. 5. get_deploy(v2.3) useful ₹2.6
  6. 6. diff_config(v2.2, v2.3) useful ₹3.0
  7. 7. read_runbook(db-pool) useful ₹3.4
  8. 8. search_logs(checkout, 'timeout') repeat ₹3.9
  9. 9. get_metrics(checkout, p95) repeat ₹4.3
  10. 10. search_logs(checkout, 'timeout') repeat ₹4.8
  11. 11. get_metrics(checkout, p95) repeat ₹5.3
  12. 12. search_logs(checkout, 'timeout') repeat ₹5.7
  13. 13. get_metrics(checkout, p95) repeat ₹6.2
  14. 14. search_logs(checkout, 'timeout') repeat ₹6.6
  15. 15. get_metrics(checkout, p95) repeat ₹7.0
  16. 16. search_logs(checkout, 'timeout') repeat ₹7.5
  17. 17. get_metrics(checkout, p95) repeat ₹8.0
  18. 18. search_logs(checkout, 'timeout') repeat ₹8.4
  19. 19. get_metrics(checkout, p95) repeat ₹8.8
  20. 20. search_logs(checkout, 'timeout') repeat ₹9.3
  21. 21. get_metrics(checkout, p95) repeat ₹9.8
  22. 22. search_logs(checkout, 'timeout') repeat ₹10.2
  23. 23. get_metrics(checkout, p95) repeat ₹10.7
  24. 24. search_logs(checkout, 'timeout') repeat ₹11.1
  25. 25. get_metrics(checkout, p95) repeat ₹11.5
  26. 26. search_logs(checkout, 'timeout') repeat ₹12.0
  27. 27. get_metrics(checkout, p95) repeat ₹12.4
  28. 28. search_logs(checkout, 'timeout') repeat ₹12.9
  29. 29. get_metrics(checkout, p95) repeat ₹13.4
  30. 30. search_logs(checkout, 'timeout') repeat ₹13.8
Guided experiments
  1. With every guard off, how much does the run cost, and how much of that was wasted? Why does each wasted step cost more than the one before?
  2. Set a step limit of 20. It stops the run, but how many steps were wasted? Why can’t a fixed step limit tell a stuck run from a long, healthy one?
  3. Turn the limit off and stop on 3 repeats. Where does it stop now? What should the agent do when the detector fires: fail, or get a nudge such as “you already have this result; answer now”?
  4. Use all three guards together. Which one would you alert on in production, and which would you just log?

5. Debug a failed run from its trace

Read the trace from the top and find the first step that went wrong. Classify it: a tool error the agent couldn’t recover from, missing information, a bad plan, a wrong tool choice, a model mistake on good inputs, or a guardrail that fired. Each class has a different fix: the tool, the retrieval, the prompt, the model, or the policy.

Turn the run into an eval scenario before fixing it, so the fix is measured and can’t silently regress.

In production: Sample a few successful traces each week too. They show slow, wasteful paths that never trigger an alert.

Hands-on: coding window

The coding window and the full VS Code lab are available after you sign in.

Sign in to code

Labs

Lab 12.1: Self-hosted tracing

Run Langfuse (or Phoenix) with Docker, instrument the capstone with OpenTelemetry or the framework integration, and check that model calls, tool calls, approvals and costs appear in one trace.

Expected: A screenshot of a full incident trace with token and cost totals.

Lab 12.2: Budgets and loop detection

Add per-run, per-user and per-tenant budgets and a repeated-call detector with a nudge, plus alerts at 80% of a tenant budget.

Expected: A looping scenario stops early with a clear message; the alert fires in a test.

Lab 12.3: Trace debugging drill (capstone increment)

Take three failed runs, classify the first wrong step of each from its trace, turn each into an eval scenario, then fix and re-run.

Expected: Three new scenarios in the suite and the fixes they verified.

Go deeper

Videos

Further reading

  • Docs Langfuse documentation · Langfuse. Traces, sessions, prompt versions and evaluation scores in an open-source tool you can self-host.
  • Docs Arize Phoenix documentation · Arize. OpenTelemetry-based tracing and evals; a good second tool to compare with Langfuse.
  • Article Agents · Chip Huyen. Tools, planning, reflection and failure modes of agents, with an engineer's view of the trade-offs.

GitHub repositories

Checkpoint quiz

1. What should one agent run produce in tracing?
2. Why check budgets before each step?
3. Which guard catches a stuck agent earliest?
4. First step when debugging a failed run?
5. Traces contain prompts and tool results. What follows?

Interview questions

  1. What would you put in a trace for an agent run, and why?
  2. How do you stop an agent from running up a large bill?
  3. Your agent loops between two tools. How do you detect and handle it?
  4. Walk me through debugging a failed agent run from its trace.
  5. How do you choose between Langfuse, Phoenix and a general APM tool?

Ask the professor

Sign in to ask questions about this module.

Discussion

Community discussion appears here once giscus is configured (see README).