Write the tool loop
Write run_loop(script, registry, max_steps). script is a list of scripted assistant messages (the fake model's replies, one per step). registry maps a tool name to a function that takes the parsed arguments dict and returns a value.
Each step: take the next scripted reply and append it to a list messages. If it has no tool_calls, return (reply["content"], messages). Otherwise, for each call, run the function and append a tool message {"role": "tool", "tool_call_id": <id>, "content": json.dumps(result)}. If max_steps steps pass without a final answer, return (None, messages).
What Check tests
- Returns the final text
- Messages are assistant, tool, assistant
- The tool message has the call's id and the JSON result
- The step limit stops the loop with
None - Several calls in one reply each get a tool message
Stuck? One hint at a time
The solution unlocks after your first Check. Try first: that is where the learning happens.
Output
Press Run to see what your program prints.