Lesson 1 / 25
The Structure of a Turn
Break a turn into call, inspect, act and append, and name what each step can go wrong with.
Four steps, every time
Every turn does the same four things: call the model with the messages so far, inspect why it stopped, act by running any tools it asked for, and append the assistant message and the tool results. Then the loop starts again. If you can name these four steps, you can place any guard exactly where it belongs.
One turn of the loop
Each turn calls the model, checks why it stopped, runs any requested tools and appends the results.
The skeleton
call_model, run_tools and final_text are placeholders for your own code. Notice the loop has no way to end except a clean stop or the for limit.
def run(messages, max_steps=10):
for step in range(max_steps):
reply = call_model(messages) # 1. call
messages.append({"role": "assistant", "content": reply.content})
if reply.stop_reason != "tool_use": # 2. inspect
return final_text(reply)
results = run_tools(reply) # 3. act
messages.append({"role": "user", "content": results}) # 4. append
raise RuntimeError("step limit reached")Put guards between steps
Checks for budget, time and repetition belong right after "inspect" and before "act". That way a runaway loop is stopped before it spends money on another tool call.
Quick check: Which step of a turn runs the tools the model asked for?
- Call
- Act
- Append
- Inspect
Answer
Act — Call asks the model, inspect reads its stop reason, act executes the tools, and append records results.