0Pricing
Claude Architect · Lesson

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 model
  • max_tokens — output ceiling
  • system — the persistent instructions
  • messages — the full history (user, assistant, tool results)
  • tools — tool definitions the model may call
  • tool_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,
)

All lessons in this course

  1. The Core Loop
  2. Terminating on stop_reason
  3. Anti-Pattern: Parsing Text for Completion
  4. Anti-Pattern: Arbitrary Iteration Caps
← Back to Claude Architect