ReAct Loop - Reason, Act, Observe
ReAct Loop implements the ReAct (Reason + Act) paradigm for solving complex problems through iterative exploration.
The core insight: Alternating between reasoning and action produces better results than either alone—reasoning guides which actions to take, and observations from actions inform better reasoning.
The Analogy: Detective Investigation
| Just Thinking | Just Acting | ReAct Loop |
|---|
| Theorize without evidence | Randomly collect evidence | Form hypothesis → Test → Update |
| Miss key clues |
Waste time on irrelevant leads | Systematic, evidence-driven |
| Paralysis by analysis | Spray and pray | Balanced exploration |
When to Use ReAct Loop
Perfect for:
- - 🔍 Research questions (gather info from multiple sources)
- 🐛 Debugging (hypothesize cause → test → observe)
- 📊 Data exploration (query → analyze → decide next query)
- 🌐 Web/API tasks (fetch data → process → fetch more)
- 🛠️ Tool-using workflows (sequence of tool calls needed)
- 🧩 Problems where you "don't know what you don't know"
Skip for:
- - ✅ Direct questions with known answers
- 📝 Single-step tasks ("write a summary")
- 🎯 Problems with clear predetermined steps
The ReAct Pattern
CODEBLOCK0
The Workflow
Step 1: Initial Reasoning
Assess what you know and what you need:
CODEBLOCK1
Step 2: Take Action
Execute the decided action:
CODEBLOCK2
Step 3: Observe Results
Record what happened:
CODEBLOCK3
Step 4: Iterate - Next Reasoning
Update understanding, decide next step:
CODEBLOCK4
Step 5: Next Action
CODEBLOCK5
Step 6: Next Observation
CODEBLOCK6
Step 7: Continue Iterating...
CODEBLOCK7
Step 8: Conclude
CODEBLOCK8
ReAct Format
Standard format for tracking:
CODEBLOCK9
Common Action Types
Information Gathering
CODEBLOCK10
Execution
CODEBLOCK11
Calculation/Processing
CODEBLOCK12
Communication
CODEBLOCK13
Best Practices
1. Thoughts Should Guide Actions
❌ Bad: Action without reasoning
CODEBLOCK14
✅ Good: Reasoning explains why
CODEBLOCK15
2. Be Specific in Actions
❌ Vague:
CODEBLOCK16
✅ Specific:
CODEBLOCK17
3. Record Full Observations
❌ Partial:
CODEBLOCK18
✅ Complete:
CODEBLOCK19
4. Update Understanding Each Iteration
CODEBLOCK20
5. Know When to Stop
Stop when:
- - ✅ Question is answered
- ✅ Problem is solved
- ✅ Sufficient information gathered
- ❌ Not making progress (stuck in loop)
- ❌ Hitting iteration limits
If stuck: Try different angle, ask for help, or escalate.
Examples
Example 1: Research Task
Task: "What's the best approach for real-time notifications in a React app?"
CODEBLOCK21
Example 2: Debugging
Task: "Fix failing CI build"
CODEBLOCK22
Error: Cannot find module '../config/database'
Require stack:
### Iteration 2
**Thought 2:**
Test setup can't find database config. This could be:
1. File was deleted/moved
2. Path changed in test
3. Config not generated in CI environment
Let me check if the file exists in repo.
**Action 2:**
Check: Does config/database.js exist in repository?
**Observation 2:**
File exists: config/database.js
But error shows it's looking for '../config/database' from tests/setup.js
Path resolution issue. Let me check directory structure.
### Iteration 3
**Thought 3:**
Path is '../config/database' from tests/setup.js.
This should resolve to config/database.js if tests/ is sibling to config/.
Let me check actual directory structure.
**Action 3:**
Run: find . -name "setup.js" -o -name "database.js" | head -20
**Observation 3:**
./src/config/database.js
./tests/setup.js
Ah! config is under src/, not at root. Path should be '../src/config/database'
### Iteration 4
**Thought 4:**
Path in test is wrong. But why did it work before?
Either:
1. File was moved recently
2. Test was changed
3. Working directory changed
Let me check git history.
**Action 4:**
Run: git log --oneline -10 -- tests/setup.js src/config/
**Observation 4:**
abc1234 Move config to src/ (yesterday)
def5678 Update test dependencies
CODEBLOCK25
Example 3: Data Analysis
Task: "Why did conversion rate drop last week?"
CODEBLOCK26
Integration with Other Skills
ReAct + Plan First:
- - Plan First: Create initial plan
- ReAct Loop: Execute each step with reasoning-observation cycles
ReAct + Self-Critique:
- - ReAct Loop: Execute and observe
- Self-Critique: Review if reasoning path is sound
ReAct + Team Code:
- - ReAct Loop: Manager uses to coordinate (reason → delegate → observe)
- Team Code: Multiple agents working in parallel
Anti-Patterns to Avoid
1. Actions Without Reasoning
❌ Bad:
CODEBLOCK27
✅ Good:
CODEBLOCK28
2. Ignoring Observations
❌ Bad:
CODEBLOCK29
✅ Good:
CODEBLOCK30
3. Getting Stuck in Loops
❌ Bad:
CODEBLOCK31
✅ Good:
CODEBLOCK32
4. Stopping Too Early
❌ Bad:
CODEBLOCK33
✅ Good:
CODEBLOCK34
Quick Start Template
CODEBLOCK35
References
- - Research: "ReAct: Synergizing Reasoning and Acting in Language Models" (Yao et al., 2022)
- Related: Chain-of-Thought, ToolFormer, WebGPT
- See references/examples.md for detailed examples
ReAct 循环 - 推理、行动、观察
ReAct 循环实现了 ReAct(推理+行动)范式,通过迭代探索解决复杂问题。
核心洞察:在推理和行动之间交替比单独使用任何一种都能产生更好的结果——推理指导应采取哪些行动,而行动中的观察则为更好的推理提供信息。
类比:侦探调查
| 仅思考 | 仅行动 | ReAct 循环 |
|---|
| 无证据的理论化 | 随机收集证据 | 形成假设 → 测试 → 更新 |
| 错过关键线索 |
在无关线索上浪费时间 | 系统性、证据驱动 |
| 分析瘫痪 | 盲目尝试 | 平衡探索 |
何时使用 ReAct 循环
适用于:
- - 🔍 研究问题(从多个来源收集信息)
- 🐛 调试(假设原因 → 测试 → 观察)
- 📊 数据探索(查询 → 分析 → 决定下一个查询)
- 🌐 Web/API 任务(获取数据 → 处理 → 获取更多数据)
- 🛠️ 工具使用工作流(需要一系列工具调用)
- 🧩 你不知道你不知道什么的问题
跳过:
- - ✅ 有已知答案的直接问题
- 📝 单步任务(写一个摘要)
- 🎯 有明确预定步骤的问题
ReAct 模式
┌─────────────────────────────────────────────────────────┐
│ 循环:重复直到解决或达到最大迭代次数 │
│ │
│ 第1步:推理(思考) │
│ 基于我所知道的,我需要... │
│ ↓ │
│ 第2步:行动(行动) │
│ [使用工具,运行查询,获取数据,执行代码] │
│ ↓ │
│ 第3步:观察(观察) │
│ [结果,输出,数据,错误] │
│ ↓ │
│ 带着新信息返回第1步 │
└─────────────────────────────────────────────────────────┘
工作流程
第1步:初始推理
评估你知道什么以及你需要什么:
思考1:
我需要找出为什么API很慢。我知道:
- - 3月15日延迟增加
- 特别是/search端点
- 数据库CPU正常
我不知道的是:
- - 是搜索算法还是数据层的问题?
- 3月15日发生了什么变化?
- 是所有查询还是特定查询?
下一步行动:检查3月15日搜索端点的git日志变更
第2步:采取行动
执行决定的行动:
行动1:
运行:git log --since=2024-03-14 --until=2024-03-16 --oneline -- api/search/
第3步:观察结果
记录发生了什么:
观察1:
abc1234 向搜索添加模糊匹配
def5678 更新搜索排名算法
ghi9012 将搜索结果限制从10增加到100
第4步:迭代 - 下一次推理
更新理解,决定下一步:
思考2:
3月15日的三个变更:
- 1. 添加了模糊匹配
- 排名算法改变
- 结果限制增加了10倍
结果限制增加(10→100)很可疑——可能导致:
我应该检查延迟是否与结果大小相关。
下一步行动:查询响应大小与延迟相关性的日志
第5步:下一步行动
行动2:
运行:analyzelogs --endpoint=/search --metric=responsesize --since=2024-03-14
第6步:下一次观察
观察2:
平均响应大小:
- - 3月14日:45KB
- 3月15日:420KB
- 3月16日:415KB
延迟与响应大小的相关性:r=0.89(强相关)
第7步:继续迭代...
思考3:
发现强相关!响应大小增加了10倍,延迟激增。
结果限制变更(ghi9012)很可能是罪魁祸首。
但我应该验证模糊匹配是否也不慢。
让我检查有和没有模糊匹配的搜索查询执行时间。
行动3:
运行:benchmark_search --fuzzy=on --fuzzy=off --iterations=100
观察3:
无模糊:平均45ms
有模糊:平均52ms(增加15%)
与响应大小影响相比不显著。
第8步:得出结论
思考4:
已确定根本原因:将结果限制从10增加到100个条目
导致响应大小增加10倍,这与延迟增加强相关。
建议:
- 1. 将结果限制恢复为10(快速修复)
- 为需要更多结果的客户端实现分页(正确修复)
最终答案:延迟激增是由提交ghi9012引起的,该提交
将搜索结果限制从10增加到100。这使响应大小增加了
10倍。恢复或实现分页。
ReAct 格式
跟踪的标准格式:
markdown
ReAct 会话:[任务描述]
迭代1
思考1:
[关于当前状态和下一步做什么的推理]
行动1:
[采取的具体行动]
观察1:
[行动结果]
迭代2
思考2:
[基于观察1的更新推理]
行动2:
[下一步行动]
观察2:
[结果]
[继续直到解决...]
最终答案
[基于累积观察的结论]
常见行动类型
信息收集
行动:搜索网络Python异步最佳实践2024
行动:查询数据库:SELECT * FROM users WHERE last_login > NOW() - INTERVAL 7 days
行动:读取文件:src/auth/middleware.py
行动:获取API:GET https://api.github.com/repos/owner/repo/issues
执行
行动:运行Python脚本分析日志文件
行动:执行SQL迁移
行动:运行测试:pytest tests/test_feature.py -v
行动:部署到staging环境
计算/处理
行动:计算变量X和Y之间的相关性
行动:转换数据:标准化列值
行动:聚合:按月汇总收入
沟通
行动:向团队发送包含发现的邮件
行动:向Slack频道发送消息
行动:创建GitHub issue
最佳实践
1. 思考应指导行动
❌ 糟糕: 无推理的行动
行动:检查日志
✅ 好: 推理解释原因
思考:错误间歇性发生。让我检查它是否
与特定时间或流量模式相关。
行动:查询过去7天按小时分组的错误日志
2. 行动要具体
❌ 模糊:
行动:查看数据库
✅ 具体:
行动:运行:SELECT COUNT(*) FROM orders WHERE status = pending
AND created_at < NOW() - INTERVAL 1 day
3. 记录完整观察
❌ 部分:
观察:发现一些错误
✅ 完整:
观察:过去24小时内有23个错误,全部来自IP 203.0.113.42,
全部发生在02:00-04:00 UTC之间,全部带有消息
连接超时
4. 每次迭代更新理解
思考1:怀疑数据库问题
[行动,观察]
思考2:数据库健康。错误与网络相关。
怀疑DNS解析。
[下一步行动]
5. 知道何时停止
停止当:
- - ✅ 问题已回答
- ✅ 问题已解决
- ✅ 已收集足够信息
- ❌ 没有进展(陷入循环)
- ❌ 达到迭代限制
如果卡住: 尝试不同角度,寻求帮助,或升级。
示例
示例1:研究任务
任务: React应用中实时通知的最佳方法是什么?
markdown
ReAct 会话:研究React实时通知
迭代1
思考1:
我需要了解React中实时通知的当前选项。
常见方法:WebSockets、服务器发送事件(SSE)、轮询。
让我搜索当前最佳实践。
行动1:
搜索网络:React实时通知最佳实践2024
观察1:
结果提到:
- - WebSockets(Socket.io)- 双向,适合聊天
- SSE - 单向,更简单,自动重连
- 长轮询 - 备选,效率较低
- Web Push API - 用于离线通知
迭代2
思考2:
存在多个选项。决策取决于:
- 1. 我们需要双向通信吗?
- 我们需要离线通知吗?
- 浏览器支持要求?
让我检查是否需要双向(客户端能否向服务器发送?)
行动2:
检查需求文档:客户端是否需要向服务器发送实时数据?
观察2:
需求:用户操作应实时更新给其他用户
这意味着双向:客户端发送操作 → 服务器广播给其他人
迭代3