Skip to content

Twenty lines make an agent

Ask, run, return, repeat: the code, running.

An agent is that ask-run-return cycle in a loop, until the model answers instead of asking. Here's the code, stepping through the real run.

agent.pylen(messages) = 1
1messages = [{"role": "user", "content": task}]
2while True:
3 reply = client.messages.create(messages=messages, tools=TOOLS)
4 messages.append({"role": "assistant", "content": reply.content})
5 if reply.stop_reason == "end_turn":
6 break
7 results = []
8 for block in reply.content:
9 if block.type == "tool_use":
10 output = call_tool(block.name, block.input)
11 results.append({"type": "tool_result", "tool_use_id": block.id, "content": output})
12 messages.append({"role": "user", "content": results})
13print(text_of(reply))
Where is my order?gemini-3-flash-previewRecorded real run
model calls
1
tool calls
0
tokens in
170
tokens out
21
time
1.9s

Task My order A123 hasn't arrived yet. Where is it?

You
task
Model1.9s
get_order
track_shipment

Model call 1. It read 170 tokens and wrote 21. It asked for a tool, so it isn't done.

get_order({"order_id":"A123"})

stop_reason: tool_use

Context window170 tokens sent

  • System prompt34
  • Tool definitions124
  • Your messages12
step 1 of 5

What tells the loop to stop?