Trace your first ADK request from prompt to response
Follow one request through instructions, runtime, model call, events and final response, and read it in the local debug trace.
By Invicly Tech, , in Google ADK with Python for AI Agents. About 11 minutes to read.
In short
One ADK request passes through the runner, which loads the session, builds a model request from the agent's instructions and the history, calls the model and any tools, and saves each step as an event. The final answer is simply the last event that is a complete reply with no pending tool calls. Reading these events in adk web, and replaying them with a fake model, is the fastest way to understand and test an agent.
Think of it like this: Events are like a flight recorder: every message, tool call and answer is written down in order, so you can replay exactly what happened.
Explained simply
When you type a message to an ADK agent, a lot happens before the reply appears. The runner, ADK's engine, loads your session, which is the record of this conversation. It adds your message, builds a request for the model from the agent's instructions and the history, and sends it.
The model can reply in two ways. It can write text, which becomes the answer. Or it can ask to call a tool, such as get_ticket_status with a ticket ID. ADK runs the tool, adds its result to the history and calls the model again so it can write an answer from the result.
Every step is saved as an event. An event records who wrote it, what it contained and any changes it made, such as updates to state. The list of events is the full story of the request. adk web shows this list, and you can click any event to see its details.
Real models give slightly different answers each time and cost money per call. While learning, it helps to swap in a fake model that always returns the same scripted reply. Then you can study the flow of events, write tests and run them offline for free.
Press Play on a diagram to walk through it one step at a time.
- User to Runner: Message: status of T-1001?1Message: status of T-1001?
- Runner to Model: Instructions + history2Instructions + history
- Model to Runner: Function call event3Function call event
- Runner to Tool: get_ticket_status(T-1001)4get_ticket_status(T-1001)
- Tool to Runner: Function response event5Function response event
- Runner to Model: History + tool result6History + tool result
- Model to Runner: Text event: final response7Text event: final response
- Runner to User: Answer shown8Answer shown
- Step 1Function call
author: support, get_ticket_status
- Step 2Function response
Tool output as data
- Step 3Final text
is_final_response() is true
In depth
Writing instructions that steer behaviour
The instruction is the agent's standing guidance. Good instructions state the job, the audience, which tool to use for which kind of question, what to do when data is missing and the format of the answer. They do not try to enforce security; that belongs in code. Keep them short and concrete.
Instructions can include state values with braces. {topic} inserts the value of the topic key from session state and fails if it is missing; {user:tier?} with a question mark inserts it only if present. Use this to give context such as the user's plan without rebuilding the prompt yourself.
- Say when to call each tool
- Say what to do when nothing matches
- Specify the answer format
- {key?} inserts optional state values
- Security rules go in code, not instructions
Following the request through the runtime
The runner is the coordinator for one invocation, which is everything triggered by one user message. It appends your message as an event, then runs the agent. The agent builds an LLM request with the instruction, tool declarations and history and calls the model. Each model response becomes an event. If it contains function calls, ADK runs the tools and records a function response event, then loops back to the model. The loop ends when the model returns text with no further calls.
Each yielded event is first saved to the session by the session service, so the stored history and what you see always match. All events from one invocation share an invocation_id, which makes them easy to group in logs.
- 1Build request
Instruction, tools, history
- 2Call model
Text or function calls
- 3Run tools
Only if calls were requested
- 4Record events
Saved to the session
- Back to step 1
Reading events in the web UI
In adk web, each message you send produces a run of events in the side panel. Click one to see its content and actions: the text, a function call with its arguments, or a function response with the tool's output. The trace view shows timing for the model and tool steps, which helps you spot slow tools. The state view shows session state after each step.
When reading a run, check three things. Did the model call the tool you expected, with the right arguments? Did the tool return what you expected? Did the final answer use the tool result correctly? Most early bugs are one of these three.
- Function call: tool name and arguments
- Function response: what the tool returned
- Final text: what the user sees
- Trace: how long each step took
Finding the final response in code
In code, runner.run_async yields events as they happen. Call event.is_final_response() on each one. It returns true when the event has no function calls, no function responses and is not a partial streaming chunk, or in a few special cases such as a tool that asks to skip summarisation. When several agents take part, each can produce its own final response, so also check event.author when you care which one spoke.
The user's own message is stored in the session but not yielded by default. A request with one tool call therefore yields three events: the function call, the function response and the final text.
Swapping in a fake model
The model parameter of an Agent accepts either a model name or a BaseLlm instance. Subclass BaseLlm, implement the async generator generate_content_async, and yield LlmResponse objects. A scripted fake can look at the last content in the request: if it is a tool result, answer from it; otherwise request the tool. That one class lets you test the whole loop, including tool calls, without a key.
Fakes do not test answer quality; they test plumbing. Later modules add live-model checks for quality. Keep both: fast fakes on every commit, live checks on a schedule.
Learn it in 5 short steps
About 25m each. Track them with a timer in your plan.
- 1Write clear agent instructions~25m
- 2Follow the request through the runtime~25m
- 3Read events in the web UI trace~25m
- 4Find the final response event~25m
- 5Swap in a mock model for repeatable runs~25m
Key points
- Clear instructions say what the agent is for, which tools to use when, and what to do when it does not know.
- Every step, including the user message, tool calls and tool results, is stored as an event in the session.
- event.is_final_response() is true for a complete reply with no function calls, no function responses and no partial text.
- A tool call costs at least two model calls: one to request the tool and one to answer with its result.
- A fake model built on BaseLlm returns scripted replies, so runs are free, offline and repeatable.
Key terms
Tap a card to flip it. 0/6 known
Examples
from google.adk.models import BaseLlm, LlmResponse
from google.genai import types
class FakeLlm(BaseLlm):
model: str = "fake-model"
async def generate_content_async(self, llm_request, stream=False):
reply = types.Part(text="Ticket T-1001 is open.")
yield LlmResponse(content=types.Content(role="model", parts=[reply]))
# root_agent = Agent(name="support", model=FakeLlm(), instruction="...")Pass a BaseLlm instance as model to run without a key or network.
Running an agent from Python and printing events
This script runs the agent with an in-memory runner and prints each event. Expected evidence with a real or scripted tool-using model: a line with the function call, a line with the response, then a line marked final with the answer. The session lives in memory, so nothing needs cleanup.
import asyncio
from google.adk.runners import InMemoryRunner
from google.genai import types
from support_agent.agent import root_agent
async def main():
runner = InMemoryRunner(agent=root_agent, app_name="support_app")
s = await runner.session_service.create_session(app_name="support_app", user_id="user-a")
msg = types.Content(role="user", parts=[types.Part(text="Status of T-1001?")])
async for ev in runner.run_async(user_id="user-a", session_id=s.id, new_message=msg):
print(ev.author, ev.get_function_calls(), ev.is_final_response())
if ev.is_final_response():
print("ANSWER:", ev.content.parts[0].text)
asyncio.run(main())A scripted fake model that calls a tool
This fake asks for get_ticket_status on the first step and answers from the tool result on the second. Use it with an agent that has the tool registered. It proves the loop works end to end: tool requested, tool run, answer written, all without network access.
from google.adk.models import BaseLlm, LlmResponse
from google.genai import types
class ScriptedLlm(BaseLlm):
model: str = "scripted"
async def generate_content_async(self, llm_request, stream=False):
last = llm_request.contents[-1].parts[0]
if last.function_response:
r = last.function_response.response
part = types.Part(text=f"Ticket {r['ticket_id']} is {r['status']}.")
else:
call = types.FunctionCall(name="get_ticket_status", args={"ticket_id": "T-1001"})
part = types.Part(function_call=call)
yield LlmResponse(content=types.Content(role="model", parts=[part]))Quick reference
- One user message = one invocation; its events share an invocation_id
- event.is_final_response(): no function calls, no responses, not partial
- event.get_function_calls() non-empty: the model asked for a tool
- run_async yields agent events; the user message is stored but not yielded by default
- Instruction templating: {key} required, {key?} optional
- Fake model: subclass BaseLlm, implement generate_content_async, yield LlmResponse
Common mistakes
- Showing the first text event to users; with tools, the first model event is usually a function call.
- Vague instructions such as "be helpful" that never say when to call a tool or when to say I do not know.
Quick quiz
Pick an answer to see if you're right and why.
Practice exercises
Try each one yourself first. Use the hint if you get stuck, then compare with the solution.
- Exercise 1easy
Improve this instruction: "You are helpful." for an agent with one tool, get_ticket_status.
- Exercise 2medium
Run the agent once in adk web with a question that needs the tool. Write down the author, type and content of each event you see.
- Exercise 3hard
Write a pytest test that runs the agent with the scripted fake model and asserts the tool was called once and the final answer mentions the ticket ID.
Check yourself
Answer in your head, reveal, then be honest with yourself.
Feel confident about this topic?
Frequently asked questions
- Why does my agent answer without calling the tool?
- Usually the instruction or the tool's docstring does not make clear when to use it. Check the function call events; if there are none, the model chose not to call it.
- Is there a quick way to test without writing a runner loop?
- Yes. InMemoryRunner has an async run_debug method that takes one or more messages and returns the events, which is handy in notebooks and quick scripts.
- Does a fake model replace evaluation?
- No. It checks plumbing such as tool calls and event handling. Answer quality needs evaluation with a real model, covered in a later module.