2026-05-20 21:39:12 +08:00

45 lines
1.3 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
title: ReAct 循环
created: 2026-04-13
updated: 2026-04-13
type: concept
tags: [agent, runtime, architecture]
sources: [packages/backend/src/modules/netaclaw/runtime/agent.ts]
---
# ReAct 循环
ReActReasoning + Acting是 NetaClaw Agent 的核心推理模式。LLM 交替进行"思考"和"行动",直到任务完成。
## 工作原理
```
用户输入 → LLM 推理
├─ 直接回答(无需工具)→ 结束
└─ 需要工具 → 输出 tool_use block
→ 执行工具 → 获取 tool_result
→ 将结果追加到对话历史
→ 再次调用 LLM 推理(下一轮)
→ 重复直到 LLM 不再调用工具
```
## 关键约束
- **最大轮次**`maxToolRounds` 限制循环次数,防止无限循环
- **流式输出**:每轮推理的 token 实时通过 WebSocket 推送
- **思考能力**:支持 Anthropic 的 `extended_thinking`,思考内容单独推送
## 与传统 Chain 的区别
| | ReAct | 固定 Chain |
|---|---|---|
| 决策方式 | LLM 自主决定是否调用工具 | 预定义的步骤序列 |
| 灵活性 | 高,可动态组合工具 | 低,固定流程 |
| 适用场景 | 开放式任务 | 结构化流程 |
## 相关页面
- [[agent-runtime]] — 实现 ReAct 循环的代码
- [[tool-system]] — 循环中可调用的工具
- [[websocket-gateway]] — 流式推送通道