0 XP
L1
?
Lessons
The ReAct Pattern
concept ⏱ 15m
1/3

The ReAct Pattern

ReAct (Reasoning + Acting) is the most common architecture for agentic AI. It's what Claude Code uses, what most agent frameworks implement, and what you'll use as a default pattern.

The Core Idea

Instead of the model thinking everything through then acting, or acting without thinking, ReAct interleaves reasoning and action:

Thought: I need to find the bug. Let me look at the error log first.
Action: Read the error log file
Observation: "TypeError: Cannot read property 'name' of undefined at line 42"
Thought: The issue is on line 42, accessing 'name' on something undefined. Let me check what's passed to that function.
Action: Read the source file around line 42
Observation: function getUser(id) { return users.find(u => u.id === id).name }
Thought: The find() could return undefined if no user matches. I need to add a null check.
Action: Edit the file to add null handling

Each cycle: think about what you know, take one action, observe the result, think again.

❓ Quiz 1
What's the key advantage of ReAct over plan-then-execute?
The power of ReAct is adaptation. Each observation can change the plan. Plan-then-execute is brittle when assumptions are wrong.
Answer to continue ↓

Now put it in order

You just read a ReAct trace. Can you reconstruct the sequence?

🎮 ReAct Loop Sequence
Put these steps in the correct order for a ReAct agent fixing a bug.
Add a null check before accessing .name (act)
See that .find() can return undefined (observe)
Run the tests to verify the fix (observe)
Read the source file at line 42 (act)
Reason: the error is a null reference on line 42 (think)
Read the error log (observe)
Complete to continue ↓

When ReAct Fails

ReAct isn't perfect. Common failure modes:

Infinite loops: The agent keeps trying the same approach that doesn't work. Solution: track attempted actions and force variation after N failures.

Hallucinated tools: The model invents tools that don't exist. Solution: strong tool descriptions and listing available tools in the system prompt.

Premature termination: The agent declares victory before actually solving the problem. Solution: require verification steps (run the test, check the output).

Context window saturation: Long ReAct chains fill the context window with observations. Solution: summarize intermediate results, drop old observations.

❓ Quiz 2
An agent keeps trying to fix a bug by editing the same file in the same way, getting the same error each time. What ReAct failure mode is this?
This is a classic infinite loop — the agent doesn't track what it's already tried. Good agent design includes action deduplication or forced exploration.
Answer to continue ↓

ReAct in Your Workflow

In Claude Code, you see ReAct in action every time it:

  1. Reads a file (observation)
  2. Thinks about what to change (extended thinking = reasoning)
  3. Edits the file (action)
  4. Runs tests to verify (observation)
  5. Iterates if tests fail (back to reasoning)
  6. The CLAUDE.md rules like "When there's a bug, start by writing a test that reproduces it, then fix it" are essentially ReAct templates — they structure the reasoning-action pattern for specific scenarios.

Review

Time to consolidate what you learned.

🛠 Exercise 1
Think about the last time Claude Code struggled with a task for you. Identify which ReAct failure mode occurred (infinite loop, hallucinated tool, premature termination, or context saturation). Describe what happened and how the agent could have recovered.
✓ Saved
advance · ? shortcuts 02.01
Claude — Tutor
select text for context
Ask me anything about this lesson.
I can see your quiz answers and decisions.

💡 Select text in the lesson to use it as context.
CONTEXT