Session 8· 01· 15 min
Messages in the Agent Loop
What you'll learn
- ▸Inspect the full message flow: Human → AI(tool_calls) → Tool → AI(content)
- ▸Understand what each message type carries
- ▸Read tool_call_id to trace which tool answered which request
What you will build
A product-lookup agent that you instrument to print every message in the trace. This teaches you to read the agent's "thought process" — critical for debugging.
01_messages.py (trace loop)
for i, msg in enumerate(result["messages"]):
msg_type = type(msg).__name__ ①
if hasattr(msg, "tool_calls") and msg.tool_calls: ②
for tc in msg.tool_calls:
print(f" tool_call: {tc['name']}({tc['args']})")
if hasattr(msg, "tool_call_id"): ③
print(f" tool_call_id: {msg.tool_call_id}")
if hasattr(msg, "content") and msg.content:
print(f" content: {msg.content[:200]}")①HumanMessage, AIMessage, ToolMessage — each is a Python class.
②AIMessage carries tool_calls when the model wants to invoke tools.
③ToolMessage carries tool_call_id linking it back to the specific request.
$ python 01_messages.py
Code Check
Which message type carries the tool_call_id?
Recap — what you just learned
- ✓Agent traces have 4 message types: System, Human, AI, Tool
- ✓AI messages carry tool_calls; Tool messages carry tool_call_id
- ✓Walking the trace is the #1 debugging technique for agents