让 Agent 学会"边想边做"的经典范式
第3章的天气 Agent 能查天气、能规划行程,但它不会思考为什么要查——用户问"明天适合跑步吗",它直接调 get_weather,却没有先推理"适合跑步需要看天气+空气质量"。这导致了两个问题:① 可能漏掉该调的工具,② 可能调了不必要的工具浪费 token。ReAct 就是为了解决这两个问题而诞生的——让 Agent 在行动前先思考,观察后再反思。
这一章内容很多,但如果你是第一次接触 ReAct,只要先搞懂三件事就够了:为什么要先想再做、Thought / Action / Observation 如何形成闭环、它和上一章工具调用有什么关系。
你可以先读 4.1 到 4.4,把 ReAct 的核心循环吃透;4.5 之后的 ReWOO、LLM Compiler、Reflexion 和生产级主循环更适合作为进阶阅读,不需要一次全吞下去。
假设用户问 Agent:"2024年诺贝尔物理学奖得主是谁?他们的研究领域是什么?"
一个没有规划能力的 Agent 可能直接让 LLM 回答——但 LLM 的知识截止到训练日期,可能不知道 2024 年的诺奖。它需要搜索。但什么时候搜?搜什么?搜完之后做什么?
这就是 ReAct 要解决的问题。
在 ReAct 之前,Agent 面临两难:
2022 年,Yao 等人在论文 "ReAct: Synergizing Reasoning and Acting in Language Models" 中提出了 ReAct 范式,将两者结合:
推理指导行动,行动反馈推理。LLM 先"想"该做什么(Thought),再"做"(Action),然后"看"结果(Observation),基于结果再"想"下一步——形成闭环。
ReAct 的核心是三个角色的循环:
ReAct 的实现核心是 System Prompt。来看一个标准的 ReAct prompt:
尽可能回答以下问题。你可以使用以下工具:
可用工具:
- search(query): 搜索互联网获取信息
- lookup(keyword): 在上一次搜索结果中查找关键词
- finish(answer): 提交最终答案
格式要求:
每一步必须使用以下格式:
Thought: 你对下一步该做什么的推理
Action: 工具名称(参数)
Observation: 工具返回的结果
... (Thought/Action/Observation 可以重复多次)
Thought: 我现在知道答案了
Action: finish(最终答案)
开始!
问题: {user_question}
用 "Thought:" "Action:" "Observation:" 标记,让 LLM 的输出可被程序解析。
"Observation:" 不是 LLM 生成的,是框架执行工具后拼接上去的。
当 LLM 输出 finish(answer) 时结束循环。也可设置最大步数防止死循环。
Thought 部分是 LLM 的"内心独白",让推理过程可审计、可调试。
ReAct 的核心循环是"边想边做"——每一步 Thought 之后立刻 Action,获得 Observation 后再想下一步。这很直观,但有一个严重的问题:每一步的 Observation 都会被塞入上下文,随着步数增加,token 消耗线性膨胀。
ReWOO 提出了一个反直觉的思路:先想好所有步骤,再统一执行。
ReWOO 把 ReAct 的循环拆成了三个独立阶段:
LLM 一次性生成完整的推理链,每一步用 #E 标记期望的工具调用。因为还没有 Observation,Planner 必须靠自身推理决定步骤——这就是 "Without Observation" 的含义。
逐个执行 Planner 规划的工具调用,将结果填入 #E 占位符。Worker 是程序化的,不需要 LLM 参与——纯工具执行。
LLM 接收已填入结果的完整推理链,综合所有信息生成最终答案。此时 LLM 看到的是"规划+真实结果"的完整视图。
from openai import OpenAI
client = OpenAI()
TOOLS = {"search": search_web, "weather": get_weather, "calc": calculate}
# ===== Phase 1: Planner =====
PLANNER_PROMPT = """你是一个规划器。根据用户问题,生成一系列步骤来解决问题。
每一步用 #E 标记需要执行的工具调用。
格式:
Plan: 第一步的推理
#E1 = tool_name("参数")
Plan: 第二步的推理(可引用 #E1 的结果)
#E2 = tool_name("参数")
...
问题: {question}"""
def plan_steps(question):
"""Phase 1: LLM 一次性规划所有步骤"""
resp = client.chat.completions.create(
model="ai-4o",
messages=[{"role": "system", "content": PLANNER_PROMPT},
{"role": "user", "content": question}]
)
return resp.choices[0].message.content
# ===== Phase 2: Worker =====
def execute_steps(plan_text):
"""Phase 2: 解析并执行所有 #E 工具调用"""
import re
results = {}
# 查找所有 #E 标记
steps = re.findall(r'#E(\d+)\s*=\s*(\w+)\((".*?"|\'.*?\'|.*?)\)', plan_text)
for step_id, tool_name, tool_input in steps:
tool_input = tool_input.strip('"\'')
if tool_name in TOOLS:
results[f"#E{step_id}"] = TOOLS[tool_name](tool_input)
else:
results[f"#E{step_id}"] = f"Error: {tool_name} not found"
return results
# ===== Phase 3: Solver =====
SOLVER_PROMPT = """根据以下规划和执行结果,给出最终答案。
规划:
{plan}
执行结果:
{results}
请综合以上信息,给出完整答案。"""
def solve_answer(plan_text, results):
"""Phase 3: LLM 综合所有结果生成答案"""
# 将结果填入规划的 #E 占位符
filled_plan = plan_text
for key, value in results.items():
filled_plan = filled_plan.replace(key, f"[结果: {value}]")
resp = client.chat.completions.create(
model="ai-4o",
messages=[{"role": "system", "content": SOLVER_PROMPT},
{"role": "user", "content":
f"规划:\n{plan_text}\n\n执行结果:\n{filled_plan}"}]
)
return resp.choices[0].message.content
# ===== 完整流程 =====
def rewoo_agent(question):
"""ReWOO: Planner → Worker → Solver"""
# 1. 规划
plan = plan_steps(question)
# 2. 执行
results = execute_steps(plan)
# 3. 求解
answer = solve_answer(plan, results)
return answer
# 使用
answer = rewoo_agent("北京今天天气如何?适合户外活动吗?")import OpenAI from 'openai'; const client = new OpenAI(); const TOOLS: Recordstring> = { search: search_web, weather: get_weather, calc: calculate }; // ===== Phase 1: Planner ===== const PLANNER_PROMPT = `你是一个规划器。根据用户问题,生成一系列步骤来解决问题。 每一步用 #E 标记需要执行的工具调用。 格式: Plan: 第一步的推理 #E1 = tool_name("参数") Plan: 第二步的推理(可引用 #E1 的结果) #E2 = tool_name("参数") ... 问题: {question}`; async function plan_steps(question: string): Promise { // Phase 1: LLM 一次性规划所有步骤 const resp = await client.chat.completions.create({ model: 'ai-4o', messages: [ { role: 'system', content: PLANNER_PROMPT }, { role: 'user', content: question } ] }); return resp.choices[0].message.content!; } // ===== Phase 2: Worker ===== function execute_steps(planText: string): Record { // Phase 2: 解析并执行所有 #E 工具调用 const results: Record = {}; const regex = /#E(\d+)\s*=\s*(\w+)\(["']?(.*?)["']?\)/g; let match: RegExpExecArray | null; while ((match = regex.exec(planText)) !== null) { const stepId = match[1]; const toolName = match[2]; const toolInput = match[3]; if (toolName in TOOLS) { results[`#E${stepId}`] = TOOLS[toolName](toolInput); } else { results[`#E${stepId}`] = `Error: ${toolName} not found`; } } return results; } // ===== Phase 3: Solver ===== const SOLVER_PROMPT = `根据以下规划和执行结果,给出最终答案。 规划: {plan} 执行结果: {results} 请综合以上信息,给出完整答案。`; async function solve_answer( planText: string, results: Record ): Promise { // Phase 3: LLM 综合所有结果生成答案 let filledPlan = planText; for (const [key, value] of Object.entries(results)) { filledPlan = filledPlan.replaceAll(key, `[结果: ${value}]`); } const resp = await client.chat.completions.create({ model: 'ai-4o', messages: [ { role: 'system', content: SOLVER_PROMPT }, { role: 'user', content: `规划:\n${planText}\n\n执行结果:\n${filledPlan}` } ] }); return resp.choices[0].message.content!; } // ===== 完整流程 ===== async function rewoo_agent(question: string): Promise { // ReWOO: Planner → Worker → Solver const plan = await plan_steps(question); // 1. 规划 const results = execute_steps(plan); // 2. 执行 const answer = await solve_answer(plan, results); // 3. 求解 return answer; } // 使用 const answer = await rewoo_agent('北京今天天气如何?适合户外活动吗?');
package main
import (
"context"
"fmt"
"regexp"
"strings"
openai "github.com/sashabaranov/go-openai"
)
var client = openai.NewClient()
func searchWeb(input string) string { return "search: " + input }
func getWeather(input string) string { return "weather: " + input }
func calculate(input string) string { return "calc: " + input }
var tools = map[string]func(string) string{
"search": searchWeb,
"weather": getWeather,
"calc": calculate,
}
// ===== Phase 1: Planner =====
const plannerPrompt = `你是一个规划器。根据用户问题,生成一系列步骤来解决问题。
每一步用 #E 标记需要执行的工具调用。
格式:
Plan: 第一步的推理
#E1 = tool_name("参数")
Plan: 第二步的推理(可引用 #E1 的结果)
#E2 = tool_name("参数")
...
问题: %s`
func planSteps(question string) (string, error) {
// Phase 1: LLM 一次性规划所有步骤
resp, err := client.CreateChatCompletion(context.Background(), openai.ChatCompletionRequest{
Model: openai.AIModel,
Messages: []openai.ChatCompletionMessage{
{Role: openai.ChatMessageRoleSystem, Content: fmt.Sprintf(plannerPrompt, "{question}")},
{Role: openai.ChatMessageRoleUser, Content: question},
},
})
if err != nil {
return "", err
}
return resp.Choices[0].Message.Content, nil
}
// ===== Phase 2: Worker =====
func executeSteps(planText string) map[string]string {
// Phase 2: 解析并执行所有 #E 工具调用
results := make(map[string]string)
re := regexp.MustCompile(`#E(\d+)\s*=\s*(\w+)\(["']?(.*?)["']?\)`)
matches := re.FindAllStringSubmatch(planText, -1)
for _, m := range matches {
stepID, toolName, toolInput := m[1], m[2], m[3]
if fn, ok := tools[toolName]; ok {
results["#E"+stepID] = fn(toolInput)
} else {
results["#E"+stepID] = fmt.Sprintf("Error: %s not found", toolName)
}
}
return results
}
// ===== Phase 3: Solver =====
const solverPrompt = `根据以下规划和执行结果,给出最终答案。
规划:
%s
执行结果:
%s
请综合以上信息,给出完整答案。`
func solveAnswer(planText string, results map[string]string) (string, error) {
// Phase 3: LLM 综合所有结果生成答案
filledPlan := planText
for key, value := range results {
filledPlan = strings.ReplaceAll(filledPlan, key, fmt.Sprintf("[结果: %s]", value))
}
resultStr := ""
for k, v := range results {
resultStr += fmt.Sprintf("%s: %s\n", k, v)
}
resp, err := client.CreateChatCompletion(context.Background(), openai.ChatCompletionRequest{
Model: openai.AIModel,
Messages: []openai.ChatCompletionMessage{
{Role: openai.ChatMessageRoleSystem, Content: solverPrompt},
{Role: openai.ChatMessageRoleUser, Content: fmt.Sprintf("规划:\n%s\n\n执行结果:\n%s", planText, filledPlan)},
},
})
if err != nil {
return "", err
}
return resp.Choices[0].Message.Content, nil
}
// ===== 完整流程 =====
func rewooAgent(question string) (string, error) {
// ReWOO: Planner → Worker → Solver
plan, err := planSteps(question) // 1. 规划
if err != nil {
return "", err
}
results := executeSteps(plan) // 2. 执行
answer, err := solveAnswer(plan, results) // 3. 求解
if err != nil {
return "", err
}
return answer, nil
}
// 使用
func main() {
answer, _ := rewooAgent("北京今天天气如何?适合户外活动吗?")
fmt.Println(answer)
}import com.openai.client.OpenAIClient;
import com.openai.models.*;
import java.util.*;
import java.util.regex.*;
public class AgentCode {
static OpenAIClient client = new OpenAIClient();
static Map<String, java.util.function.Function<String, String>> tools = new HashMap<>();
static {
tools.put("search", AgentCode::searchWeb);
tools.put("weather", AgentCode::getWeather);
tools.put("calc", AgentCode::calculate);
}
static String searchWeb(String input) { return "search: " + input; }
static String getWeather(String input) { return "weather: " + input; }
static String calculate(String input) { return "calc: " + input; }
// ===== Phase 1: Planner =====
static final String PLANNER_PROMPT = """
你是一个规划器。根据用户问题,生成一系列步骤来解决问题。
每一步用 #E 标记需要执行的工具调用。
格式:
Plan: 第一步的推理
#E1 = tool_name("参数")
Plan: 第二步的推理(可引用 #E1 的结果)
#E2 = tool_name("参数")
...
问题: %s""";
static String planSteps(String question) {
// Phase 1: LLM 一次性规划所有步骤
ChatCompletionCreateParams params = ChatCompletionCreateParams.builder()
.model("ai-4o")
.addMessage(ChatCompletionMessage.builder()
.role(ChatCompletionMessage.Role.SYSTEM)
.content(String.format(PLANNER_PROMPT, "{question}"))
.build())
.addMessage(ChatCompletionMessage.builder()
.role(ChatCompletionMessage.Role.USER)
.content(question)
.build())
.build();
ChatCompletion resp = client.chat().completions().create(params);
return resp.choices().get(0).message().content().orElse("");
}
// ===== Phase 2: Worker =====
static Map<String, String> executeSteps(String planText) {
// Phase 2: 解析并执行所有 #E 工具调用
Map<String, String> results = new LinkedHashMap<>();
Pattern pattern = Pattern.compile("#E(\\\\d+)\\\\s*=\\\\s*(\\\\w+)\\\\([\"']?(.*?)[\"']?\\\\)");
Matcher matcher = pattern.matcher(planText);
while (matcher.find()) {
String stepId = matcher.group(1);
String toolName = matcher.group(2);
String toolInput = matcher.group(3);
if (tools.containsKey(toolName)) {
results.put("#E" + stepId, tools.get(toolName).apply(toolInput));
} else {
results.put("#E" + stepId, "Error: " + toolName + " not found");
}
}
return results;
}
// ===== Phase 3: Solver =====
static final String SOLVER_PROMPT = """
根据以下规划和执行结果,给出最终答案。
规划:
%s
执行结果:
%s
请综合以上信息,给出完整答案。""";
static String solveAnswer(String planText, Map<String, String> results) {
// Phase 3: LLM 综合所有结果生成答案
String filledPlan = planText;
for (Map.Entry<String, String> entry : results.entrySet()) {
filledPlan = filledPlan.replace(entry.getKey(),
"[结果: " + entry.getValue() + "]");
}
ChatCompletionCreateParams params = ChatCompletionCreateParams.builder()
.model("ai-4o")
.addMessage(ChatCompletionMessage.builder()
.role(ChatCompletionMessage.Role.SYSTEM)
.content(SOLVER_PROMPT)
.build())
.addMessage(ChatCompletionMessage.builder()
.role(ChatCompletionMessage.Role.USER)
.content("规划:\\n" + planText + "\\n\\n执行结果:\\n" + filledPlan)
.build())
.build();
ChatCompletion resp = client.chat().completions().create(params);
return resp.choices().get(0).message().content().orElse("");
}
// ===== 完整流程 =====
static String rewooAgent(String question) {
// ReWOO: Planner → Worker → Solver
String plan = planSteps(question); // 1. 规划
Map<String, String> results = executeSteps(plan); // 2. 执行
String answer = solveAnswer(plan, results); // 3. 求解
return answer;
}
// 使用
public static void main(String[] args) {
String answer = rewooAgent("北京今天天气如何?适合户外活动吗?");
System.out.println(answer);
}
}ReWOO 的 Planner 必须在没有 Observation 的条件下规划所有步骤,这意味着它无法根据中间结果调整策略。如果第一步的搜索结果出乎意料,Planner 无法"转向"——它只能沿着预设路径走到底。这是 ReWOO 相比 ReAct 的核心劣势。
ReAct 是串行的:Thought₁ → Action₁ → Observation₁ → Thought₂ → Action₂ → …。如果 Agent 需要调用 5 个独立的工具,即使它们之间没有依赖关系,也必须一个一个等。
LLM Compiler 提出了并行执行的思路:让 LLM 一次性输出多个独立的工具调用,然后并行执行它们。
关键洞察:很多工具调用之间没有数据依赖。比如:
import asyncio
from openai import OpenAI
client = OpenAI()
# ===== Step 1: 编译器生成并行计划 =====
COMPILER_PROMPT = """根据用户问题,生成工具调用计划。
标记依赖关系:如果一个调用需要另一个的结果,标注 depends_on。
输出JSON格式:
[
{"id": 1, "tool": "search", "args": {"query": "北京天气"},
"depends_on": []},
{"id": 2, "tool": "search", "args": {"query": "上海天气"},
"depends_on": []},
{"id": 3, "tool": "compare", "args": {"data_from": [1, 2]},
"depends_on": [1, 2]}
]
问题: {question}"""
async def compile_plan(question):
"""LLM 编译:生成带依赖关系的工具调用计划"""
resp = client.chat.completions.create(
model="ai-4o",
messages=[{"role": "system", "content": COMPILER_PROMPT},
{"role": "user", "content": question}],
response_format={"type": "json_object"}
)
import json
return json.loads(resp.choices[0].message.content)
# ===== Step 2: 并行执行独立工具 =====
async def execute_parallel(plan):
"""按依赖图并行执行:无依赖的步骤同时运行"""
results = {}
completed = set()
while len(completed) < len(plan):
# 找出所有依赖已完成的步骤
ready = [step for step in plan
if step["id"] not in completed
and all(d in completed for d in step["depends_on"])]
if not ready:
break # 防止死锁
# 并行执行所有就绪步骤
tasks = []
for step in ready:
task = asyncio.create_task(
call_tool_async(step["tool"], step["args"], results)
)
tasks.append((step["id"], task))
# 等待本轮所有任务完成
for step_id, task in tasks:
results[step_id] = await task
completed.add(step_id)
return results
async def call_tool_async(tool_name, args, prev_results):
"""异步调用单个工具"""
# 替换依赖引用为实际结果
if "data_from" in args:
args["data"] = [prev_results[d] for d in args["data_from"]]
# 模拟工具执行(实际项目中替换为真实API调用)
await asyncio.sleep(1) # 模拟网络延迟
return f"{tool_name} 结果: {args}"
# ===== Step 3: Solver 综合答案 =====
async def solve_with_results(question, plan, results):
"""Solver:根据工具调用结果综合最终答案"""
resp = client.chat.completions.create(
model="ai-4o",
messages=[
{"role": "system", "content": "根据以下工具调用结果,回答用户问题。"},
{"role": "user", "content": f"问题: {question}\n\n结果: {results}"}
]
)
return resp.choices[0].message.content
async def llm_compiler_agent(question):
"""LLM Compiler 完整流程"""
plan = await compile_plan(question)
results = await execute_parallel(plan)
answer = solve_with_results(question, plan, results)
return answer
# 使用
answer = await llm_compiler_agent(
"北京、上海、广州三城市今天天气如何?哪个最适合户外活动?"
)// Node.js built-in or npm package for: asyncio
import OpenAI from 'openai';
const client = OpenAI();
// ===== Step 1: 编译器生成并行计划 =====
const COMPILER_PROMPT = """根据用户问题,生成工具调用计划。;
// 标记依赖关系:如果一个调用需要另一个的结果,标注 depends_on。
// 输出JSON格式:
// [
// {"id": 1, "tool": "search", "args": {"query": "北京天气"},
// "depends_on": []},
// {"id": 2, "tool": "search", "args": {"query": "上海天气"},
// "depends_on": []},
// {"id": 3, "tool": "compare", "args": {"data_from": [1, 2]},
// "depends_on": [1, 2]}
// ]
// 问题: {question}"""
// async def compile_plan(question):
/** docstring */
const resp = client.chat.completions.create(;
const model = "ai-4o",;
const messages = [{"role": "system", "content": COMPILER_PROMPT},;
// {"role": "user", "content": question}],
const response_format = {"type": "json_object"};
// )
// Node.js built-in or npm package for: json
return json.loads(resp.choices[0].message.content);
// ===== Step 2: 并行执行独立工具 =====
// async def execute_parallel(plan):
/** docstring */
const results = {};
const completed = set();
while (len(completed) < len(plan)) {
// 找出所有依赖已完成的步骤
const ready = [step for step in plan;
// if step["id"] not in completed
// and all(d in completed for d in step["depends_on"])]
if (!ready) {
// break # 防止死锁
// 并行执行所有就绪步骤
const tasks = [];
for (const step of ready) {
const task = asyncio.create_task(;
// call_tool_async(step["tool"], step["args"], results)
// )
// tasks.append((step["id"], task))
// 等待本轮所有任务完成
for (const [step_id, task] of tasks) {
// results[step_id] = await task
// completed.add(step_id)
return results;
// async def call_tool_async(tool_name, args, prev_results):
/** docstring */
// 替换依赖引用为实际结果
if ("data_from" in args) {
// args["data"] = [prev_results[d] for d in args["data_from"]]
// 模拟工具执行(实际项目中替换为真实API调用)
// await asyncio.sleep(1) # 模拟网络延迟
return `${$1} 结果: ${$1}`;
// ===== Step 3: Solver 综合答案 =====
// async def solve_with_results(question, plan, results):
/** docstring */
const resp = client.chat.completions.create(;
const model = "ai-4o",;
const messages = [;
// {"role": "system", "content": "根据以下工具调用结果,回答用户问题。"},
// {"role": "user", "content": f"问题: {question}\n\n结果: {results}"}
// ]
// )
return resp.choices[0].message.content;
// async def llm_compiler_agent(question):
/** docstring */
const plan = await compile_plan(question);
const results = await execute_parallel(plan);
const answer = solve_with_results(question, plan, results);
return answer;
// 使用
const answer = await llm_compiler_agent(;
// "北京、上海、广州三城市今天天气如何?哪个最适合户外活动?"
// )package main
import (
"fmt"
"os"
"os/exec"
"strings"
)
// import asyncio
// from openai import OpenAI
// Python: client = OpenAI()
// ===== Step 1: 编译器生成并行计划 =====
// Python: COMPILER_PROMPT = """根据用户问题,生成工具调用计划。
// Python: 标记依赖关系:如果一个调用需要另一个的结果,标注 depends_on。
// Python: 输出JSON格式:
// Python: [
// Python: {"id": 1, "tool": "search", "args": {"query": "北京天气"},
// Python: "depends_on": []},
// Python: {"id": 2, "tool": "search", "args": {"query": "上海天气"},
// Python: "depends_on": []},
// Python: {"id": 3, "tool": "compare", "args": {"data_from": [1, 2]},
// Python: "depends_on": [1, 2]}
// Python: ]
// Python: 问题: {question}"""
// Python: async def compile_plan(question):
// Python: resp = client.chat.completions.create(
// Python: model="ai-4o",
// Python: messages=[{"role": "system", "content": COMPILER_PROMPT},
// Python: {"role": "user", "content": question}],
// Python: response_format={"type": "json_object"}
// Python: )
// import json
return json.loads(resp.choices[0].message.content)
// ===== Step 2: 并行执行独立工具 =====
// Python: async def execute_parallel(plan):
// Python: results = {}
// Python: completed = set()
// Python: while len(completed) < len(plan):
// 找出所有依赖已完成的步骤
// Python: ready = [step for step in plan
// Python: if step["id"] not in completed
// Python: and all(d in completed for d in step["depends_on"])]
if not ready {
// Python: break # 防止死锁
// 并行执行所有就绪步骤
// Python: tasks = []
for _, step := range ready {
// Python: task = asyncio.create_task(
// Python: call_tool_async(step["tool"], step["args"], results)
// Python: )
// Python: tasks.append((step["id"], task))
// 等待本轮所有任务完成
// Python: for step_id, task in tasks:
// Python: results[step_id] = await task
// Python: completed.add(step_id)
return results
// Python: async def call_tool_async(tool_name, args, prev_results):
// 替换依赖引用为实际结果
if "data_from" in args {
// Python: args["data"] = [prev_results[d] for d in args["data_from"]]
// 模拟工具执行(实际项目中替换为真实API调用)
// Python: await asyncio.sleep(1) # 模拟网络延迟
return f"{tool_name} 结果: {args}"
// ===== Step 3: Solver 综合答案 =====
// Python: async def solve_with_results(question, plan, results):
// Python: resp = client.chat.completions.create(
// Python: model="ai-4o",
// Python: messages=[
// Python: {"role": "system", "content": "根据以下工具调用结果,回答用户问题。"},
// Python: {"role": "user", "content": f"问题: {question}\n\n结果: {results}"}
// Python: ]
// Python: )
return resp.choices[0].message.content
// Python: async def llm_compiler_agent(question):
// Python: plan = await compile_plan(question)
// Python: results = await execute_parallel(plan)
// Python: answer = solve_with_results(question, plan, results)
return answer
// 使用
// Python: answer = await llm_compiler_agent(
// Python: "北京、上海、广州三城市今天天气如何?哪个最适合户外活动?"
// Python: )import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
import java.io.*;
// import asyncio
// from openai import OpenAI
// Python: client = OpenAI()
// ===== Step 1: 编译器生成并行计划 =====
// Python: COMPILER_PROMPT = """根据用户问题,生成工具调用计划。
// Python: 标记依赖关系:如果一个调用需要另一个的结果,标注 depends_on。
// Python: 输出JSON格式:
// Python: [
// Python: {"id": 1, "tool": "search", "args": {"query": "北京天气"},
// Python: "depends_on": []},
// Python: {"id": 2, "tool": "search", "args": {"query": "上海天气"},
// Python: "depends_on": []},
// Python: {"id": 3, "tool": "compare", "args": {"data_from": [1, 2]},
// Python: "depends_on": [1, 2]}
// Python: ]
// Python: 问题: {question}"""
// Python: async def compile_plan(question):
// Python: resp = client.chat.completions.create(
// Python: model="ai-4o",
// Python: messages=[{"role": "system", "content": COMPILER_PROMPT},
// Python: {"role": "user", "content": question}],
// Python: response_format={"type": "json_object"}
// Python: )
// import json
return json.loads(resp.choices[0].message.content);
// ===== Step 2: 并行执行独立工具 =====
// Python: async def execute_parallel(plan):
// Python: results = {}
// Python: completed = set()
// Python: while len(completed) < len(plan):
// 找出所有依赖已完成的步骤
// Python: ready = [step for step in plan
// Python: if step["id"] not in completed
// Python: and all(d in completed for d in step["depends_on"])]
if (!ready) {
// Python: break # 防止死锁
// 并行执行所有就绪步骤
// Python: tasks = []
for (var step : ready) {
// Python: task = asyncio.create_task(
// Python: call_tool_async(step["tool"], step["args"], results)
// Python: )
// Python: tasks.append((step["id"], task))
// 等待本轮所有任务完成
// Python: for step_id, task in tasks:
// Python: results[step_id] = await task
// Python: completed.add(step_id)
return results;
// Python: async def call_tool_async(tool_name, args, prev_results):
// 替换依赖引用为实际结果
if ("data_from" in args) {
// Python: args["data"] = [prev_results[d] for d in args["data_from"]]
// 模拟工具执行(实际项目中替换为真实API调用)
// Python: await asyncio.sleep(1) # 模拟网络延迟
return f"{tool_name} 结果: {args}";
// ===== Step 3: Solver 综合答案 =====
// Python: async def solve_with_results(question, plan, results):
// Python: resp = client.chat.completions.create(
// Python: model="ai-4o",
// Python: messages=[
// Python: {"role": "system", "content": "根据以下工具调用结果,回答用户问题。"},
// Python: {"role": "user", "content": f"问题: {question}\n\n结果: {results}"}
// Python: ]
// Python: )
return resp.choices[0].message.content;
// Python: async def llm_compiler_agent(question):
// Python: plan = await compile_plan(question)
// Python: results = await execute_parallel(plan)
// Python: answer = solve_with_results(question, plan, results)
return answer;
// 使用
// Python: answer = await llm_compiler_agent(
// Python: "北京、上海、广州三城市今天天气如何?哪个最适合户外活动?"
// Python: )
}| 指标 | ReAct(串行) | LLM Compiler(并行) |
|---|---|---|
| LLM 调用次数 | N 次(每步1次) | 1 次编译 + 1 求解 |
| 工具执行时间 | N × 单次耗时 | ≈ max(并行组耗时) |
| 总耗时(3个独立工具) | ~12s | ~4s(3x加速) |
| 灵活性 | 高(可中途调整) | 低(预编译路径) |
ReAct 有一个隐含假设:LLM 的推理是对的。它只观察工具返回的外部结果,从不质疑自己的推理质量。如果 LLM 在 Thought 中犯了逻辑错误,ReAct 无法发现——它只会继续沿着错误方向走。
Reflexion 给 Agent 加了一个自我反思环节。
from openai import OpenAI
client = OpenAI()
REFLECT_PROMPT = """你刚刚完成了一个任务,但结果不够好。
回顾你的推理过程和执行结果,找出问题所在。
之前的尝试:
{trajectory}
评估反馈:
{evaluation}
请反思:
1. 哪一步推理出了问题?
2. 应该怎么做才更好?
3. 下次尝试时需要注意什么?
输出格式:
Reflection: 你的反思内容"""
def react_agent_with_context(question, context=""):
"""带上下文的 ReAct Agent(复用 react_agent 逻辑,注入额外上下文)"""
messages = [{"role": "system", "content": REACT_PROMPT + context}]
messages.append({"role": "user", "content": question})
trajectory = []
for step in range(10):
resp = client.chat.completions.create(model="ai-4o", messages=messages)
thought = resp.choices[0].message.content
trajectory.append(thought)
if "finish" in thought.lower():
return {"answer": thought, "trajectory": "\n".join(trajectory)}
messages.append({"role": "assistant", "content": thought})
return {"answer": thought, "trajectory": "\n".join(trajectory)}
def reflexion_agent(question, max_trials=3):
"""Reflexion: 执行→评估→反思→重试"""
reflections = [] # 累积的反思经验
for trial in range(max_trials):
# 1. 执行任务(使用 ReAct + 历史反思作为额外上下文)
context = ""
if reflections:
context = f"\n之前的反思经验:\n" + "\n".join(reflections)
result = react_agent_with_context(question, context)
# 2. 评估结果质量
evaluation = evaluate_result(question, result)
if evaluation["score"] >= 0.8: # 质量达标
return result
# 3. 反思推理过程
reflection = client.chat.completions.create(
model="ai-4o",
messages=[
{"role": "system", "content": REFLECT_PROMPT},
{"role": "user", "content":
f"之前的尝试:\n{result['trajectory']}\n\n"
f"评估反馈:\n{evaluation['feedback']}"}
]
).choices[0].message.content
reflections.append(reflection)
print(f"Trial {trial+1}: score={evaluation['score']:.2f}")
print(f"Reflection: {reflection[:100]}...")
return result # 达到最大尝试次数,返回最后结果
def evaluate_result(question, result):
"""LLM 评估答案质量"""
resp = client.chat.completions.create(
model="ai-4o",
messages=[
{"role": "system", "content":
"评估以下答案的质量。输出JSON: {\"score\": 0-1, \"feedback\": \"...\"}"},
{"role": "user", "content":
f"问题: {question}\n答案: {result['answer']}"}
],
response_format={"type": "json_object"}
)
import json
return json.loads(resp.choices[0].message.content)
# 使用
answer = reflexion_agent(
"解释量子纠缠的原理,并说明它与经典关联的区别"
)import OpenAI from 'openai';
const client = OpenAI();
const REFLECT_PROMPT = """你刚刚完成了一个任务,但结果不够好。;
// 回顾你的推理过程和执行结果,找出问题所在。
// 之前的尝试:
// {trajectory}
// 评估反馈:
// {evaluation}
// 请反思:
// 1. 哪一步推理出了问题?
// 2. 应该怎么做才更好?
// 3. 下次尝试时需要注意什么?
// 输出格式:
// Reflection: 你的反思内容"""
function react_agent_with_context(question, context="") {
/** docstring */
const messages = [{"role": "system", "content": REACT_PROMPT + context}];
// messages.append({"role": "user", "content": question})
const trajectory = [];
for (const step of range(10)) {
const resp = client.chat.completions.create(model="ai-4o", messages=messages);
const thought = resp.choices[0].message.content;
// trajectory.append(thought)
if ("finish" in thought.lower()) {
return {"answer": thought, "trajectory": "\n".join(trajectory)};
// messages.append({"role": "assistant", "content": thought})
return {"answer": thought, "trajectory": "\n".join(trajectory)};
function reflexion_agent(question, max_trials=3) {
/** docstring */
const reflections = [] # 累积的反思经验;
for (const trial of range(max_trials)) {
// 1. 执行任务(使用 ReAct + 历史反思作为额外上下文)
const context = "";
if (reflections) {
const context = `\n之前的反思经验:\n" + "\n`;
const result = react_agent_with_context(question, context);
// 2. 评估结果质量
const evaluation = evaluate_result(question, result);
if (evaluation["score"] >= 0.8) {
return result;
// 3. 反思推理过程
const reflection = client.chat.completions.create(;
const model = "ai-4o",;
const messages = [;
// {"role": "system", "content": REFLECT_PROMPT},
// {"role": "user", "content":
// f"之前的尝试:\n{result['trajectory']}\n\n"
// f"评估反馈:\n{evaluation['feedback']}"}
// ]
// ).choices[0].message.content
// reflections.append(reflection)
console.log(`Trial {trial+1}: score={evaluation['score']:.2f}`);
console.log(`Reflection: {reflection[:100]}...`);
return result # 达到最大尝试次数,返回最后结果;
function evaluate_result(question, result) {
/** docstring */
const resp = client.chat.completions.create(;
const model = "ai-4o",;
const messages = [;
// {"role": "system", "content":
// "评估以下答案的质量。输出JSON: {\"score\": 0-1, \"feedback\": \"...\"}"},
// {"role": "user", "content":
// f"问题: {question}\n答案: {result['answer']}"}
// ],
const response_format = {"type": "json_object"};
// )
// Node.js built-in or npm package for: json
return json.loads(resp.choices[0].message.content);
// 使用
const answer = reflexion_agent(;
// "解释量子纠缠的原理,并说明它与经典关联的区别"
// )package main
import (
"fmt"
"os"
"os/exec"
"strings"
)
// from openai import OpenAI
// Python: client = OpenAI()
// Python: REFLECT_PROMPT = """你刚刚完成了一个任务,但结果不够好。
// Python: 回顾你的推理过程和执行结果,找出问题所在。
// Python: 之前的尝试:
// Python: {trajectory}
// Python: 评估反馈:
// Python: {evaluation}
// Python: 请反思:
// Python: 1. 哪一步推理出了问题?
// Python: 2. 应该怎么做才更好?
// Python: 3. 下次尝试时需要注意什么?
// Python: 输出格式:
// Python: Reflection: 你的反思内容"""
func react_agent_with_context() {
// Python: messages = [{"role": "system", "content": REACT_PROMPT + context}]
// Python: messages.append({"role": "user", "content": question})
// Python: trajectory = []
for _, step := range range(10) {
// Python: resp = client.chat.completions.create(model="ai-4o", messages=messages)
// Python: thought = resp.choices[0].message.content
// Python: trajectory.append(thought)
if "finish" in thought.lower() {
return {"answer": thought, "trajectory": "\n".join(trajectory)}
// Python: messages.append({"role": "assistant", "content": thought})
return {"answer": thought, "trajectory": "\n".join(trajectory)}
func reflexion_agent() {
// Python: reflections = [] # 累积的反思经验
for _, trial := range range(max_trials) {
// 1. 执行任务(使用 ReAct + 历史反思作为额外上下文)
// Python: context = ""
if reflections {
// Python: context = f"\n之前的反思经验:\n" + "\n".join(reflections)
// Python: result = react_agent_with_context(question, context)
// 2. 评估结果质量
// Python: evaluation = evaluate_result(question, result)
if evaluation["score"] >= 0.8 {
return result
// 3. 反思推理过程
// Python: reflection = client.chat.completions.create(
// Python: model="ai-4o",
// Python: messages=[
// Python: {"role": "system", "content": REFLECT_PROMPT},
// Python: {"role": "user", "content":
// Python: f"之前的尝试:\n{result['trajectory']}\n\n"
// Python: f"评估反馈:\n{evaluation['feedback']}"}
// Python: ]
// Python: ).choices[0].message.content
// Python: reflections.append(reflection)
fmt.Println(f"Trial {trial+1}: score={evaluation['score']:.2f}")
fmt.Println(f"Reflection: {reflection[:100]}...")
return result # 达到最大尝试次数,返回最后结果
func evaluate_result() {
// Python: resp = client.chat.completions.create(
// Python: model="ai-4o",
// Python: messages=[
// Python: {"role": "system", "content":
// Python: "评估以下答案的质量。输出JSON: {\"score\": 0-1, \"feedback\": \"...\"}"},
// Python: {"role": "user", "content":
// Python: f"问题: {question}\n答案: {result['answer']}"}
// Python: ],
// Python: response_format={"type": "json_object"}
// Python: )
// import json
return json.loads(resp.choices[0].message.content)
// 使用
// Python: answer = reflexion_agent(
// Python: "解释量子纠缠的原理,并说明它与经典关联的区别"
// Python: )import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
import java.io.*;
// from openai import OpenAI
// Python: client = OpenAI()
// Python: REFLECT_PROMPT = """你刚刚完成了一个任务,但结果不够好。
// Python: 回顾你的推理过程和执行结果,找出问题所在。
// Python: 之前的尝试:
// Python: {trajectory}
// Python: 评估反馈:
// Python: {evaluation}
// Python: 请反思:
// Python: 1. 哪一步推理出了问题?
// Python: 2. 应该怎么做才更好?
// Python: 3. 下次尝试时需要注意什么?
// Python: 输出格式:
// Python: Reflection: 你的反思内容"""
public static void react_agent_with_context() {
// Python: messages = [{"role": "system", "content": REACT_PROMPT + context}]
// Python: messages.append({"role": "user", "content": question})
// Python: trajectory = []
for (var step : range(10)) {
// Python: resp = client.chat.completions.create(model="ai-4o", messages=messages)
// Python: thought = resp.choices[0].message.content
// Python: trajectory.append(thought)
if ("finish" in thought.lower()) {
return {"answer": thought, "trajectory": "\n".join(trajectory)};
// Python: messages.append({"role": "assistant", "content": thought})
return {"answer": thought, "trajectory": "\n".join(trajectory)};
public static void reflexion_agent() {
// Python: reflections = [] # 累积的反思经验
for (var trial : range(max_trials)) {
// 1. 执行任务(使用 ReAct + 历史反思作为额外上下文)
// Python: context = ""
if (reflections) {
// Python: context = f"\n之前的反思经验:\n" + "\n".join(reflections)
// Python: result = react_agent_with_context(question, context)
// 2. 评估结果质量
// Python: evaluation = evaluate_result(question, result)
if (evaluation["score"] >= 0.8) {
return result;
// 3. 反思推理过程
// Python: reflection = client.chat.completions.create(
// Python: model="ai-4o",
// Python: messages=[
// Python: {"role": "system", "content": REFLECT_PROMPT},
// Python: {"role": "user", "content":
// Python: f"之前的尝试:\n{result['trajectory']}\n\n"
// Python: f"评估反馈:\n{evaluation['feedback']}"}
// Python: ]
// Python: ).choices[0].message.content
// Python: reflections.append(reflection)
System.out.println(String.format("$1"));
System.out.println(String.format("$1"));
return result # 达到最大尝试次数,返回最后结果;
public static void evaluate_result() {
// Python: resp = client.chat.completions.create(
// Python: model="ai-4o",
// Python: messages=[
// Python: {"role": "system", "content":
// Python: "评估以下答案的质量。输出JSON: {\"score\": 0-1, \"feedback\": \"...\"}"},
// Python: {"role": "user", "content":
// Python: f"问题: {question}\n答案: {result['answer']}"}
// Python: ],
// Python: response_format={"type": "json_object"}
// Python: )
// import json
return json.loads(resp.choices[0].message.content);
// 使用
// Python: answer = reflexion_agent(
// Python: "解释量子纠缠的原理,并说明它与经典关联的区别"
// Python: )
}Reflexion 不只是"多做几遍"。它的关键是反思内容会累积——每轮尝试的反思都被保存下来,作为下一轮的额外上下文。这意味着 Agent 在"从失败中学习",而不是盲目重试。这就像人类做题:第一次做错,反思原因,第二次带着反思去做,大概率比第一次好。
ReAct 用 "Thought:" "Action:" 标记强制 LLM 输出特定格式,但文本标记解析极不稳定——LLM 可能输出 "Thought: ..." 或 "思考: ..." 或干脆不写标记。这是 Agent 开发中最头疼的问题之一。
结构化输出(Structured Output)就是为了彻底解决 LLM 输出不可靠的问题。
| 方式 | 约束强度 | 字段类型保证 | 必填项保证 | 适用场景 |
|---|---|---|---|---|
| JSON Mode | 弱(只保证是合法JSON) | ✗ 不保证 | ✗ 不保证 | 简单JSON输出 |
| Function Calling | 中(参数有Schema) | △ 部分保证 | △ 部分保证 | 工具调用 |
| Structured Output | 强(完整Schema约束) | ✓ 严格保证 | ✓ 严格保证 | Agent结构化推理 |
from pydantic import BaseModel, Field
from openai import OpenAI
from typing import List, Optional
client = OpenAI()
# ===== 1. 用 Pydantic 定义输出 Schema =====
class AgentStep(BaseModel):
"""Agent 单步推理的结构化输出"""
thought: str = Field(
description="对当前状态的推理分析"
)
action: str = Field(
description="要调用的工具名称",
enum=["search", "lookup", "calculate", "finish"]
)
action_input: str = Field(
description="工具的输入参数"
)
confidence: float = Field(
description="对当前推理的置信度 0-1",
ge=0, le=1
)
class AgentResponse(BaseModel):
"""Agent 完整响应的结构化输出"""
steps: List[AgentStep] = Field(
description="推理步骤列表"
)
final_answer: Optional[str] = Field(
description="最终答案(仅当 action=finish 时提供)",
default=None
)
needs_more_info: bool = Field(
description="是否需要更多信息才能回答"
)
# ===== 2. 用 OpenAI Structured Output 调用 =====
def structured_agent(question):
"""使用 Structured Output 的 Agent"""
response = client.beta.chat.completions.parse(
model="ai-4o-2024-08-06",
messages=[
{"role": "system", "content":
"你是一个研究助手。分析问题并给出推理步骤。"},
{"role": "user", "content": question}
],
response_format=AgentResponse # Pydantic 模型直接作为 Schema
)
# 3. 返回的是已验证的 Pydantic 对象
result = response.choices[0].message.parsed
# 类型安全!result.thought 是 str,result.confidence 是 float
for step in result.steps:
print(f"Thought: {step.thought}")
print(f"Action: {step.action}({step.action_input})")
print(f"Confidence: {step.confidence:.2f}")
if result.final_answer:
print(f"Answer: {result.final_answer}")
return result
# ===== 3. Pydantic 自动验证 =====
# 如果 LLM 输出的 confidence 超出 0-1 范围,Pydantic 会自动拒绝
# 如果 action 不是枚举值之一,Pydantic 会自动拒绝
# 如果 steps 是空列表,可以根据业务规则添加额外验证
def validate_agent_response(response: AgentResponse):
"""业务层面的额外验证"""
if not response.steps:
raise ValueError("Agent 必须至少输出一个推理步骤")
if response.needs_more_info and response.final_answer:
raise ValueError("标注需要更多信息时不应有最终答案")
if response.steps[-1].action == "finish" and not response.final_answer:
raise ValueError("finish 动作必须提供最终答案")
return True
# 使用
result = structured_agent("量子计算和经典计算的本质区别是什么?")
validate_agent_response(result)import {BaseModel, Field} from 'pydantic';
import OpenAI from 'openai';
// TypeScript has built-in types, no import needed for List, Optional
const client = OpenAI();
// ===== 1. 用 Pydantic 定义输出 Schema =====
class AgentStep {
/** docstring */
// thought: str = Field(
// description = "对当前状态的推理分析"
// )
// action: str = Field(
// description = "要调用的工具名称",
// enum = ["search", "lookup", "calculate", "finish"]
// )
// action_input: str = Field(
// description = "工具的输入参数"
// )
// confidence: float = Field(
// description = "对当前推理的置信度 0-1",
// ge = 0, le=1
// )
class AgentResponse {
/** docstring */
// steps: List[AgentStep] = Field(
// description = "推理步骤列表"
// )
// final_answer: Optional[str] = Field(
// description = "最终答案(仅当 action=finish 时提供)",
// default = None
// )
// needs_more_info: bool = Field(
// description = "是否需要更多信息才能回答"
// )
// ===== 2. 用 OpenAI Structured Output 调用 =====
function structured_agent(question) {
/** docstring */
// response = client.beta.chat.completions.parse(
// model = "ai-4o-2024-08-06",
// messages = [
// {"role": "system", "content":
// "你是一个研究助手。分析问题并给出推理步骤。"},
// {"role": "user", "content": question}
// ],
// response_format = AgentResponse # Pydantic 模型直接作为 Schema
// )
// 3. 返回的是已验证的 Pydantic 对象
// result = response.choices[0].message.parsed
// 类型安全!result.thought 是 str,result.confidence 是 float
for (const step of result.steps) {
console.log(`Thought: {step.thought}`);
console.log(`Action: {step.action}({step.action_input})`);
console.log(`Confidence: {step.confidence:.2f}`);
if (result.final_answer) {
console.log(`Answer: {result.final_answer}`);
return result;
// ===== 3. Pydantic 自动验证 =====
// 如果 LLM 输出的 confidence 超出 0-1 范围,Pydantic 会自动拒绝
// 如果 action 不是枚举值之一,Pydantic 会自动拒绝
// 如果 steps 是空列表,可以根据业务规则添加额外验证
function validate_agent_response(response: AgentResponse) {
/** docstring */
if (!response.steps) {
// raise ValueError("Agent 必须至少输出一个推理步骤")
if (response.needs_more_info && response.final_answer) {
// raise ValueError("标注需要更多信息时不应有最终答案")
if (response.steps[-1].action == "finish" && !response.final_answer) {
// raise ValueError("finish 动作必须提供最终答案")
return true;
// 使用
// result = structured_agent("量子计算和经典计算的本质区别是什么?")
// validate_agent_response(result)
}package main
import (
"fmt"
"os"
"os/exec"
"strings"
)
// from pydantic import BaseModel, Field
// from openai import OpenAI
// from typing import List, Optional
// Python: client = OpenAI()
// ===== 1. 用 Pydantic 定义输出 Schema =====
// AgentStep - CLI Agent class
type AgentStep struct {
// Python: thought: str = Field(
// Python: description="对当前状态的推理分析"
// Python: )
// Python: action: str = Field(
// Python: description="要调用的工具名称",
// Python: enum=["search", "lookup", "calculate", "finish"]
// Python: )
// Python: action_input: str = Field(
// Python: description="工具的输入参数"
// Python: )
// Python: confidence: float = Field(
// Python: description="对当前推理的置信度 0-1",
// Python: ge=0, le=1
// Python: )
// AgentResponse - CLI Agent class
type AgentResponse struct {
// Python: steps: List[AgentStep] = Field(
// Python: description="推理步骤列表"
// Python: )
// Python: final_answer: Optional[str] = Field(
// Python: description="最终答案(仅当 action=finish 时提供)",
// Python: default=None
// Python: )
// Python: needs_more_info: bool = Field(
// Python: description="是否需要更多信息才能回答"
// Python: )
// ===== 2. 用 OpenAI Structured Output 调用 =====
func structured_agent() {
// Python: response = client.beta.chat.completions.parse(
// Python: model="ai-4o-2024-08-06",
// Python: messages=[
// Python: {"role": "system", "content":
// Python: "你是一个研究助手。分析问题并给出推理步骤。"},
// Python: {"role": "user", "content": question}
// Python: ],
// Python: response_format=AgentResponse # Pydantic 模型直接作为 Schema
// Python: )
// 3. 返回的是已验证的 Pydantic 对象
// Python: result = response.choices[0].message.parsed
// 类型安全!result.thought 是 str,result.confidence 是 float
for _, step := range result.steps {
fmt.Println(f"Thought: {step.thought}")
fmt.Println(f"Action: {step.action}({step.action_input})")
fmt.Println(f"Confidence: {step.confidence:.2f}")
if result.final_answer {
fmt.Println(f"Answer: {result.final_answer}")
return result
// ===== 3. Pydantic 自动验证 =====
// 如果 LLM 输出的 confidence 超出 0-1 范围,Pydantic 会自动拒绝
// 如果 action 不是枚举值之一,Pydantic 会自动拒绝
// 如果 steps 是空列表,可以根据业务规则添加额外验证
func validate_agent_response() {
if not response.steps {
// Python: raise ValueError("Agent 必须至少输出一个推理步骤")
if response.needs_more_info and response.final_answer {
// Python: raise ValueError("标注需要更多信息时不应有最终答案")
if response.steps[-1].action == "finish" and not response.final_answer {
// Python: raise ValueError("finish 动作必须提供最终答案")
return true
// 使用
// Python: result = structured_agent("量子计算和经典计算的本质区别是什么?")
// Python: validate_agent_response(result)
}import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
import java.io.*;
// from pydantic import BaseModel, Field
// from openai import OpenAI
// from typing import List, Optional
// Python: client = OpenAI()
// ===== 1. 用 Pydantic 定义输出 Schema =====
public class AgentStep {
// Python: thought: str = Field(
// Python: description="对当前状态的推理分析"
// Python: )
// Python: action: str = Field(
// Python: description="要调用的工具名称",
// Python: enum=["search", "lookup", "calculate", "finish"]
// Python: )
// Python: action_input: str = Field(
// Python: description="工具的输入参数"
// Python: )
// Python: confidence: float = Field(
// Python: description="对当前推理的置信度 0-1",
// Python: ge=0, le=1
// Python: )
public class AgentResponse {
// Python: steps: List[AgentStep] = Field(
// Python: description="推理步骤列表"
// Python: )
// Python: final_answer: Optional[str] = Field(
// Python: description="最终答案(仅当 action=finish 时提供)",
// Python: default=None
// Python: )
// Python: needs_more_info: bool = Field(
// Python: description="是否需要更多信息才能回答"
// Python: )
// ===== 2. 用 OpenAI Structured Output 调用 =====
public static void structured_agent() {
// Python: response = client.beta.chat.completions.parse(
// Python: model="ai-4o-2024-08-06",
// Python: messages=[
// Python: {"role": "system", "content":
// Python: "你是一个研究助手。分析问题并给出推理步骤。"},
// Python: {"role": "user", "content": question}
// Python: ],
// Python: response_format=AgentResponse # Pydantic 模型直接作为 Schema
// Python: )
// 3. 返回的是已验证的 Pydantic 对象
// Python: result = response.choices[0].message.parsed
// 类型安全!result.thought 是 str,result.confidence 是 float
for (var step : result.steps) {
System.out.println(String.format("$1"));
System.out.println(String.format("$1"));
System.out.println(String.format("$1"));
if (result.final_answer) {
System.out.println(String.format("$1"));
return result;
// ===== 3. Pydantic 自动验证 =====
// 如果 LLM 输出的 confidence 超出 0-1 范围,Pydantic 会自动拒绝
// 如果 action 不是枚举值之一,Pydantic 会自动拒绝
// 如果 steps 是空列表,可以根据业务规则添加额外验证
public static void validate_agent_response() {
if (!response.steps) {
// Python: raise ValueError("Agent 必须至少输出一个推理步骤")
if (response.needs_more_info && response.final_answer) {
// Python: raise ValueError("标注需要更多信息时不应有最终答案")
if (response.steps[-1].action == "finish" && !response.final_answer) {
// Python: raise ValueError("finish 动作必须提供最终答案")
return true;
// 使用
// Python: result = structured_agent("量子计算和经典计算的本质区别是什么?")
// Python: validate_agent_response(result)
}
}ReAct 的致命问题是上下文膨胀。每一步 Thought + Action + Observation 都被追加到消息历史中,10步任务可能积累上千 token 的上下文。到第20步,LLM 可能因为上下文太长而"遗忘"最初的问题。
最粗暴:直接丢弃最早的 N 条消息。优点是简单,缺点是可能丢失关键信息。
用 LLM 将早期对话压缩成摘要。保留语义,但消耗额外 LLM 调用。
最精细:锁定核心指令,只压缩中间步骤。确保关键信息不丢失。
from openai import OpenAI
client = OpenAI()
def semantic_compress(messages, keep_recent=3):
"""语义保护型压缩:锁定核心指令,压缩中间步骤"""
# 1. 锁定:System Prompt 和原始问题
locked = messages[:2] # system + user question
# 2. 保留:最近K步(完整不压缩)
recent = messages[-(keep_recent * 3):] # 每步3条(thought+action+obs)
# 3. 压缩:中间所有步骤 → 一段摘要
middle = messages[2:-(keep_recent * 3)]
if len(middle) > 6: # 只有中间步骤足够多才压缩
summary = client.chat.completions.create(
model="ai-4o-mini", # 用小模型压缩,节省成本
messages=[
{"role": "system", "content":
"将以下对话历史压缩为简短摘要,保留关键决策和发现。"
"忽略冗余的推理过程,只保留结论性信息。"},
{"role": "user", "content":
f"对话历史:\n{format_messages(middle)}"}
]
).choices[0].message.content
compressed_middle = [
{"role": "system", "content": f"[历史摘要] {summary}"}
]
else:
compressed_middle = middle
# 4. 组合:锁定 + 压缩 + 保留
return locked + compressed_middle + recent
def format_messages(messages):
"""格式化消息列表为可读文本"""
text = ""
for msg in messages:
role = msg["role"]
content = msg["content"][:200] # 每条截断到200字
text += f"[{role}] {content}\n"
return text
# ===== Token 对比示例 =====
def compare_compression(original_msgs, compressed_msgs):
"""压缩前后 token 对比"""
def count_tokens(msgs):
# 粗略估算:1个中文字≈1.5token,1个英文词≈1token
total = 0
for msg in msgs:
content = msg["content"]
chinese_chars = sum(1 for c in content if '\u4e00' <= c <= '\u9fff')
english_words = len(content.split()) - chinese_chars
total += chinese_chars * 1.5 + english_words
return int(total)
orig_tokens = count_tokens(original_msgs)
comp_tokens = count_tokens(compressed_msgs)
ratio = comp_tokens / orig_tokens
print(f"原始 token: ~{orig_tokens}")
print(f"压缩 token: ~{comp_tokens}")
print(f"压缩率: {(1-ratio)*100:.1f}%")
return orig_tokens, comp_tokens
# ===== 实战数据 =====
# 假设一个20步 ReAct 任务:
# 原始消息数 = 2(system+question) + 20*3(steps) = 62条
# 原始 token ≈ 4000
# 压缩后消息数 = 2(锁定) + 1(摘要) + 3*3(保留) = 12条
# 压缩后 token ≈ 800
# 压缩率: 80% token节省!import OpenAI from 'openai';
const client = OpenAI();
function semantic_compress(messages, keep_recent=3) {
/** docstring */
// 1. 锁定:System Prompt 和原始问题
const locked = messages[:2] # system + user question;
// 2. 保留:最近K步(完整不压缩)
const recent = messages[-(keep_recent * 3):] # 每步3条(thought+action+obs);
// 3. 压缩:中间所有步骤 → 一段摘要
const middle = messages[2:-(keep_recent * 3)];
if (len(middle) > 6) {
const summary = client.chat.completions.create(;
const model = "ai-4o-mini", # 用小模型压缩,节省成本;
const messages = [;
// {"role": "system", "content":
// "将以下对话历史压缩为简短摘要,保留关键决策和发现。"
// "忽略冗余的推理过程,只保留结论性信息。"},
// {"role": "user", "content":
// f"对话历史:\n{format_messages(middle)}"}
// ]
// ).choices[0].message.content
const compressed_middle = [;
// {"role": "system", "content": f"[历史摘要] {summary}"}
// ]
} else {
const compressed_middle = middle;
// 4. 组合:锁定 + 压缩 + 保留
return locked + compressed_middle + recent;
function format_messages(messages) {
/** docstring */
const text = "";
for (const msg of messages) {
const role = msg["role"];
const content = msg["content"][:200] # 每条截断到200字;
// text += f"[{role}] {content}\n"
return text;
// ===== Token 对比示例 =====
function compare_compression(original_msgs, compressed_msgs) {
/** docstring */
function count_tokens(msgs) {
// 粗略估算:1个中文字≈1.5token,1个英文词≈1token
const total = 0;
for (const msg of msgs) {
const content = msg["content"];
const chinese_chars = sum(1 for c in content if '\u4e00' <= c <= '\u9fff');
const english_words = len(content.split()) - chinese_chars;
// total += chinese_chars * 1.5 + english_words
return int(total);
const orig_tokens = count_tokens(original_msgs);
const comp_tokens = count_tokens(compressed_msgs);
const ratio = comp_tokens / orig_tokens;
console.log(`原始 token: ~${$1}`);
console.log(`压缩 token: ~${$1}`);
console.log(`压缩率: {(1-ratio)*100:.1f}%`);
return orig_tokens, comp_tokens;
// ===== 实战数据 =====
// 假设一个20步 ReAct 任务:
// 原始消息数 = 2(system+question) + 20*3(steps) = 62条
// 原始 token ≈ 4000
// 压缩后消息数 = 2(锁定) + 1(摘要) + 3*3(保留) = 12条
// 压缩后 token ≈ 800
// 压缩率: 80% token节省!package main
import (
"fmt"
"os"
"os/exec"
"strings"
)
// from openai import OpenAI
// Python: client = OpenAI()
func semantic_compress() {
// 1. 锁定:System Prompt 和原始问题
// Python: locked = messages[:2] # system + user question
// 2. 保留:最近K步(完整不压缩)
// Python: recent = messages[-(keep_recent * 3):] # 每步3条(thought+action+obs)
// 3. 压缩:中间所有步骤 → 一段摘要
// Python: middle = messages[2:-(keep_recent * 3)]
if len(middle) > 6 {
// Python: summary = client.chat.completions.create(
// Python: model="ai-4o-mini", # 用小模型压缩,节省成本
// Python: messages=[
// Python: {"role": "system", "content":
// Python: "将以下对话历史压缩为简短摘要,保留关键决策和发现。"
// Python: "忽略冗余的推理过程,只保留结论性信息。"},
// Python: {"role": "user", "content":
// Python: f"对话历史:\n{format_messages(middle)}"}
// Python: ]
// Python: ).choices[0].message.content
// Python: compressed_middle = [
// Python: {"role": "system", "content": f"[历史摘要] {summary}"}
// Python: ]
} else {
// Python: compressed_middle = middle
// 4. 组合:锁定 + 压缩 + 保留
return locked + compressed_middle + recent
func format_messages() {
// Python: text = ""
for _, msg := range messages {
// Python: role = msg["role"]
// Python: content = msg["content"][:200] # 每条截断到200字
// Python: text += f"[{role}] {content}\n"
return text
// ===== Token 对比示例 =====
func compare_compression() {
func count_tokens() {
// 粗略估算:1个中文字≈1.5token,1个英文词≈1token
// Python: total = 0
for _, msg := range msgs {
// Python: content = msg["content"]
// Python: chinese_chars = sum(1 for c in content if '\u4e00' <= c <= '\u9fff')
// Python: english_words = len(content.split()) - chinese_chars
// Python: total += chinese_chars * 1.5 + english_words
return int(total)
// Python: orig_tokens = count_tokens(original_msgs)
// Python: comp_tokens = count_tokens(compressed_msgs)
// Python: ratio = comp_tokens / orig_tokens
fmt.Println(f"原始 token: ~{orig_tokens}")
fmt.Println(f"压缩 token: ~{comp_tokens}")
fmt.Println(f"压缩率: {(1-ratio)*100:.1f}%")
return orig_tokens, comp_tokens
// ===== 实战数据 =====
// 假设一个20步 ReAct 任务:
// 原始消息数 = 2(system+question) + 20*3(steps) = 62条
// 原始 token ≈ 4000
// 压缩后消息数 = 2(锁定) + 1(摘要) + 3*3(保留) = 12条
// 压缩后 token ≈ 800
// 压缩率: 80% token节省!import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
import java.io.*;
// from openai import OpenAI
// Python: client = OpenAI()
public static void semantic_compress() {
// 1. 锁定:System Prompt 和原始问题
// Python: locked = messages[:2] # system + user question
// 2. 保留:最近K步(完整不压缩)
// Python: recent = messages[-(keep_recent * 3):] # 每步3条(thought+action+obs)
// 3. 压缩:中间所有步骤 → 一段摘要
// Python: middle = messages[2:-(keep_recent * 3)]
if (len(middle) > 6) {
// Python: summary = client.chat.completions.create(
// Python: model="ai-4o-mini", # 用小模型压缩,节省成本
// Python: messages=[
// Python: {"role": "system", "content":
// Python: "将以下对话历史压缩为简短摘要,保留关键决策和发现。"
// Python: "忽略冗余的推理过程,只保留结论性信息。"},
// Python: {"role": "user", "content":
// Python: f"对话历史:\n{format_messages(middle)}"}
// Python: ]
// Python: ).choices[0].message.content
// Python: compressed_middle = [
// Python: {"role": "system", "content": f"[历史摘要] {summary}"}
// Python: ]
} else {
// Python: compressed_middle = middle
// 4. 组合:锁定 + 压缩 + 保留
return locked + compressed_middle + recent;
public static void format_messages() {
// Python: text = ""
for (var msg : messages) {
// Python: role = msg["role"]
// Python: content = msg["content"][:200] # 每条截断到200字
// Python: text += f"[{role}] {content}\n"
return text;
// ===== Token 对比示例 =====
public static void compare_compression() {
public static void count_tokens() {
// 粗略估算:1个中文字≈1.5token,1个英文词≈1token
// Python: total = 0
for (var msg : msgs) {
// Python: content = msg["content"]
// Python: chinese_chars = sum(1 for c in content if '\u4e00' <= c <= '\u9fff')
// Python: english_words = len(content.split()) - chinese_chars
// Python: total += chinese_chars * 1.5 + english_words
return int(total);
// Python: orig_tokens = count_tokens(original_msgs)
// Python: comp_tokens = count_tokens(compressed_msgs)
// Python: ratio = comp_tokens / orig_tokens
System.out.println(String.format("$1"));
System.out.println(String.format("$1"));
System.out.println(String.format("$1"));
return orig_tokens, comp_tokens;
// ===== 实战数据 =====
// 假设一个20步 ReAct 任务:
// 原始消息数 = 2(system+question) + 20*3(steps) = 62条
// 原始 token ≈ 4000
// 压缩后消息数 = 2(锁定) + 1(摘要) + 3*3(保留) = 12条
// 压缩后 token ≈ 800
// 压缩率: 80% token节省!
}| 指标 | 未压缩 | 截断 | 摘要压缩 | 语义保护型 |
|---|---|---|---|---|
| 消息条数 | 62 | 18 | 20 | 12 |
| 估算 Token | ~4000 | ~1200 | ~1000 | ~800 |
| 核心指令安全 | ✓ | ✗ 可能丢失 | △ 摘要可能遗漏 | ✓ 锁定保护 |
| 额外 LLM 调用 | 0 | 0 | 1(摘要生成) | 1(摘要生成) |
import re
from openai import OpenAI
client = OpenAI()
TOOLS = {"search": search_web, "lookup": lookup_keyword}
def react_agent(question, max_steps=10):
messages = [{"role": "system", "content": REACT_PROMPT}]
messages.append({"role": "user", "content": question})
for step in range(max_steps):
# 1. LLM 生成 Thought + Action
response = client.chat.completions.create(
model="ai-4o", messages=messages
)
output = response.choices[0].message.content
messages.append({"role": "assistant", "content": output})
# 2. 解析 Action
action_match = re.search(r'Action: (\w+)\((.*?)\)', output)
if not action_match:
break
tool_name = action_match.group(1)
tool_input = action_match.group(2).strip('"\'')
# 3. 检查是否完成
if tool_name == "finish":
return tool_input
# 4. 执行工具
if tool_name in TOOLS:
result = TOOLS[tool_name](tool_input)
else:
result = f"Error: tool {tool_name} not found"
# 5. 注入 Observation
messages.append({"role": "user", "content": f"Observation: {result}"})
return "达到最大步数限制"
# 使用
answer = react_agent("2024年诺贝尔物理学奖得主是谁?")import OpenAI from 'openai
const client = new OpenAI();
const TOOLS: Record<string, (input: string) => string> = {
search: searchWeb,
lookup: lookupKeyword,
};
const REACT_PROMPT = `...`; // ReAct 系统提示词
async function reactAgent(question: string, maxSteps = 10): Promise<string> {
const messages: any[] = [
{ role: "system", content: REACT_PROMPT },
{ role: "user", content: question },
];
for (let step = 0; step < maxSteps; step++) {
// 1. LLM 生成 Thought + Action
const response = await client.chat.completions.create({
model: "ai-4o",
messages,
});
const output = response.choices[0].message.content || "";
messages.push({ role: "assistant", content: output });
// 2. 解析 Action
const actionMatch = output.match(/Action: (\w+)\((.*?)\)/);
if (!actionMatch) break;
const toolName = actionMatch[1];
const toolInput = actionMatch[2].replace(/^["']|["']$/g, "");
// 3. 检查是否完成
if (toolName === "finish") return toolInput;
// 4. 执行工具
const tool = TOOLS[toolName];
const result = tool ? tool(toolInput) : `Error: tool ${toolName} not found`;
// 5. 注入 Observation
messages.push({ role: "user", content: `Observation: ${result}` });
}
return "达到最大步数限制";
}
// 使用
reactAgent("2024年诺贝尔物理学奖得主是谁?").then(console.log);package main
import (
"context"
"fmt"
"regexp"
"strings"
openai "github.com/sashabaranov/go-openai"
)
var tools = map[string]func(string) string{
"search": searchWeb,
"lookup": lookupKeyword,
}
func reactAgent(question string, maxSteps int) string {
client := openai.NewClient("") // API Key from env
messages := []openai.ChatCompletionMessage{
{Role: openai.ChatMessageRoleSystem, Content: reactPrompt},
{Role: openai.ChatMessageRoleUser, Content: question},
}
actionRe := regexp.MustCompile(`Action: (\w+)\((.*?)\)`)
for step := 0; step < maxSteps; step++ {
// 1. LLM 生成 Thought + Action
resp, err := client.CreateChatCompletion(context.Background(),
openai.ChatCompletionRequest{
Model:ai-4o",
Messages: messages,
})
if err != nil {
return err.Error()
}
output := resp.Choices[0].Message.Content
messages = append(messages, openai.ChatCompletionMessage{
Role: openai.ChatMessageRoleAssistant,
Content: output,
})
// 2. 解析 Action
matches := actionRe.FindStringSubmatch(output)
if matches == nil {
break
}
toolName := matches[1]
toolInput := strings.Trim(matches[2], `"'`)
// 3. 检查是否完成
if toolName == "finish" {
return toolInput
}
// 4. 执行工具
var result string
if tool, ok := tools[toolName]; ok {
result = tool(toolInput)
} else {
result = fmt.Sprintf("Error: tool %s not found", toolName)
}
// 5. 注入 Observation
messages = append(messages, openai.ChatCompletionMessage{
Role: openai.ChatMessageRoleUser,
Content: fmt.Sprintf("Observation: %s", result),
})
}
return "达到最大步数限制"
}import com.openai.client.OpenAIClient;
import com.openai.models.*;
import java.util.*;
import java.util.regex.*;
public class ReActAgent {
static OpenAIClient client = OpenAIClient.fromEnv();
static Map<String, java.util.function.Function<String, String>> tools = new HashMap<>();
static {
tools.put("search", ReActAgent::searchWeb);
tools.put("lookup", ReActAgent::lookupKeyword);
}
static final String REACT_PROMPT = "..."; // ReAct 系统提示词
public static String reactAgent(String question, int maxSteps) {
List<ChatCompletionMessageParam> messages = new ArrayList<>();
messages.add(ChatCompletionMessageParam.ofSystem(
ChatCompletionSystemMessageParam.builder()
.systemMessage(REACT_PROMPT).build()));
messages.add(ChatCompletionMessageParam.ofUser(
ChatCompletionUserMessageParam.builder()
.userMessage(question).build()));
Pattern actionRe = Pattern.compile("Action: (\\w+)\\((.*?)\\)");
for (int step = 0; step < maxSteps; step++) {
// 1. LLM 生成 Thought + Action
ChatCompletionCreateParams params = ChatCompletionCreateParams.builder()
.model("ai-4o")
.messages(messages)
.build();
ChatCompletion response = client.chat().completions().create(params);
String output = response.choices().get(0).message().content().orElse("");
messages.add(ChatCompletionMessageParam.ofAssistant(
ChatCompletionAssistantMessageParam.builder()
.assistantMessage(output).build()));
// 2. 解析 Action
Matcher m = actionRe.matcher(output);
if (!m.find()) break;
String toolName = m.group(1);
String toolInput = m.group(2).replaceAll("^[\"']|[\"']$", "");
// 3. 检查是否完成
if ("finish".equals(toolName)) return toolInput;
// 4. 执行工具
java.util.function.Function<String, String> tool = tools.get(toolName);
String result = tool != null ? tool.apply(toolInput)
: "Error: tool " + toolName + " not found";
// 5. 注入 Observation
messages.add(ChatCompletionMessageParam.ofUser(
ChatCompletionUserMessageParam.builder()
.userMessage("Observation: " + result).build()));
}
return "达到最大步数限制";
}
}核心就这几步:LLM 生成 → 解析 Action → 执行工具 → 注入 Observation → 循环。
本章介绍了 ReAct 的多个演进变体,下面是完整的 ReAct 家族演进图谱:
| 变体 | 改进点 | 核心机制 | 年份 |
|---|---|---|---|
| ReAct | 原始版:Thought-Action-Observation | 推理+行动交替循环 | 2022 |
| ReWOO | 先规划再执行,减少token消耗 | Planner→Worker→Solver | 2023 |
| LLM Compiler | 并行执行独立工具调用 | 依赖图(DAG)编译+并行 | 2023 |
| Reflexion | 加入自我反思,从失败中学习 | 执行→评估→反思→重试循环 | 2023 |
| LATS | 结合蒙特卡洛树搜索+ReAct | 树搜索+推理行动 | 2023 |
| Function Calling ReAct | 用原生 Function Calling 替代文本解析 | 结构化工具调用 | 2023+ |
前面我们学习了 ReAct、ReWOO、Reflexion 等推理模式,但它们都是概念模型。生产环境中,Agent 主循环需要解决一系列工程问题:烧钱怎么控制?死循环怎么检测?AI 输出被截断怎么办?接口超时怎么重试?本节基于 WaLiCode 项目的 streamingAgent.ts 真实实现,讲解生产级 Agent 主循环的六大工程机制。
WaLiCode 的 Agent 主循环支持两种模式,用对比表格展示差异:
| 维度 | Chat 模式 | Agent 模式 |
|---|---|---|
| 交互方式 | 一问一答,不自动续写 | 自动多轮,直到任务完成 |
| 最大轮次 | 1 轮 | 20 轮(可配置) |
| 工具调用 | 每轮最多 5 次 | 每轮最多 5 次,总计最多 200 次 |
| 重试次数 | 3 次 | 5 次(更宽容) |
| 适用场景 | 简单问答、闲聊 | 编码任务、文件操作、复杂工作流 |
| Token 预算 | 无全局限制 | 200k token 全局预算 |
为什么需要两种模式?如果用 Agent 模式处理简单问答,AI 可能会"过度思考"——明明一句话能回答的问题,它非要调几个工具验证一下,浪费 token 和时间。反过来,用 Chat 模式处理编码任务,AI 无法自动连续执行多步操作,用户体验很差。
// 伪代码:双模式入口
interface AgentLoopConfig {
mode: 'chat' | 'agent';
maxRounds: number; // chat: 1, agent: 20
maxToolCallsPerRound: number; // 默认 5
maxTotalToolCalls: number; // agent: 200
maxRetries: number; // chat: 3, agent: 5
tokenBudget: number; // agent: 200000
}
async function runAgentLoop(
messages: Message[],
config: AgentLoopConfig,
callbacks: StreamingCallbacks
) {
let round = 0;
let totalToolCalls = 0;
let totalTokens = 0;
while (round < config.maxRounds) {
// ... 主循环逻辑
round++;
}
} // // 伪代码:双模式入口
// interface AgentLoopConfig {
// mode: 'chat' | 'agent';
// maxRounds: number; // chat: 1, agent: 20
// maxToolCallsPerRound: number; // 默认 5
// maxTotalToolCalls: number; // agent: 200
// maxRetries: number; // chat: 3, agent: 5
// tokenBudget: number; // agent: 200000
// }
// async function runAgentLoop(
// messages: Message[],
// config: AgentLoopConfig,
// callbacks: StreamingCallbacks
// ) {
// let round = 0;
// let totalToolCalls = 0;
// let totalTokens = 0;
// while (round < config.maxRounds) {
// // ... 主循环逻辑
// round++;
// }
// }package main
import (
"fmt"
"os"
"os/exec"
"strings"
)
// Python: // 伪代码:双模式入口
// Python: interface AgentLoopConfig {
// Python: mode: 'chat' | 'agent';
// Python: maxRounds: number; // chat: 1, agent: 20
// Python: maxToolCallsPerRound: number; // 默认 5
// Python: maxTotalToolCalls: number; // agent: 200
// Python: maxRetries: number; // chat: 3, agent: 5
// Python: tokenBudget: number; // agent: 200000
// Python: }
// Python: async function runAgentLoop(
// Python: messages: Message[],
// Python: config: AgentLoopConfig,
// Python: callbacks: StreamingCallbacks
// Python: ) {
// Python: let round = 0;
// Python: let totalToolCalls = 0;
// Python: let totalTokens = 0;
// Python: while (round < config.maxRounds) {
// Python: // ... 主循环逻辑
// Python: round++;
// Python: }
// Python: }import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
import java.io.*;
// Python: // 伪代码:双模式入口
// Python: interface AgentLoopConfig {
// Python: mode: 'chat' | 'agent';
// Python: maxRounds: number; // chat: 1, agent: 20
// Python: maxToolCallsPerRound: number; // 默认 5
// Python: maxTotalToolCalls: number; // agent: 200
// Python: maxRetries: number; // chat: 3, agent: 5
// Python: tokenBudget: number; // agent: 200000
// Python: }
// Python: async function runAgentLoop(
// Python: messages: Message[],
// Python: config: AgentLoopConfig,
// Python: callbacks: StreamingCallbacks
// Python: ) {
// Python: let round = 0;
// Python: let totalToolCalls = 0;
// Python: let totalTokens = 0;
// Python: while (round < config.maxRounds) {
// Python: // ... 主循环逻辑
// Python: round++;
// Python: }
// Python: }
}Agent 模式下 AI 会自动多轮执行,如果不加限制,一个复杂任务可能消耗几十万 token——用户可能一觉醒来收到天价账单。WaLiCode 设计了全局 Token Budget 机制:
// 伪代码:Token Budget 管理
const GLOBAL_TOKEN_BUDGET = 200_000; // 20万 token 全局预算
let totalTokensAcrossRounds = 0;
while (round < maxRounds) {
// 检查全局预算
if (totalTokensAcrossRounds >= GLOBAL_TOKEN_BUDGET) {
callbacks.onText('\n⚠️ 已达到全局 Token 预算限制,自动停止。');
break;
}
// 流式接收 AI 输出
for await (const event of ai.stream(currentMessages)) {
if (event.type === 'text') {
const deltaTokens = countTokens(event.content);
totalTokensAcrossRounds += deltaTokens;
// ... 实时累加
}
}
round++;
} // // 伪代码:Token Budget 管理
// const GLOBAL_TOKEN_BUDGET = 200_000; // 20万 token 全局预算
// let totalTokensAcrossRounds = 0;
// while (round < maxRounds) {
// // 检查全局预算
// if (totalTokensAcrossRounds >= GLOBAL_TOKEN_BUDGET) {
// callbacks.onText('\n⚠️ 已达到全局 Token 预算限制,自动停止。');
// break;
// }
// // 流式接收 AI 输出
// for await (const event of ai.stream(currentMessages)) {
// if (event.type === 'text') {
// const deltaTokens = countTokens(event.content);
// totalTokensAcrossRounds += deltaTokens;
// // ... 实时累加
// }
// }
// round++;
// }package main
import (
"fmt"
"os"
"os/exec"
"strings"
)
// Python: // 伪代码:Token Budget 管理
// Python: const GLOBAL_TOKEN_BUDGET = 200_000; // 20万 token 全局预算
// Python: let totalTokensAcrossRounds = 0;
// Python: while (round < maxRounds) {
// Python: // 检查全局预算
// Python: if (totalTokensAcrossRounds >= GLOBAL_TOKEN_BUDGET) {
// Python: callbacks.onText('\n⚠️ 已达到全局 Token 预算限制,自动停止。');
// Python: break;
// Python: }
// Python: // 流式接收 AI 输出
// Python: for await (const event of ai.stream(currentMessages)) {
// Python: if (event.type === 'text') {
// Python: const deltaTokens = countTokens(event.content);
// Python: totalTokensAcrossRounds += deltaTokens;
// Python: // ... 实时累加
// Python: }
// Python: }
// Python: round++;
// Python: }import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
import java.io.*;
// Python: // 伪代码:Token Budget 管理
// Python: const GLOBAL_TOKEN_BUDGET = 200_000; // 20万 token 全局预算
// Python: let totalTokensAcrossRounds = 0;
// Python: while (round < maxRounds) {
// Python: // 检查全局预算
// Python: if (totalTokensAcrossRounds >= GLOBAL_TOKEN_BUDGET) {
// Python: callbacks.onText('\n⚠️ 已达到全局 Token 预算限制,自动停止。');
// Python: break;
// Python: }
// Python: // 流式接收 AI 输出
// Python: for await (const event of ai.stream(currentMessages)) {
// Python: if (event.type === 'text') {
// Python: const deltaTokens = countTokens(event.content);
// Python: totalTokensAcrossRounds += deltaTokens;
// Python: // ... 实时累加
// Python: }
// Python: }
// Python: round++;
// Python: }
}关键设计点:
Agent 模式下 AI 可能陷入死循环——反复调用同一个工具、传完全相同的参数、得到完全相同的错误结果。这不罕见:AI 看到 `FileNotFoundError`,于是去 `read_file`,文件确实不存在,于是又去 `write_file`,写错了路径,又看到 `FileNotFoundError`……
WaLiCode 的检测策略:连续 2 轮执行完全相同的失败工具调用时自动终止。
// 伪代码:死循环检测
let lastRoundToolCalls: string[] = [];
let lastRoundErrors: string[] = [];
while (round < maxRounds) {
const currentRoundToolCalls: string[] = [];
const currentRoundErrors: string[] = [];
// 执行工具调用...
for (const tc of toolCalls) {
const signature = `${tc.name}:${JSON.stringify(tc.args)}`;
currentRoundToolCalls.push(signature);
if (tc.status === 'error') {
currentRoundErrors.push(signature);
}
}
// 检测:连续 2 轮相同的失败工具调用
const sameErrors = currentRoundErrors.length > 0
&& currentRoundErrors.every(e => lastRoundErrors.includes(e))
&& currentRoundErrors.length === lastRoundErrors.length;
if (sameErrors) {
callbacks.onText('\n⚠️ 检测到重复失败,自动停止以避免死循环。');
break;
}
lastRoundToolCalls = currentRoundToolCalls;
lastRoundErrors = currentRoundErrors;
round++;
}为什么阈值设为 2 而不是 1?因为有些问题需要重试一次才能确认——比如网络抖动导致的临时失败,第二次重试可能就成功了。设为 2 是在"尽早发现死循环"和"允许合理重试"之间的平衡。
AI 模型有输出长度限制(如 4096 token)。当输出被截断时,finish_reason 会变成 'length' 而不是 'stop'。如果直接忽略,用户会看到半截代码或不完整的回答。WaLiCode 的方案是注入 "please continue" 自动恢复:
// 伪代码:截断恢复
for await (const event of ai.stream(currentMessages)) {
if (event.type === 'text') {
assistantContent += event.content;
}
if (event.type === 'finish') {
if (event.finish_reason === 'length') {
// 输出被 max_tokens 截断
wasTruncated = true;
}
}
}
if (wasTruncated && round < maxRounds) {
// 注入 "please continue" 让 AI 从截断处继续
currentMessages.push({ role: 'assistant', content: assistantContent });
currentMessages.push({ role: 'user', content: '请继续输出,从刚才截断的地方接着写。' });
assistantContent = ''; // 重置,新轮次会追加
wasTruncated = false;
continue; // 进入下一轮
} // // 伪代码:截断恢复
// for await (const event of ai.stream(currentMessages)) {
// if (event.type === 'text') {
// assistantContent += event.content;
// }
// if (event.type === 'finish') {
// if (event.finish_reason === 'length') {
// // 输出被 max_tokens 截断
const wasTruncated = true;;
// }
// }
// }
// if (wasTruncated && round < maxRounds) {
// // 注入 "please continue" 让 AI 从截断处继续
// currentMessages.push({ role: 'assistant', content: assistantContent });
// currentMessages.push({ role: 'user', content: '请继续输出,从刚才截断的地方接着写。' });
const assistantContent = ''; // 重置,新轮次会追加;
const wasTruncated = false;;
// continue; // 进入下一轮
// }package main
import (
"fmt"
"os"
"os/exec"
"strings"
)
// Python: // 伪代码:截断恢复
// Python: for await (const event of ai.stream(currentMessages)) {
// Python: if (event.type === 'text') {
// Python: assistantContent += event.content;
// Python: }
// Python: if (event.type === 'finish') {
// Python: if (event.finish_reason === 'length') {
// Python: // 输出被 max_tokens 截断
// Python: wasTruncated = true;
// Python: }
// Python: }
// Python: }
// Python: if (wasTruncated && round < maxRounds) {
// Python: // 注入 "please continue" 让 AI 从截断处继续
// Python: currentMessages.push({ role: 'assistant', content: assistantContent });
// Python: currentMessages.push({ role: 'user', content: '请继续输出,从刚才截断的地方接着写。' });
// Python: assistantContent = ''; // 重置,新轮次会追加
// Python: wasTruncated = false;
// Python: continue; // 进入下一轮
// Python: }import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
import java.io.*;
// Python: // 伪代码:截断恢复
// Python: for await (const event of ai.stream(currentMessages)) {
// Python: if (event.type === 'text') {
// Python: assistantContent += event.content;
// Python: }
// Python: if (event.type === 'finish') {
// Python: if (event.finish_reason === 'length') {
// Python: // 输出被 max_tokens 截断
// Python: wasTruncated = true;
// Python: }
// Python: }
// Python: }
// Python: if (wasTruncated && round < maxRounds) {
// Python: // 注入 "please continue" 让 AI 从截断处继续
// Python: currentMessages.push({ role: 'assistant', content: assistantContent });
// Python: currentMessages.push({ role: 'user', content: '请继续输出,从刚才截断的地方接着写。' });
// Python: assistantContent = ''; // 重置,新轮次会追加
// Python: wasTruncated = false;
// Python: continue; // 进入下一轮
// Python: }
}注意事项:
AI API 调用可能遇到临时性错误:429(限流)、503(服务不可用)、网络超时。直接报错会让用户重试,体验很差。WaLiCode 的方案是自动重试,指数退避:
// 伪代码:指数退避重试
const MAX_RETRIES = 5; // agent 模式
for (let attempt = 0; attempt <= MAX_RETRIES; attempt++) {
try {
// 尝试调用 AI API
for await (const event of ai.stream(currentMessages)) {
callbacks.onText(event.content, assistantContent);
}
break; // 成功,跳出重试循环
} catch (err) {
if (!isRetryable(err)) throw err; // 不可重试的错误直接抛出
if (attempt === MAX_RETRIES) throw err; // 重试次数用尽
// 指数退避:1s → 2s → 4s → 8s → 16s
const delayMs = Math.pow(2, attempt) * 1000;
callbacks.onText(`\n⏳ 遇到临时错误,${delayMs/1000}s 后重试...`);
await abortableSleep(delayMs);
}
}
function isRetryable(err: Error): boolean {
if (err instanceof HTTPError) {
return [429, 500, 502, 503, 504].includes(err.status);
}
return err instanceof NetworkError || err instanceof TimeoutError;
} // // 伪代码:指数退避重试
// const MAX_RETRIES = 5; // agent 模式
// for (let attempt = 0; attempt <= MAX_RETRIES; attempt++) {
// try {
// // 尝试调用 AI API
// for await (const event of ai.stream(currentMessages)) {
// callbacks.onText(event.content, assistantContent);
// }
// break; // 成功,跳出重试循环
// } catch (err) {
// if (!isRetryable(err)) throw err; // 不可重试的错误直接抛出
// if (attempt === MAX_RETRIES) throw err; // 重试次数用尽
// // 指数退避:1s → 2s → 4s → 8s → 16s
// const delayMs = Math.pow(2, attempt) * 1000;
// callbacks.onText(`\n⏳ 遇到临时错误,${delayMs/1000}s 后重试...`);
// await abortableSleep(delayMs);
// }
// }
// function isRetryable(err: Error): boolean {
// if (err instanceof HTTPError) {
return [429, 500, 502, 503, 504].includes(err.status);;
// }
return err instanceof NetworkError || err instanceof TimeoutError;;
// }package main
import (
"fmt"
"os"
"os/exec"
"strings"
)
// Python: // 伪代码:指数退避重试
// Python: const MAX_RETRIES = 5; // agent 模式
// Python: for (let attempt = 0; attempt <= MAX_RETRIES; attempt++) {
// Python: try {
// Python: // 尝试调用 AI API
// Python: for await (const event of ai.stream(currentMessages)) {
// Python: callbacks.onText(event.content, assistantContent);
// Python: }
// Python: break; // 成功,跳出重试循环
// Python: } catch (err) {
// Python: if (!isRetryable(err)) throw err; // 不可重试的错误直接抛出
// Python: if (attempt === MAX_RETRIES) throw err; // 重试次数用尽
// Python: // 指数退避:1s → 2s → 4s → 8s → 16s
// Python: const delayMs = Math.pow(2, attempt) * 1000;
// Python: callbacks.onText(`\n⏳ 遇到临时错误,${delayMs/1000}s 后重试...`);
// Python: await abortableSleep(delayMs);
// Python: }
// Python: }
// Python: function isRetryable(err: Error): boolean {
// Python: if (err instanceof HTTPError) {
return [429, 500, 502, 503, 504].includes(err.status);
// Python: }
return err instanceof NetworkError || err instanceof TimeoutError;
// Python: }import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
import java.io.*;
// Python: // 伪代码:指数退避重试
// Python: const MAX_RETRIES = 5; // agent 模式
// Python: for (let attempt = 0; attempt <= MAX_RETRIES; attempt++) {
// Python: try {
// Python: // 尝试调用 AI API
// Python: for await (const event of ai.stream(currentMessages)) {
// Python: callbacks.onText(event.content, assistantContent);
// Python: }
// Python: break; // 成功,跳出重试循环
// Python: } catch (err) {
// Python: if (!isRetryable(err)) throw err; // 不可重试的错误直接抛出
// Python: if (attempt === MAX_RETRIES) throw err; // 重试次数用尽
// Python: // 指数退避:1s → 2s → 4s → 8s → 16s
// Python: const delayMs = Math.pow(2, attempt) * 1000;
// Python: callbacks.onText(`\n⏳ 遇到临时错误,${delayMs/1000}s 后重试...`);
// Python: await abortableSleep(delayMs);
// Python: }
// Python: }
// Python: function isRetryable(err: Error): boolean {
// Python: if (err instanceof HTTPError) {
return [429, 500, 502, 503, 504].includes(err.status);;
// Python: }
return err instanceof NetworkError || err instanceof TimeoutError;;
// Python: }
}两个额外的保护机制:
Idle Timeout(空闲超时):10 分钟无活动自动停止。防止 Agent 在用户离开后继续烧钱。
const IDLE_TIMEOUT_MS = 10 * 60 * 1000; // 10 分钟
let lastActivityTime = Date.now();
// 在主循环中检查
if (Date.now() - lastActivityTime > IDLE_TIMEOUT_MS) {
callbacks.onText('\n⚠️ 空闲超时,自动停止。');
break;
}
// 每次收到事件时更新
for await (const event of ai.stream(...)) {
lastActivityTime = Date.now();
// ...
} // const IDLE_TIMEOUT_MS = 10 * 60 * 1000; // 10 分钟
// let lastActivityTime = Date.now();
// // 在主循环中检查
// if (Date.now() - lastActivityTime > IDLE_TIMEOUT_MS) {
// callbacks.onText('\n⚠️ 空闲超时,自动停止。');
// break;
// }
// // 每次收到事件时更新
// for await (const event of ai.stream(...)) {
const lastActivityTime = Date.now();;
// // ...
// }package main
import (
"fmt"
"os"
"os/exec"
"strings"
)
// Python: const IDLE_TIMEOUT_MS = 10 * 60 * 1000; // 10 分钟
// Python: let lastActivityTime = Date.now();
// Python: // 在主循环中检查
// Python: if (Date.now() - lastActivityTime > IDLE_TIMEOUT_MS) {
// Python: callbacks.onText('\n⚠️ 空闲超时,自动停止。');
// Python: break;
// Python: }
// Python: // 每次收到事件时更新
// Python: for await (const event of ai.stream(...)) {
// Python: lastActivityTime = Date.now();
// Python: // ...
// Python: }import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
import java.io.*;
// Python: const IDLE_TIMEOUT_MS = 10 * 60 * 1000; // 10 分钟
// Python: let lastActivityTime = Date.now();
// Python: // 在主循环中检查
// Python: if (Date.now() - lastActivityTime > IDLE_TIMEOUT_MS) {
// Python: callbacks.onText('\n⚠️ 空闲超时,自动停止。');
// Python: break;
// Python: }
// Python: // 每次收到事件时更新
// Python: for await (const event of ai.stream(...)) {
// Python: lastActivityTime = Date.now();
// Python: // ...
// Python: }
}413 反应式压缩:当 API 返回 413(请求体过大)时,紧急压缩上下文后重试。
try {
for await (const event of ai.stream(currentMessages)) {
// ...
}
} catch (err) {
if (err instanceof HTTPError && err.status === 413) {
// 请求体过大,紧急压缩上下文
const summary = await generateAiSummary(currentMessages);
currentMessages = [
{ role: 'system', content: '之前的对话已压缩:' + summary },
...recentMessages // 保留最近的几轮
];
// 用压缩后的上下文重试
continue;
}
throw err;
} // try {
// for await (const event of ai.stream(currentMessages)) {
// // ...
// }
// } catch (err) {
// if (err instanceof HTTPError && err.status === 413) {
// // 请求体过大,紧急压缩上下文
// const summary = await generateAiSummary(currentMessages);
const currentMessages = [;
// { role: 'system', content: '之前的对话已压缩:' + summary },
// ...recentMessages // 保留最近的几轮
// ];
// // 用压缩后的上下文重试
// continue;
// }
// throw err;
// }package main
import (
"fmt"
"os"
"os/exec"
"strings"
)
// Python: try {
// Python: for await (const event of ai.stream(currentMessages)) {
// Python: // ...
// Python: }
// Python: } catch (err) {
// Python: if (err instanceof HTTPError && err.status === 413) {
// Python: // 请求体过大,紧急压缩上下文
// Python: const summary = await generateAiSummary(currentMessages);
// Python: currentMessages = [
// Python: { role: 'system', content: '之前的对话已压缩:' + summary },
// Python: ...recentMessages // 保留最近的几轮
// Python: ];
// Python: // 用压缩后的上下文重试
// Python: continue;
// Python: }
// Python: throw err;
// Python: }import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
import java.io.*;
// Python: try {
// Python: for await (const event of ai.stream(currentMessages)) {
// Python: // ...
// Python: }
// Python: } catch (err) {
// Python: if (err instanceof HTTPError && err.status === 413) {
// Python: // 请求体过大,紧急压缩上下文
// Python: const summary = await generateAiSummary(currentMessages);
// Python: currentMessages = [
// Python: { role: 'system', content: '之前的对话已压缩:' + summary },
// Python: ...recentMessages // 保留最近的几轮
// Python: ];
// Python: // 用压缩后的上下文重试
// Python: continue;
// Python: }
// Python: throw err;
// Python: }
}413 压缩与正常压缩的区别:
双模式:Chat 简单高效,Agent 自动多轮,按场景选择避免浪费。
Token Budget:全局预算防止烧钱,实时累加及时停止。
死循环保护:连续 2 轮相同失败自动终止,阈值 2 平衡了灵敏度和容错。
截断恢复:max_tokens 截断时注入 continue 自动续写,有限次恢复避免无限循环。
指数退避:临时错误自动重试,2^n 秒退避避免雪崩,区分可重试和不可重试错误。
413 压缩:请求过大时紧急压缩重试,是正常压缩的兜底方案。
5.13 讲了主循环的工程化(Token Budget、死循环保护、重试机制)。但 ReAct 模式在生产环境中还有一个核心挑战:复杂任务的单线程执行效率低下。解决思路是引入子代理机制,让 ReAct 循环可以并行和委派。
Claude Code 基于 ReAct 模式运行(Thought → Action → Observation 循环),但在主循环之外增加了三个增强机制:子代理 Fork、验证代理、TodoWrite 任务管理。
标准 ReAct 是串行的:Thought → Action → Observation → Thought → …。如果任务中有独立的子任务(如“运行测试”和“检查类型”),串行执行浪费时间。
Claude Code 的增强方案:主代理在 Thought 阶段判断“这个子任务可以委派”,然后 Fork 一个子代理执行。子代理在后台运行,主代理继续处理其他任务。子代理完成后发通知,主代理在下一个 Thought 阶段整合结果。
标准 ReAct(串行):
Thought: 需要运行测试
Action: npm test
Observation: 测试结果
Thought: 需要检查类型
Action: tsc --noEmit
Observation: 类型检查结果
→ 2 次串行等待
增强 ReAct(Fork 并行):
Thought: 测试和类型检查互相独立,可以并行
Action: Fork 子代理 A(运行测试) + Fork 子代理 B(类型检查)
[主代理继续处理其他工作]
Notification: 子代理 A 完成(测试通过)
Notification: 子代理 B 完成(发现 2 个类型错误)
Thought: 整合结果,修复类型错误
→ 1 次并行等待
Fork 子代理后,主代理不能偷看(peek output_file)、不能编造(fabricate results)。如果用户问“子任务进展如何”而通知未到,只能回答“正在运行”。这保证了结果的可靠性。
ReAct 循环的一个常见问题是自我确认偏误:Agent 写了代码后,用自己的标准检查自己的代码,容易忽略自己的错误。
Claude Code 的规则:非平凡实现(3+ 文件修改或后端逻辑变更)必须由独立的验证代理进行对抗性验证。验证代理是 Fresh Subagent(零上下文启动),不继承主代理的分析和推理——以完全独立的视角审查代码变更。
| 维度 | 自我验证(标准 ReAct) | 验证代理(增强 ReAct) |
|---|---|---|
| 视角 | 主观(自己检查自己) | 客观(独立代理审查) |
| 上下文 | 继承完整推理链 | 零上下文(只看代码 diff) |
| 偏误风险 | ⚠️ 高(可能重复自己的错误逻辑) | ✅ 低(不受主代理推理影响) |
| 成本 | 低(无额外 API 调用) | 中(一次额外 LLM 调用) |
标准 ReAct 的 Thought 阶段是“自由推理”——LLM 自行决定下一步做什么。但在多步骤任务中,自由推理容易导致任务遗漏和优先级漂移。
Claude Code 在 ReAct 循环中集成了 TodoWrite 工具:
标准 ReAct 面试题:“ReAct 的优势和局限是什么”。如果你能回答“标准 ReAct 的局限是串行执行和自我确认偏误,工业级增强方案是 Fork 子代理并行 + Fresh 验证代理对抗性自查 + TodoWrite 任务管理”,基本能拿到高级工程师的 Offer。