The loop is driven by stop_reason
Confirmed in official docsPlain languageAn agent works in a loop: you send a message, the model replies, you carry out whatever it asked for, and you send the result back. The API tells you what to do next through a field called stop_reason. As long as it says the model wants to use a tool, you run that tool and loop again. Once it says the model is finished, you stop. A hard cap on the number of turns is just a safety net in case something goes wrong — it isn't how the loop is supposed to normally end.
Technicalstop_reason == 'tool_use' means the model wants to call a tool — execute it, append the tool_result, and continue the loop. stop_reason == 'end_turn' (or another terminal value) means the model is done and the loop should exit. A max-turns cap is a safety backstop, not the primary control signal.
Why it matters
Treating a turn cap as the main stop condition (rather than a backstop) will either cut the agent off mid-task or let it run needlessly long when it already signaled completion via stop_reason.
The model is stateless — you carry the memory
Confirmed in official docsPlain languageThe model remembers nothing between one request and the next except whatever conversation history you resend it. If an agent seems to 'forget' something it was told earlier, the near-certain explanation is that those earlier messages simply weren't included in the latest request — there's no hidden memory to go configure somewhere.
TechnicalThe API is stateless: the model has access only to the message history included in the current request. Apparent memory loss traces to conversation history not being resent, not to a missing 'remember this' instruction or a memory-length setting.
Why it matters
This reframes a whole category of 'the agent forgot' bugs as a plumbing problem (what's actually being sent in the request) rather than a prompting problem (what the agent was told to remember).
Guarantees live outside the model
Confirmed in official docsPlain languageBecause the model is inherently probabilistic, any rule that absolutely must hold has to be enforced by the code around it, not by asking nicely. Want every conversation to end in either a resolution or a clean handoff to a person? Wrap the loop in code that checks the final state and force-escalates if neither happened. Want refunds over a certain amount to always go to a human? Use a hook that intercepts that specific tool call. Want a particular tool to always run first? Force it directly rather than hoping the model picks it.
TechnicalHard guarantees are enforced in orchestration code, not the model's reasoning: wrap the loop in code that inspects the final state and force-escalates when the outcome isn't 'resolved or escalated'; use a hook to intercept and gate compliance-critical tool calls (e.g., refunds above a threshold); force a required first step via tool_choice rather than relying on the model to sequence correctly.
Why it matters
This is the Module 4 restatement of the guarantee-vs-influence axis, specifically applied to agentic loops and API-level guarantees rather than Claude Code hooks.
Reasoning over results, and escalating well
Per study guidePlain languageBetween tool calls, the agent looks at what came back and decides what to do next — it's not following a rigid, pre-drawn decision tree. When it does need to hand a problem to a person, it should package up a clear summary (who the customer is, what the order was, what the issue is) so the human can act immediately instead of re-investigating from scratch. And it should escalate for real reasons — the customer explicitly asked, the situation needs authority the agent doesn't have, or it's genuinely stuck — not because of a crude proxy like a sentiment score or a fixed number of failures.
TechnicalTool results are appended to the conversation and the model reasons over them to select the next action, rather than following a hard-coded decision tree. Escalations should include a structured handoff summary (customer, order, issue) and should trigger on genuine judgment-based conditions (explicit request, policy exception, real impasse) rather than rigid proxies like sentiment thresholds or fixed failure counts.
Why it matters
Proxies like 'three failed calls' or a sentiment score both over- and under-escalate relative to what actually warrants a human — judgment-based criteria track the real thing you care about.