Back to Lexicon

ReAct

intermediate

Reasoning and Acting pattern - An agent architecture that interleaves reasoning traces with action execution. The agent thinks about what to do, takes an action, observes the result, and reasons about next steps.

Category: architecture
patternsreasoning

Overview

ReAct (Reasoning + Acting) is a foundational pattern for building AI agents. Instead of having the model directly produce a final answer, ReAct alternates between thinking (reasoning about what to do) and doing (taking actions with tools). The pattern mirrors how humans solve problems: we think about what we need to know, take an action to get that information, reflect on what we learned, and decide what to do next. This loop continues until we have enough information to answer. ReAct significantly improves reliability because each step is explicit and observable. You can see exactly why the agent chose each action, making debugging and improvement much easier.

Key Concepts

Thought

The reasoning step where the agent analyzes the situation and decides what action to take next.

Action

Executing a tool or function to gather information or make changes in the environment.

Observation

The result returned from an action, which becomes input for the next reasoning step.

Loop

The cycle of Thought → Action → Observation that continues until the task is complete.

Code Examples

ReAct Trace Exampletext
Question: What is the population of the capital of France?

Thought: I need to find the capital of France first, then look up its population.
Action: search("capital of France")
Observation: Paris is the capital of France.

Thought: Now I know Paris is the capital. Let me search for its population.
Action: search("population of Paris")
Observation: Paris has a population of approximately 2.1 million in the city proper.

Thought: I now have the answer.
Answer: The population of Paris, the capital of France, is approximately 2.1 million.

Each step shows explicit reasoning followed by an action. The observations feed back into the next thought.

ReAct Implementationpython
def react_agent(question: str, tools: dict, max_steps: int = 10):
    prompt = f"""Answer this question: {question}

Use this format:
Thought: [your reasoning]
Action: [tool_name](params)
Observation: [result will appear here]
... (repeat as needed)
Answer: [final answer]

Available tools: {list(tools.keys())}"""

    messages = [{"role": "user", "content": prompt}]

    for step in range(max_steps):
        response = llm.generate(messages)

        if "Answer:" in response:
            return extract_answer(response)

        if "Action:" in response:
            tool_call = parse_action(response)
            result = tools[tool_call.name](**tool_call.params)
            messages.append({"role": "assistant", "content": response})
            messages.append({"role": "user", "content": f"Observation: {result}"})

    return "Max steps reached without answer"

This shows the core ReAct loop: generate reasoning, parse action, execute tool, add observation, repeat.

Real-World Use Cases

  • 1Question answering with search and retrieval
  • 2Data analysis requiring multiple queries
  • 3Research tasks across multiple sources
  • 4Complex problem-solving with tool use

Practical Tips

  • Always set a maximum step limit
  • Truncate long observations to key information
  • Include few-shot examples of successful ReAct traces
  • Log each step for debugging and analysis

Common Mistakes to Avoid

  • Not limiting the number of steps, causing infinite loops
  • Observations that are too long, overwhelming context
  • Not handling tool errors gracefully
  • Forgetting to include observations in the context

Further Reading

Related Concepts