Back to paths

Observing and troubleshooting agents in production

Agents fail in their own ways. They loop, wait on slow tools, drift and hallucinate. Read a trace to find which failure you have, match each symptom to the signal that confirms it, and choose the fix that addresses the cause.

advanced~30 min#agentic-ai#agents#observability#operations
Chapter 5Evaluate and deployLesson 5 of 5Show

Prerequisites: What good looks like for an agent


You cannot fix what you cannot see

A user complains that the travel agent took almost fourteen seconds to move a flight. Your uptime dashboard is green, and the error rate is flat. Nothing in the usual service metrics says what went wrong, because the failure is inside the agent's loop: what it asked the model, which tools it called, how many times and how long each took.

Agents fail in ways ordinary services do not. They loop, calling the same tool again and again. They wait on a slow tool. They , getting worse as the model, the prompts or the data change. They hallucinate, stating things no tool returned. And they cost more than expected, because every turn of the loop sends tokens. Each of these leaves a different mark, and you find it in a trace.

Traces, metrics and logs

When telemetry is enabled, ADK records three kinds, using the OpenTelemetry conventions for generative AI, and can export them to Google Cloud (Cloud Trace, Cloud Monitoring and Cloud Logging) or to any OpenTelemetry backend. (Support varies by language: Python emits all three; Kotlin emits traces and metrics but no log records; the metrics docs do not cover Go.)

  • A trace follows one request. It is a tree of : an invoke_agent span for the whole turn, with child spans for each model call (generate_content) and each tool call (execute_tool). The length of each span shows where the time went.
  • Metrics aggregate across many requests: turn duration, tool duration per tool, model calls and tool calls per turn, and token usage per model call. They show trends and outliers that a single trace cannot.
  • Logs record what happened in detail. By default ADK leaves prompt content out of its logs, for security; you can turn content capture on (for logs, and optionally for spans) when you need it, at the cost of storing what users said. Spans are redacted too: when you deploy to Agent Runtime with --otel_to_cloud, ADK sets ADK_CAPTURE_MESSAGE_CONTENT_IN_SPANS to false, so span payloads, such as a model request or a tool's result, show as {} or N/A until you turn capture on.

Use metrics to see that something changed, traces to see where in the loop, and logs for the detail.

Reading a trace

Here is the trace of that slow turn.

Predict which single fix gets closest to the 4 second target. Then click the spans you blame, pick a fix for each, and watch the turn total recompute.

Simulation: trace-waterfall-explorer

Read it in two passes.

  1. Find the longest spans. get_fare_rules takes 6.6 of the 13.6 seconds. That is a slow downstream API, not the agent's reasoning. Making the model faster would barely move the total.
  2. Look for repeats. search_flights runs four times with the same arguments, each followed by a model call. The model is not learning anything new between calls: it is a . The three repeat searches and the model calls that re-issued them add about 3.6 seconds and three extra model calls.

Each finding has its own fix, below. Neither shows up in an uptime check.

In a trace, an agent calls get_weather(city='Lyon') five times in a row, and each call takes 0.2 s. What is the problem?

Symptom, signal, fix

SymptomLikely causeSignal that confirms itFix
Slow turnsA slow tool or downstream API; too many model callsLong execute_tool spans; tool-duration metrics by tool; model calls per turnSpeed up or cache the tool, call independent tools in parallel, use a faster model for simple steps
Reasoning loopThe model cannot tell it is done: an unclear tool result, a vague goalThe same call repeated with the same arguments; high tool-call and model-call counts per turnClear tool descriptions and outputs, instructions on when to stop, and a cap on model calls per run
DriftA new model version, a changed prompt or tool, changed data or usersFalling quality scores on sampled live responses; new failures when the offline dataset is re-run on a new versionScoring live responses, re-running the offline dataset on every change, pinned model versions, and failures fed back into the dataset
ErrorsA failing tool, quota limits, bad permissionsError type on spans; error-rate metrics by tool and model; logsFix the tool or permission, handle errors, retry with backoff where safe
HallucinationWeak retrieval, or a model answering beyond its tool resultsA claim no tool or retrieval span returnedBetter retrieval, instructions to answer from tool results, a groundedness check
Rising costLong context, extra turns, a larger model than neededToken usage per model call and per turn; model calls per turnTrim or summarise history, cut loops, a smaller model where it is good enough

Two rules sit behind this table.

A timeout is not a loop fix. If the agent repeats the same call, more time buys more repeats, more tokens and a later failure. Fix why it repeats: make the tool's result unambiguous (an empty search should say "no flights found", not return nothing), tell the model when to stop, and keep a hard cap as a safety net. ADK's RunConfig has max_llm_calls, which caps the model calls in one run (500 by default), so a runaway loop ends instead of running up a bill.

Drift needs evaluation, not uptime checks. A drifting agent stays up and fast; only its answers get worse. You catch it by scoring answers, not by checking that the service responds. Two habits do that. First, score a sample of live responses with the quality checks from the agent quality lesson (correct, grounded, safe), so a fall in quality shows up as a falling score. Second, re-run your offline evaluation dataset on every model, prompt or tool change, so a change that breaks known cases is caught before release. A pinned model version keeps the model from changing under you between evaluations.

Why keep a cap on model calls per run if you have fixed the tool descriptions?

Fixing the slow turn

Back to the trace. Two fixes, one for each finding:

  • The loop (A). The traces show search_flights returning an empty list when no Friday flight matches the fare class, and the model searching again in the hope of a different answer. Change the tool to return an explicit result ("no flights match; try another fare class or date") and add an instruction to ask the user when a search finds nothing. Keep the run cap.
  • The slow tool (B). get_fare_rules calls a slow partner API. Fare rules change rarely, so cache them for each fare class, and fetch them in parallel with the search rather than after it (instruct the model to request both tools in one response, or have the search tool fetch the rules itself).

Then confirm the fix: re-run the offline evaluation dataset (a new case for "no matching flights" belongs there now), and watch the tool-duration and calls-per-turn metrics after release.

Recap

  • Trace every model and tool call. A trace shows where the time went and what repeated; metrics show trends; logs give detail.
  • Read a trace in two passes: the longest spans, then repeated calls.
  • Fix loops with clearer tool results, stop instructions and a call cap, not a longer timeout.
  • Catch drift by scoring answers, live and offline; uptime checks stay green while quality falls.

Sources

Show 5 sourcesHide sources
SourcePublisherWhat it supports
Agent activity traces (opens in a new tab)github.comGoogle, Agent Development Kit documentationretrieved 2026-11-17: OpenTelemetry GenAI conventions; invoke_agent, execute_tool and generate_content spans; export to Cloud Trace
Agent activity metrics (opens in a new tab)github.comGoogle, Agent Development Kit documentationretrieved 2026-11-17: agent and tool duration, inference and tool calls per invocation, token usage per model call
Agent activity logging (opens in a new tab)github.comGoogle, Agent Development Kit documentationretrieved 2026-11-17: prompt content is elided by default for security and can be captured on request
Google Cloud Trace observability for ADK (opens in a new tab)adk.devGoogle, Agent Development Kit documentationretrieved 2026-12-02: when deploying with adk deploy agent_engine --otel_to_cloud, ADK sets ADK_CAPTURE_MESSAGE_CONTENT_IN_SPANS='false' unless already defined; payload attributes, including tool_response, are then replaced with placeholders such as an empty object or "N/A"; set it to 'true' to capture full payloads
Runtime configuration (opens in a new tab)github.comGoogle, Agent Development Kit documentationretrieved 2026-11-17: max_llm_calls caps the total number of LLM calls per run, default 500

Suggested videos

Show 2 videosHide videos

Review

Flashcards1 / 4

What is a span in an agent trace?

Check yourself

Quick exam1 / 5 · 0 correct

A 9-second turn has these child spans: three model calls of 0.8 s each, lookup_customer at 0.3 s, and get_invoice_pdf at 6.2 s. Where should you look first to cut latency?

Your notes on this lesson