【Agent封装 Prompt】openclaw的自带提示词是怎么写的,比如用户发一个需求过来,openclaw是怎么处理的?不需要加其它的提示词吗?OpenClaw如果任务不断失败,会无限循环吗?

AI 时代程序员必备技能

Codex、Claude Code、Cursor、Hermes Agent、OpenClaw等工程化实战专栏 ,讲透 AI 如何接管脏活累活

很多人以为 OpenClaw 只是把用户需求直接丢给大模型,其实不是。
它背后有一套 分层 Prompt(提示词系统),通常至少包含 4层提示

核心思想:

用户需求
↓
任务理解 Prompt
↓
行动规划 Prompt
↓
环境观察 Prompt
↓
动作生成 Prompt

所以并不是“只用一句提示词”。

下面给你拆开讲。


一、OpenClaw处理用户需求的完整流程

假设用户输入:

帮我在淘宝找10个蓝牙耳机,价格低于200元,并整理到Excel

OpenClaw的流程通常是:

User Request
↓
Task Planner(任务拆解)
↓
Environment Observer(环境观察)
↓
Action Generator(动作生成)
↓
Execute(执行)
↓
Loop

这其实是一个 Agent循环


二、第一层 Prompt:系统角色 Prompt(最重要)

OpenClaw会先给大模型一个 固定系统提示词

类似这样:

You are an AI computer operator.

You can control a computer by issuing actions such as:
- mouse_move(x,y)
- mouse_click()
- keyboard_type(text)
- open_application(name)

Your task is to accomplish the user's request by observing the screen and performing actions.

You must reason step by step.

Always follow this format:

Thought:
Action:

这层提示词定义了:

1️⃣ AI的角色
2️⃣ 可以使用的工具
3️⃣ 输出格式

这是 Agent的核心 Prompt


三、第二层 Prompt:任务规划 Prompt

收到用户需求后,系统通常会再加一层提示:

User request:
{用户输入}

Please break the task into smaller steps before acting.

例如:

用户任务:

找蓝牙耳机

LLM会输出:

Thought:
1. Open browser
2. Go to taobao.com
3. Search bluetooth headset
4. Filter price < 200
5. Collect product info
6. Open Excel
7. Write data

这就是 任务规划阶段


四、第三层 Prompt:环境观察 Prompt

每次操作前,OpenClaw都会提供:

Screen screenshot:
{当前屏幕截图}

Please analyze the current screen and decide the next action.

大模型会看到:

Google page
search box
buttons
links

然后推理:

Thought:
I should click the search box.

Action:
mouse_click(520,300)

五、第四层 Prompt:动作生成 Prompt

系统会限制动作格式:

例如:

Available actions:

click(x,y)
type(text)
scroll()
open_app(name)

Return only ONE action.

这样可以保证:

LLM输出
→ 机器可执行

例如:

Action:
type("bluetooth headset")

六、OpenClaw最关键的 Prompt结构

通常是这个格式:

SYSTEM PROMPT
↓
TOOLS DESCRIPTION
↓
USER REQUEST
↓
SCREEN STATE
↓
ACTION HISTORY

例如完整Prompt:

You are an AI computer operator.

Available actions:
click(x,y)
type(text)
scroll()

User request:
Find bluetooth earphones under 200 yuan.

Current screen:
[screen description]

Previous actions:
click(500,200)

What is the next action?

七、OpenClaw为什么不需要用户写提示词

因为系统已经预设了:

System Prompt
Tool Prompt
Action Prompt
Environment Prompt

用户只需要输入:

帮我做什么

就可以。

这其实是 Agent封装 Prompt


八、真正厉害的是 Prompt + 状态

OpenClaw不是只靠提示词。

还依赖:

State(状态)

每次循环都会带上:

任务目标
历史动作
当前屏幕
失败信息

例如:

Goal: find products
History: opened browser
Screen: search page

这样AI不会忘记任务。


九、如果你自己做一个OpenClaw

核心Prompt其实很简单。

基础版本:

You are an AI that controls a computer.

Goal:
{用户需求}

You can perform these actions:
click(x,y)
type(text)
scroll()

Screen:
{截图}

Previous actions:
{历史}

Decide the next action.

基本就能跑。


十、真正高级的OpenClaw Prompt

真正复杂的版本还会加:

1 反思 Prompt

Did the last action succeed?
If not, try another method.

2 错误恢复 Prompt

If you encounter errors, find alternative ways.

3 任务完成判断

Check if the task is completed.

十一、总结一句话

OpenClaw其实就是:

Prompt
+ Tool
+ Screen
+ Loop

核心循环:

Observe
Think
Act
Repeat

如果你愿意,我可以给你写一份 100行代码实现简化版 OpenClaw 的架构(程序员看了会非常清晰),包括:

  • Prompt设计
  • Agent循环
  • 屏幕解析
  • 动作执行

你会一眼看懂 Agent到底是怎么工作的

OpenClaw如果任务不断失败,会无限循环吗?直到token费用耗尽

不会。像 OpenClaw 这类 GUI-Agent 一般都会设计 多层停止机制(stop conditions),否则确实可能出现你说的情况:无限循环直到 token 或时间耗尽
成熟的实现通常会在 Agent循环外加“安全边界”。下面是常见的机制。


1 最基础:最大步骤限制(Step Limit)

几乎所有 Agent 都会设置:

max_steps = 20 或 50

流程:

for step in range(max_steps):
    Observe
    Think
    Act

超过就停止。

例如:

任务:订机票
最大步骤:30

30步还没完成
→ 强制终止

返回结果:

Task failed: step limit reached

这是 最重要的防无限循环机制


2 Token预算限制

很多系统会设:

max_tokens_per_task

例如:

50000 tokens

当达到预算:

stop

一些 Agent 框架(例如
LangChain 和
AutoGPT)
就支持这种 cost guardrail


3 连续失败检测

系统会记录:

失败次数

例如:

retry_count = 0

规则:

如果连续失败 > 5
→ 停止

例子:

click按钮
失败
click按钮
失败
click按钮
失败

连续失败:

> 5

系统就认为:

页面识别错误

然后退出。


4 动作重复检测(防死循环)

GUI Agent 很容易出现:

scroll
scroll
scroll
scroll
scroll

或者:

click同一个地方
click同一个地方
click同一个地方

所以会检测:

Action history

例如:

最近10步

如果发现:

同样动作重复

就会停止。


5 任务完成判断

系统通常还会有一个 Prompt:

Check if the task is finished.

例如:

Goal: 下载文件

AI判断:

文件已存在

就返回:

Task completed

而不是继续循环。


6 人类确认(高级系统)

一些 Agent 会要求:

关键步骤需要确认

例如:

是否付款?
是否删除文件?

例如 Claude Computer Use 就有这种机制。


7 超时机制

很多系统还会设:

max_runtime

例如:

10分钟

超过:

强制结束

8 实际系统通常是“五重保险”

成熟 Agent 一般同时有:

1 max_steps
2 token_limit
3 failure_limit
4 repeat_action_detection
5 timeout

只要触发任何一个:

停止

9 如果不加这些限制会发生什么

早期 AutoGPT 就经常出现:

AI思考
AI重新思考
AI再思考

无限循环:

"I should improve the plan"
"I should improve the plan"
"I should improve the plan"

最后:

花掉几十美元 token

什么也没完成。

这也是早期 Agent 被吐槽的原因。


10 真正更高级的解决方案(现在很多人在研究)

不是简单限制循环,而是:

让AI知道自己失败了。

例如增加:

Reflection Prompt

类似:

The previous attempt failed.
Analyze why it failed and change strategy.

这叫:

Self Reflection Agent

很多新系统在做,例如:

  • CrewAI
  • Open Interpreter

总结一句话

OpenClaw不会无限循环,因为通常会设置:

step limit
+ token budget
+ failure detection
+ repeat detection
+ timeout

只要触发一个就停止。


如果你是程序员,其实还有一个 更关键的问题

为什么现在很多 AI Agent 会“卡死”在循环里?

如何让AI来自主决定什么时候放弃?

让 AI 自主决定什么时候放弃,本质上是一个 “自我评估 + 资源管理”问题。在像 OpenClaw 这种 Agent 中,一般不是只靠固定限制(step limit),而是让模型在循环里 评估进展、成本、成功概率。下面是几种常见且有效的方法。


1 让 AI 定期进行“进度评估”(Progress Check)

在 Agent 循环中,每隔几步让模型回答一个问题:

Goal: {用户目标}

Current progress:
{已完成步骤}

Question:
1. Is the goal closer to completion?
2. Are we making meaningful progress?
3. If not, should we change strategy or stop?

Respond:
CONTINUE / CHANGE_STRATEGY / GIVE_UP

示例:

Goal: 找10个蓝牙耳机并整理Excel

Progress:
- 打开浏览器
- 打开淘宝
- 搜索成功
- 但无法复制商品信息

AI判断:
成功概率很低

Decision: CHANGE_STRATEGY

如果多次评估仍无进展:

Decision: GIVE_UP

2 引入“成功概率估计”

可以要求模型估计:

Probability of success (0–100%)

例如:

Progress evaluation:

Success probability: 25%
Reason:
无法识别网页结构
页面加载异常

然后设一个规则:

如果 success_probability < 30%
并且连续3次
→ 放弃

这样 AI 就会主动退出。


3 引入“成本意识”(Token / 时间预算)

让 AI 在 Prompt 中看到:

Remaining budget:
Tokens left: 8000
Time left: 3 minutes
Steps left: 8

然后要求它思考:

Can the task realistically be finished within the remaining budget?

如果答案是:

No

就退出。

很多 Agent(如 AutoGPTLangChain)都开始加入这种预算感知。


4 失败模式检测(Failure Pattern)

系统可以记录:

action_history
error_history

例如:

click(500,200) → error
click(500,200) → error
click(500,200) → error

然后让 AI 分析:

Repeated failure detected.

Question:
Is this approach likely to succeed if repeated?

Answer:
NO
Decision:
GIVE_UP

5 引入“反思 Prompt”(Self-Reflection)

这是目前 Agent 研究里非常重要的一种方法。

每隔几步加入:

Reflection:

What has been tried?
Why did it fail?
What alternative strategy exists?

Options:
1 Continue
2 Change strategy
3 Abort task

这种方式在多 Agent 框架(例如 CrewAI)中非常常见。


6 设置“价值判断”

AI可以评估:

Expected Value = 成功概率 × 任务价值

例如:

成功概率:20%
任务价值:低
成本:高

结论:

Expected value very low
→ GIVE_UP

这种方式其实接近 强化学习里的策略评估


7 一个比较完整的 Prompt 示例

You are an autonomous agent executing a task.

Goal:
{用户目标}

Current state:
{环境状态}

Action history:
{最近动作}

Remaining resources:
Steps left: {n}
Token budget: {m}

Evaluate:

1. Are we making progress toward the goal?
2. Estimate probability of success (0-100%).
3. If probability < 30% and no progress for 3 steps, abort.

Respond in JSON:

{
  "decision": "continue | change_strategy | give_up",
  "success_probability": number,
  "reason": ""
}

8 实际系统通常会结合三层机制

成熟 Agent 一般是:

AI判断是否继续
+ 系统硬限制
+ 失败模式检测

也就是:

soft decision (AI)
+
hard guardrail (系统)

这样既有灵活性,又不会无限循环。


核心思想其实很简单:

让 AI 不只是“执行者”,而是 任务经理(Task Manager),它需要持续回答三个问题:

任务是否有进展?
成功概率还有多高?
剩余资源是否足够?

当三个答案都变差时,它就会主动放弃。


如果你愿意,我可以再讲一个 Agent设计里非常关键但很多人忽略的问题

为什么很多 AI Agent 明明已经失败,却还在“自信地继续执行”?

这个其实和 LLM 的训练方式(RLHF) 有很深的关系。

AI 时代程序员必备技能

Codex、Claude Code、Cursor、Hermes Agent、OpenClaw等工程化实战专栏 ,讲透 AI 如何接管脏活累活

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小黄人软件

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值