agent-loops
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseAgent Loops
Agent循环
Enable LLMs to reason, plan, and take autonomous actions.
让LLM能够进行推理、规划并执行自主操作。
ReAct Pattern (Reasoning + Acting)
ReAct模式(推理+行动)
python
REACT_PROMPT = """You are an agent that reasons step by step.
For each step, respond with:
Thought: [your reasoning about what to do next]
Action: [tool_name(arg1, arg2)]
Observation: [you'll see the result here]
When you have the final answer:
Thought: I now have enough information
Final Answer: [your response]
Available tools: {tools}
Question: {question}
"""
async def react_loop(question: str, tools: dict, max_steps: int = 10) -> str:
"""Execute ReAct reasoning loop."""
history = REACT_PROMPT.format(tools=list(tools.keys()), question=question)
for step in range(max_steps):
response = await llm.chat([{"role": "user", "content": history}])
history += response.content
# Check for final answer
if "Final Answer:" in response.content:
return response.content.split("Final Answer:")[-1].strip()
# Extract and execute action
if "Action:" in response.content:
action = parse_action(response.content)
result = await tools[action.name](*action.args)
history += f"\nObservation: {result}\n"
return "Max steps reached without answer"python
REACT_PROMPT = """You are an agent that reasons step by step.
For each step, respond with:
Thought: [your reasoning about what to do next]
Action: [tool_name(arg1, arg2)]
Observation: [you'll see the result here]
When you have the final answer:
Thought: I now have enough information
Final Answer: [your response]
Available tools: {tools}
Question: {question}
"""
async def react_loop(question: str, tools: dict, max_steps: int = 10) -> str:
"""Execute ReAct reasoning loop."""
history = REACT_PROMPT.format(tools=list(tools.keys()), question=question)
for step in range(max_steps):
response = await llm.chat([{"role": "user", "content": history}])
history += response.content
# Check for final answer
if "Final Answer:" in response.content:
return response.content.split("Final Answer:")[-1].strip()
# Extract and execute action
if "Action:" in response.content:
action = parse_action(response.content)
result = await tools[action.name](*action.args)
history += f"\nObservation: {result}\n"
return "Max steps reached without answer"Plan-and-Execute Pattern
规划-执行模式
python
async def plan_and_execute(goal: str) -> str:
"""Create plan first, then execute steps."""
# 1. Generate plan
plan = await llm.chat([{
"role": "user",
"content": f"Create a step-by-step plan to: {goal}\n\nFormat as numbered list."
}])
steps = parse_plan(plan.content)
results = []
# 2. Execute each step
for i, step in enumerate(steps):
result = await execute_step(step, context=results)
results.append({"step": step, "result": result})
# 3. Check if replanning needed
if should_replan(results):
return await plan_and_execute(
f"{goal}\n\nProgress so far: {results}"
)
# 4. Synthesize final answer
return await synthesize(goal, results)python
async def plan_and_execute(goal: str) -> str:
"""Create plan first, then execute steps."""
# 1. 生成规划
plan = await llm.chat([{
"role": "user",
"content": f"Create a step-by-step plan to: {goal}\n\nFormat as numbered list."
}])
steps = parse_plan(plan.content)
results = []
# 2. 执行每个步骤
for i, step in enumerate(steps):
result = await execute_step(step, context=results)
results.append({"step": step, "result": result})
# 3. 检查是否需要重新规划
if should_replan(results):
return await plan_and_execute(
f"{goal}\n\nProgress so far: {results}"
)
# 4. 合成最终答案
return await synthesize(goal, results)Self-Correction Loop
自我修正循环
python
async def self_correcting_agent(task: str, max_retries: int = 3) -> str:
"""Agent that validates and corrects its own output."""
for attempt in range(max_retries):
# Generate response
response = await llm.chat([{
"role": "user",
"content": task
}])
# Self-validate
validation = await llm.chat([{
"role": "user",
"content": f"""Validate this response for the task: {task}
Response: {response.content}
Check for:
1. Correctness - Is it factually accurate?
2. Completeness - Does it fully answer the task?
3. Format - Is it properly formatted?
If valid, respond: VALID
If invalid, respond: INVALID: [what's wrong and how to fix]"""
}])
if "VALID" in validation.content:
return response.content
# Correct based on feedback
task = f"{task}\n\nPrevious attempt had issues: {validation.content}"
return response.content # Return best attemptpython
async def self_correcting_agent(task: str, max_retries: int = 3) -> str:
"""Agent that validates and corrects its own output."""
for attempt in range(max_retries):
# 生成响应
response = await llm.chat([{
"role": "user",
"content": task
}])
# 自我验证
validation = await llm.chat([{
"role": "user",
"content": f"""Validate this response for the task: {task}
Response: {response.content}
Check for:
1. Correctness - Is it factually accurate?
2. Completeness - Does it fully answer the task?
3. Format - Is it properly formatted?
If valid, respond: VALID
If invalid, respond: INVALID: [what's wrong and how to fix]"""
}])
if "VALID" in validation.content:
return response.content
# 根据反馈修正
task = f"{task}\n\nPrevious attempt had issues: {validation.content}"
return response.content # 返回最佳尝试结果Memory Management
内存管理
python
class AgentMemory:
"""Sliding window memory for agents."""
def __init__(self, max_messages: int = 20):
self.messages = []
self.max_messages = max_messages
self.summary = ""
def add(self, role: str, content: str):
self.messages.append({"role": role, "content": content})
# Summarize old messages when window full
if len(self.messages) > self.max_messages:
self._compress()
def _compress(self):
"""Summarize oldest messages."""
old = self.messages[:10]
self.messages = self.messages[10:]
# Async summarize would be better
summary = summarize(old)
self.summary = f"{self.summary}\n{summary}"
def get_context(self) -> list:
"""Get messages with summary prefix."""
context = []
if self.summary:
context.append({
"role": "system",
"content": f"Previous context summary: {self.summary}"
})
return context + self.messagespython
class AgentMemory:
"""Sliding window memory for agents."""
def __init__(self, max_messages: int = 20):
self.messages = []
self.max_messages = max_messages
self.summary = ""
def add(self, role: str, content: str):
self.messages.append({"role": role, "content": content})
# 当窗口已满时总结旧消息
if len(self.messages) > self.max_messages:
self._compress()
def _compress(self):
"""总结最早的消息。"""
old = self.messages[:10]
self.messages = self.messages[10:]
# 异步总结会更好
summary = summarize(old)
self.summary = f"{self.summary}\n{summary}"
def get_context(self) -> list:
"""获取带总结前缀的消息。"""
context = []
if self.summary:
context.append({
"role": "system",
"content": f"Previous context summary: {self.summary}"
})
return context + self.messagesKey Decisions
关键决策
| Decision | Recommendation |
|---|---|
| Max steps | 5-15 (prevent infinite loops) |
| Temperature | 0.3-0.7 (balance creativity/focus) |
| Memory window | 10-20 messages |
| Validation | Every 3-5 steps |
| 决策项 | 建议 |
|---|---|
| 最大步骤数 | 5-15(防止无限循环) |
| 温度参数 | 0.3-0.7(平衡创意与专注度) |
| 内存窗口 | 10-20条消息 |
| 验证频率 | 每3-5步一次 |
Common Mistakes
常见误区
- No step limit (infinite loops)
- No memory management (context overflow)
- No error recovery (crashes on tool failure)
- Over-complex prompts (agent gets confused)
- 未设置步骤限制(无限循环)
- 未进行内存管理(上下文溢出)
- 无错误恢复机制(工具调用失败时崩溃)
- 提示词过于复杂(Agent产生困惑)
Related Skills
相关技能
- - Tool definitions and execution
function-calling - - Coordinating multiple agents
multi-agent-orchestration - - Stateful agent graphs
langgraph-workflows
- - 工具定义与执行
function-calling - - 多Agent协调
multi-agent-orchestration - - 有状态Agent图
langgraph-workflows
Capability Details
能力细节
react-loop
react-loop
Keywords: react, reason, act, observe, loop
Solves:
- Implement ReAct pattern
- Create reasoning loops
- Build iterative agents
关键词: react, reason, act, observe, loop
解决问题:
- 实现ReAct模式
- 创建推理循环
- 构建迭代式Agent
tool-use
tool-use
Keywords: tool, function, call, execution
Solves:
- Implement tool calling
- Execute functions from LLM
- Parse tool responses
关键词: tool, function, call, execution
解决问题:
- 实现工具调用
- 从LLM执行函数
- 解析工具响应
workflow-template
workflow-template
Keywords: template, workflow, agent, typescript
Solves:
- Agent workflow template
- TypeScript implementation
- Copy-paste starter
关键词: template, workflow, agent, typescript
解决问题:
- Agent工作流模板
- TypeScript实现
- 可复制的启动模板