AI Agents
What is stop_reason?
The field in an LLM API response that tells you why the model stopped — tool_use means run a tool and continue; end_turn or max_tokens means the turn is over.
How it works
Your agent loop branches on this value. Ignore it and you either spin forever or cut off mid-task. Handling both paths deliberately is one of the decisions that separate a working agent from a broken one.
Example
In practiceAfter the model returns stop_reason='tool_use' you execute the requested tools; when it returns 'end_turn' you return the final text answer.
What happens if I ignore stop_reason?
The loop either never terminates or exits at the wrong time. Always branch on it explicitly.
Go deeper: Deploy Your First AI Agent
The complete, step-by-step guide — every command, config, and the reasoning behind it.
Get the guide