The Core Loop
request to stop_reason to tool execution to history append.
Why a Loop at All?
A single call to Claude returns one response. But real agents need to act: look something up, run a tool, then keep going. The agentic loop is the engine that makes this happen.
The model itself is stateless — it keeps no memory between calls. Your code holds the conversation and decides when to keep going and when to stop. Master this loop and you have mastered the foundation every Claude agent is built on.
In this lesson you will trace one full turn: request → stop_reason → tool execution → history append, and repeat.
The Request: Full History Every Turn
Because the model keeps no state, you must send the entire conversation history on every request. The key fields of a Messages API request:
model— which Claude modelmax_tokens— output ceilingsystem— the persistent instructionsmessages— the full history (user, assistant, tool results)tools— tool definitions the model may calltool_choice— auto, any, or a forced tool
If you forget to append a turn to messages, the model simply won't see it. The history is the agent's memory.
from anthropic import Anthropic
client = Anthropic()
messages = [{"role": "user", "content": "What is the weather in Paris?"}]
response = client.messages.create(
model="claude-opus-4-8",
max_tokens=1024,
system="You are a helpful travel assistant.",
tools=tools,
messages=messages,
)