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.
Chapter 5Evaluate and deployLesson 5 of 5ShowHide
Chapter 5 of 7 in Google Cloud Professional Agentic Architect
- Observing and troubleshooting agents in productionYou're here
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_agentspan 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 setsADK_CAPTURE_MESSAGE_CONTENT_IN_SPANSto false, so span payloads, such as a model request or a tool's result, show as{}orN/Auntil 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.
Read it in two passes.
- Find the longest spans.
get_fare_rulestakes 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. - Look for repeats.
search_flightsruns 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
| Symptom | Likely cause | Signal that confirms it | Fix |
|---|---|---|---|
| Slow turns | A slow tool or downstream API; too many model calls | Long execute_tool spans; tool-duration metrics by tool; model calls per turn | Speed up or cache the tool, call independent tools in parallel, use a faster model for simple steps |
| Reasoning loop | The model cannot tell it is done: an unclear tool result, a vague goal | The same call repeated with the same arguments; high tool-call and model-call counts per turn | Clear tool descriptions and outputs, instructions on when to stop, and a cap on model calls per run |
| Drift | A new model version, a changed prompt or tool, changed data or users | Falling quality scores on sampled live responses; new failures when the offline dataset is re-run on a new version | Scoring live responses, re-running the offline dataset on every change, pinned model versions, and failures fed back into the dataset |
| Errors | A failing tool, quota limits, bad permissions | Error type on spans; error-rate metrics by tool and model; logs | Fix the tool or permission, handle errors, retry with backoff where safe |
| Hallucination | Weak retrieval, or a model answering beyond its tool results | A claim no tool or retrieval span returned | Better retrieval, instructions to answer from tool results, a groundedness check |
| Rising cost | Long context, extra turns, a larger model than needed | Token usage per model call and per turn; model calls per turn | Trim 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_flightsreturning 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_rulescalls 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
| Source | Publisher | What it supports |
|---|---|---|
| Agent activity traces (opens in a new tab) | Google, Agent Development Kit documentation | retrieved 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) | Google, Agent Development Kit documentation | retrieved 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) | Google, Agent Development Kit documentation | retrieved 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) | Google, Agent Development Kit documentation | retrieved 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) | Google, Agent Development Kit documentation | retrieved 2026-11-17: max_llm_calls caps the total number of LLM calls per run, default 500 |
Suggested videos
Show 2 videosHide videos
Debugging agents at scale (opens YouTube in a new tab)
Next '26 demo of debugging a misbehaving agent with Google Cloud AI Observability, going from symptom to signal to fix.
Note: A short demo with no trace-waterfall walkthrough.
Why Your AI Agent Fails in Production (And How to Catch It) (opens YouTube in a new tab)
Uses agent traces to measure quality, latency and token cost, matching the lesson's traces/metrics/logs section.
Note: The demo agent is built with LangGraph, not ADK.
Review
What is a span in an agent trace?
Check yourself
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?