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 break7 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))
- 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 window
170 tokens sent
- Your messages12
- Tool definitions124
- System prompt34
0.02% of the 10,48,576-token window. Every call sends all of it again.
Context window170 tokens sent
- System prompt34
- Tool definitions124
- Your messages12
What tells the loop to stop?