Skip to content

ReAct Pattern

Translation in Progress

This page is being translated. Content below is a placeholder.

What is ReAct

ReAct = Reasoning + Acting

A paradigm that combines reasoning and acting for AI Agents.

The Three-Step Loop

1. Thought

The LLM analyzes the current situation:

  • What has been done?
  • What information is needed?
  • What's the next step?

2. Action

Choose and execute an action:

  • Call a tool
  • Generate output
  • Request more information

3. Observation

Observe action results:

  • Tool execution result
  • Error messages
  • New information discovered

Example

User: "What's the total line count of all TypeScript files in the project?"

Thought: Need to find all .ts files and count lines
Action: run_command("find . -name '*.ts' | xargs wc -l")
Observation: Total 12345 lines

Thought: Got the result, can answer now
Action: Output answer
Result: "This project has 12,345 lines of TypeScript code"

Implementation in gemini-cli

typescript
// Simplified ReAct loop
async function agentLoop(task: string) {
  let context = task

  while (true) {
    // Thought: LLM decides what to do
    const response = await llm.generate(context)

    // Check if task is complete
    if (!response.hasToolCall()) {
      return response.text()
    }

    // Action: Execute tool
    const result = await executeTool(response.toolCall)

    // Observation: Add result to context
    context = addToContext(context, result)
  }
}

Why ReAct Works

  1. Decomposition: Complex tasks → simple steps
  2. Feedback: Each step has observable results
  3. Correction: Can adjust strategy based on results
  4. Transparency: Reasoning process is visible

Summary

  • ReAct = Reasoning + Acting
  • Core loop: Thought → Action → Observation
  • Well-suited for complex task decomposition and execution

Next

Understand how to interact with LLM APIs: LLM API Interaction →

Learn AI Agent development through real source code