The Agent Loop
Every agent runs the same fundamental cycle: Perceive, Think, Act, Observe, Learn. This is the pattern behind every autonomous AI system — from Claude Code to self-driving cars. Here is how it works, in theory and in code.
The Five Steps
Click any node on the loop to explore it
The Loop
This is the heartbeat of every AI agent. Unlike a chatbot that responds once and stops, an agent cycles through these five steps continuously until its goal is achieved. Click a node above to dive in.
The Loop in Code
Here is a minimal but complete agent loop in Python. Every agent framework (LangChain, CrewAI, Claude Agent SDK) implements this same pattern under the hood:
import anthropic
client = anthropic.Anthropic()
def agent_loop(goal, tools, max_turns=10):
memory = [] # Conversation history = agent memory
turn = 0
while turn < max_turns:
# STEP 1: PERCEIVE — gather current state
messages = memory + [{
"role": "user",
"content": goal if turn == 0 else "Continue working toward the goal."
}]
# STEP 2: THINK — LLM reasons about what to do
response = client.messages.create(
model="claude-sonnet-4-6",
max_tokens=1024,
system=f"You are an agent. Goal: {goal}",
tools=tools,
messages=messages
)
# STEP 3: ACT — execute any tool calls
if response.stop_reason == "tool_use":
for block in response.content:
if block.type == "tool_use":
# STEP 4: OBSERVE — run the tool and get result
result = execute_tool(block.name, block.input)
# STEP 5: LEARN — store result in memory
memory.append({
"role": "assistant",
"content": response.content
})
memory.append({
"role": "user",
"content": [{
"type": "tool_result",
"tool_use_id": block.id,
"content": result
}]
})
else:
# No tool call = agent is done
return response.content[0].text
turn += 1
return "Max turns reached without completion"
execute_tool() runs it for real — reading files, calling APIs, querying databases.
Why the Loop Matters
A chatbot calls the LLM once and returns the result. An agent calls the LLM in a loop, feeding each result back as context for the next decision. This is the difference between "answer a question" and "solve a problem."
AI: "I cannot check the weather."
Done. No tools. No loop.
Loop 2: Check calendar → find outdoor meeting
Loop 3: Send email → "Bring an umbrella"
Problem solved autonomously.
The Stop Condition
Every loop needs a way to stop. Without a stop condition, your agent runs forever. There are three ways agents decide to stop:
else branch on line 37 — when stop_reason is not "tool_use", the loop exits.
max_turns=10 parameter prevents runaway agents. If the agent cannot solve the problem in 10 loops, something is wrong — stop and report.
Real-World Agent Loops
The same loop pattern powers vastly different systems. The only thing that changes is what tools are available and what the goal is:
Common Loop Failures
Understanding how loops break makes you a better agent builder:
max_turns limit + progress detection. If the last 3 tool results are identical, stop.