# The Structure of a Turn — Agent Loops, Stop Conditions and Token Budgets

Source: https://www.geekswithgeeks.com/en/agent-loops/loop-turn-structure

> 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.

![Four stages in every turn: call, inspect, act, append.](assets/figures/agent-loops/section-1-map.svg) — Figure 1.1 — Call, inspect, act and append.

## 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.

```python
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.

**Quiz:** Which step of a turn runs the tools the model asked for?

- [ ] Call
- [x] Act
- [ ] Append
- [ ] Inspect

*Answer:* Act. Call asks the model, inspect reads its stop reason, act executes the tools, and append records results.
