Writing the Agent loop
In the previous section, the model handed over a slip that said "please compute 3829 plus 9471". In this section, we execute the slip, hand the result back, and make this repeat automatically until the task is complete. That's an Agent.
The loop has only four steps
- 1?
- 2?
- 3?
- 4?
Click them one by one, in order.
Code
Download step2_loop.py. The core is the run function:
def run(question, max_steps=8):
messages = [{"role": "user", "content": question}]
for step in range(1, max_steps + 1):
resp = client.chat.completions.create(model=MODEL, messages=messages, tools=tools)
msg = resp.choices[0].message
messages.append(msg.model_dump(exclude_none=True)) # 模型这一轮说的话也要放回对话
if not msg.tool_calls: # 不再调工具:这就是最终回答
return msg.content
for call in msg.tool_calls:
try:
args = json.loads(call.function.arguments)
result = FUNCTIONS[call.function.name](**args)
except Exception as e: # 出错也要告诉模型,让它自己改
result = f"出错了:{e}"
print(f"[第 {step} 步] {call.function.name}({args}) = {result}")
messages.append({"role": "tool", "tool_call_id": call.id, "content": str(result)})
return "超过最大步数,停下。"The tools are two ordinary Python functions, add and multiply, looked up by name using a dictionary FUNCTIONS.
Ask it a two-step question:
python step2_loop.pyYou should see (on a local qwen3:8b, 4 seconds):
[Step 1] multiply({'a': 24, 'b': 17}) = 408
[Step 1] add({'a': 408, 'b': 9}) = 417
There are 417 apples in total. The calculation steps are as follows:
...
Final answer: 417Four details that are easy to miss
**1. The model's own turn must also be put back into messages. ** Line 6 messages.append(msg...) cannot be omitted. In the next round, the model needs to see "what I requested last time" to match the subsequent tool messages correctly.
**2. tool_call_id must match. ** Every tool message must carry the id of the corresponding call, so the model knows which result belongs to which request.
**3. There must be a maximum step count. ** The model may fall into an endless loop of repeatedly calling tools. max_steps is the fuse—stop once it's reached.
**4. Don't let the program crash on errors—hand the error to the model. ** Wrong parameters, missing files... all get sent back as a single "Error: ..." message, and the model can often correct itself. In the real run shown on the course homepage, the model wrote the wrong path; after receiving the error, it first listed the directory and then re-read with the correct path—that was thanks to this line.
An interesting phenomenon: two tools called in one step
Note that both lines above say "Step 1": qwen3:8b requested multiply and add simultaneously in the same turn, and the parameter 408 of add was computed by itself in advance, without waiting for the multiplication result to come back.
This is called parallel tool calling: multiple tool_calls returned in one turn. Our loop executes them one by one with for call in msg.tool_calls, so it supports this naturally.
For the same question, the cloud GLM model directly gave 24 × 17 = 408 and 408 + 9 = 417 in its answer. Different models vary in how eagerly they use tools. To make it definitely use tools, you can explicitly require it in the prompt, or set tool_choice in the API (note: Ollama's OpenAI-compatible interface does not support this parameter, so local models can only rely on prompting).
Quiz timeWhat could happen if you remove max_steps?
Check yourself
- I got
step2_loop.pyrunning and saw the tool calls and the final answer - I can state the four steps of the loop
- I know why we put the model's own messages back, match the tool_call_id, set a maximum step count, and hand errors back
Next section: Give it real tools.