One full round trip
The scripted fake model stands in for the API. Each step prints what a real log of this loop would show.
The tool loop is: send the conversation and the tools, run any tool calls the model returns, append the results as tool messages, and ask again until the model answers in plain text. A step limit stops a model that never finishes.
Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.
Ready for more? This phase has 6 hands-on exercises with automatic checks.
Practice questions
Write the code in the editor, run it, then open the model answer to compare.
Write run_loop(script, registry, max_steps) that returns the final text of the first scripted message without tool_calls, running each call with registry[name](args), and returns None if max_steps is reached. Messages in script use the real shape.
Write count_steps(messages) that returns how many assistant messages a message list contains.
Explain it without notes
Describe the tool-call loop step by step.
Why must the assistant message be appended before the tool message?
Why does every tool loop need a step limit, and what should happen when it is reached?
Expected output
step 1: model asks get_weather {"city":"Pune"} (id call_1)
step 1: tool result {"city":"Pune","temp_c":31,"sky":"sunny"}
step 2: model answers: It is 31 C and sunny in Pune.
roles in the conversation: user, assistant, tool, assistant