Skip to content
AgenticAIWiki
  1. Learn
  2. Part 9
  3. 46. ReAct
  4. 46.2

Lesson 46.2 · 2 of 3 · 5 min read · Reviewed 24 Nov 2026

ReAct with native tool use

By the end, you can

  • Map ReAct onto Claude's tool use.
  • Build the loop without hand-written Thought/Action text.

ReAct, the API way

ReAct step In the Messages API
Thought A thinking block (13.6), or short text before a tool call
Action A tool_use block with a tool name and JSON input
Observation Your tool_result block in the next user message
Answer A final text block with stop_reason: "end_turn"

The loop

messages = [user question]
for step in range(MAX_STEPS):
    response = claude(messages, tools=TOOLS)
    messages.append(assistant: response.content)          # keep every block
    if response.stop_reason != "tool_use":
        return final_text(response)
    results = [run(call) for call in tool_uses(response)]  # can run in parallel
    messages.append(user: results)                         # all results together
raise StepLimitReached

Chapter 33 builds this in detail; chapter 42 shows the Agent SDKClaude Agent SDK: Anthropic’s library for building agents on the same loop that powers Claude Code, with tools, hooks, subagents and MCP. doing it for you.

What you still decide

  • Which tools the agentAgent: A system where a model decides its own next steps, calling tools in a loop until the task is done. sees (fewer, clearer tools work better: chapter 34).
  • Limits: steps, tokensToken: The unit a model reads and writes: a word, part of a word, a space or a symbol, mapped to an integer id., time, and what happens when they’re hit.
  • Checks: validate tool inputs and block risky actions in code (chapters 44 and 45).
  • Effort: higher effort for hard investigations, lower for simple lookups (13.6).

Try it

The Playground’s tool-loop project runs this loop against simulated tools. No API key needed.

Key points

  • Claude's tool-use loop is ReAct built into the API: thinking or text, then tool_use, then your tool_result.
  • You don't need to parse 'Thought:' and 'Action:' strings; tool calls arrive as structured blocks.
  • Adaptive thinking gives a hidden reasoning step between tool calls.
  • Your code owns the loop: run tools, return results, enforce limits.
Hands-on labRuns in the Playground

Run ReAct on structured tool use

Map thought, action and observation onto tool_use and tool_result blocks with no text parsing.

You will have: A bounded tool-use loop against the simulated model that answers an order question.

Steps

  1. Run the starter: the first response stops with tool_use and nothing happens next.
  2. Append the full assistant content to messages so every tool_use stays in history.
  3. Return the final text when stop_reason is not tool_use.
  4. Run each tool_use block and send all results back in one user message.
  5. Check the Trace tab: each step lines up with a ReAct step.

Done when

  • The run ends with an answer about order A-1042 in 2 steps.
  • The Action and Observation lines come from structured blocks, not parsed strings.
  • The loop is bounded by MAX_STEPS.

Open in the Playground No API key needed.

Starter code (Python)
import json
import anthropic

# ReAct with structured tool use: tool_use is the Action, your tool_result is the Observation.
client = anthropic.Anthropic()   # simulated key and model; nothing real is called

ORDERS = {"A-1042": {"item": "desk lamp", "status": "shipped", "total": 35}}

tools = [{
    "name": "get_order",
    "description": "Look up an order by its id, like A-1042. Returns the item, status and total. Use it whenever the user asks about an order.",
    "input_schema": {"type": "object", "properties": {"order_id": {"type": "string", "description": "Order id, like A-1042"}}, "required": ["order_id"]},
}]


def get_order(order_id):
    order = ORDERS.get(order_id)
    return (json.dumps(order), True) if order else (f"No order {order_id}. Ids look like A-1042.", False)


messages = [{"role": "user", "content": "Where is my order A-1042?"}]
MAX_STEPS = 5

for step in range(1, MAX_STEPS + 1):
    r = client.messages.create(model="claude-sonnet-5-5", max_tokens=400, tools=tools, messages=messages)
    print(f"step {step}: stop_reason={r.stop_reason}")
    # TODO: append the full assistant content to messages (keep every block)
    # TODO: if stop_reason is not "tool_use", print "Answer:" with the text and "steps used:", then break
    # TODO: run each tool_use block (print "Action:" and "Observation:") and send all tool_result blocks in ONE user message
    break
Show a solution
import json
import anthropic

# ReAct with structured tool use: tool_use is the Action, your tool_result is the Observation.
client = anthropic.Anthropic()   # simulated key and model; nothing real is called

ORDERS = {"A-1042": {"item": "desk lamp", "status": "shipped", "total": 35}}

tools = [{
    "name": "get_order",
    "description": "Look up an order by its id, like A-1042. Returns the item, status and total. Use it whenever the user asks about an order.",
    "input_schema": {"type": "object", "properties": {"order_id": {"type": "string", "description": "Order id, like A-1042"}}, "required": ["order_id"]},
}]


def get_order(order_id):
    order = ORDERS.get(order_id)
    return (json.dumps(order), True) if order else (f"No order {order_id}. Ids look like A-1042.", False)


messages = [{"role": "user", "content": "Where is my order A-1042?"}]
MAX_STEPS = 5

for step in range(1, MAX_STEPS + 1):
    r = client.messages.create(model="claude-sonnet-5-5", max_tokens=400, tools=tools, messages=messages)
    print(f"step {step}: stop_reason={r.stop_reason}")
    messages.append({"role": "assistant", "content": r.content})
    if r.stop_reason != "tool_use":
        print("Answer:", "".join(b.text for b in r.content if b.type == "text"))
        print("steps used:", step)
        break
    results = []
    for b in r.content:
        if b.type != "tool_use":
            continue
        out, ok = get_order(**b.input) if "order_id" in b.input else ("Missing order_id", False)
        print(f"Action: {b.name}({b.input['order_id']})")
        print(f"Observation: {out}")
        results.append({"type": "tool_result", "tool_use_id": b.id, "content": out, "is_error": not ok})
    messages.append({"role": "user", "content": results})
else:
    print("Stopped: step limit reached")

Do it Run a tool loop in the Playground in the Playground. No API key needed.

Quick checkAn old agent parses 'Action: search(...)' from plain text and often breaks. What's the better approach on Claude?
How sure are you?
Pick an answer and how sure you are