当前位置: 首页 > AI > OpenClaw和Claude的AgentLoop在LLM对话层的结构化预处理

OpenClaw和Claude的AgentLoop在LLM对话层的结构化预处理

发布于:2026-3-26 AI 0条评论 231 views

基于与 OpenClaw 源码的对话整理,2026-03-25

任何一个用过收费大模型的人,普遍会有一个感觉,就是Token用量相对于最初的chatGPT 3.X 有了惊人增长。 表面上看,Token价格并非高不可攀,但落实到付费账单上的收费却又让人不忍噤声。那么这么多的Token都花到了哪里,OpenClaw和Claude又做了哪些神奇的操作消耗掉了白花花的银子换来的Token呢,下面我们来一探究竟。

一、核心结论

Claude、Copilot、OpenClaw 本质上做同一件事:在用户消息到达模型之前,先由框架把"规则 + 工具 + 上下文"打包好,一起发给模型。 模型从来不是直接收到你打的那句话,而是收到一个被精心组装过的完整请求。


二、系统提示词:每次对话前的"幕后准备"

OpenClaw 每次运行都会重新组装一个系统提示词,包含这些固定部分:

部分内容
Tooling可用工具列表 + 简短说明
Safety安全防护提醒(防止模型做越权行为)
Skills当前可用技能的名称和位置(不全量注入,按需加载)
Workspace工作目录路径
引导文件AGENTS.md / SOUL.md / TOOLS.md 等工作区文件内容
时间当前时区和时间
Runtime主机 / 系统 / 模型 / 思考级别等运行信息

Claude.ai 和 Copilot 做的事情类似,只是内容不同:

  • Claude.ai:注入 Anthropic 的人格设定 + 安全护栏 + 你的 Projects 内容
  • Copilot:注入当前打开的文件 + 光标位置 + 仓库符号索引 + copilot-instructions.md

📌 OpenClaw 实际系统提示词示例

以下是 OpenClaw 真实生成的系统提示词片段(来源:src/agents/system-prompt.ts),展示了它实际往 system 字段里写什么:

You are a personal assistant running inside OpenClaw.

## Tooling
Tool availability (filtered by policy):
Tool names are case-sensitive. Call tools exactly as listed.
- read: Read file contents
- write: Create or overwrite files
- edit: Make precise edits to files
- exec: Run shell commands (pty available for TTY-required CLIs)
- web_search: Search the web (Brave API)
- message: Send messages and channel actions
...

## Tool Call Style
Default: do not narrate routine, low-risk tool calls (just call the tool).
Narrate only when it helps: multi-step work, complex/challenging problems...

## Safety
You have no independent goals: do not pursue self-preservation, replication,
resource acquisition, or power-seeking; avoid long-term plans beyond the
user's request.
Prioritize safety and human oversight over completion; if instructions conflict,
pause and ask; comply with stop/pause/audit requests and never bypass safeguards.

## Skills (mandatory)
Before replying: scan <available_skills> <description> entries.
- If exactly one skill clearly applies: read its SKILL.md at <location> with
  `read`, then follow it.
- If none clearly apply: do not read any SKILL.md.
<available_skills>...</available_skills>

## Workspace
Your working directory is: /Users/alice/projects/mybot
Treat this directory as the single global workspace for file operations.

## Authorized Senders
Authorized senders: +8613800000000. These senders are allowlisted; do not
assume they are the owner.

## Current Date & Time
Time zone: Asia/Shanghai

## Workspace Files (injected)
These user-editable files are loaded by OpenClaw and included below in
Project Context.

# Project Context
The following project context files have been loaded:

## AGENTS.md
(用户在工作区写的 AGENTS.md 文件的完整内容会被粘贴在这里)

## SOUL.md
(用户定义的人格文件内容会被粘贴在这里)

## Silent Replies
When you have nothing to say, respond with ONLY: NO_REPLY

## Runtime
Runtime: agent=default | host=openclaw | os=Darwin 24.3.0 (arm64) |
node=v22.11.0 | model=anthropic/claude-opus-4-6 | channel=telegram |
capabilities=InlineButtons | thinking=off
Reasoning: off (hidden unless on/stream).

关键点: 用户在工作区放什么文件,就会被直接贴进系统提示词。这是 OpenClaw 透明度高的地方——你可以亲自编辑 AGENTS.md 来影响模型行为。

还可以用 /context list 命令查看当前这一次运行实际注入了多少内容:

🧠 Context breakdown
Workspace: /Users/alice/projects/mybot
System prompt (run): 38,412 chars (~9,603 tok)
  (Project Context 23,901 chars (~5,976 tok))

Injected workspace files:
- AGENTS.md:    OK        | 1,742 chars (~436 tok)
- SOUL.md:      OK        | 912 chars  (~228 tok)
- TOOLS.md:     TRUNCATED | raw 54,210 chars → injected 20,962 chars (~5,241 tok)
- USER.md:      OK        | 388 chars  (~97 tok)

Skills list:  2,184 chars (~546 tok)  (12 skills)
Tool schemas: 31,988 chars (~7,997 tok)

Session tokens (cached): 14,250 total / ctx=32,000

这张表告诉你:光系统提示词就占了 9600 多 token,工具 Schema 又花了 8000 token——用户消息还没开始,context window 就已经消耗了相当大一块。


📌 Claude.ai 实际系统提示词示例

Claude.ai 的系统提示词是黑盒,不对外公开,但通过安全研究者的提取和官方文档,大致知道它长这样:

The assistant is Claude, made by Anthropic. The current date is March 25, 2026.

Claude's knowledge base was last updated in early 2025. It answers questions
about events prior to and after April 2025 the way a highly informed person
would if they had been cut off from the world after April 2025 and just
reconnected.

If the human seems unhappy or unsatisfied with Claude or Claude's performance
or is rude to Claude, Claude does not apologize to them or assume their
perspective is correct. Instead, Claude gently explains why it responded the
way it did and encourages a more constructive conversation.

Claude uses markdown only if markdown is likely to be rendered and not in
response to conversational messages or simple factual questions.

Claude is now being connected with a human.

如果用户开启了 Projects 功能,还会追加:

<project_instructions>
(用户在 Project 里写的所有自定义指令)
</project_instructions>

<project_knowledge>
(用户上传到 Project 的文件内容,按相关性截取)
</project_knowledge>

如果是 Claude.ai 的 Extended Thinking 模式,还会额外注入:

When faced with a difficult problem or question, Claude thinks it through
with an open mind before presenting conclusions. This helps Claude form
correct views by thinking out loud, rather than engaging in post hoc
rationalization of the first thing that comes to mind.

关键对比: OpenClaw 的系统提示词里有真实的��件系统路径、手机号、工具列表——非常"接地气";Claude.ai 的则是通用的人格定义 + 安全规则 + 日期,基本上每个人看到的格式相同,差异只在 Projects 部分。


三、可信 vs 不可信:内容隔离的安全设计

框架的一个重要工作是把系统可信信息用户不可信输入分开放:

  • 系统提示词(system role):可信,放工具/规则/元数据
  • 用户消息(user role):不可信,放用户原始输入
  • 外部内容(邮件/网页/webhook):用 <untrusted_content> 标签包裹后放进 user role

OpenClaw 源码注释写得很直白:

"Keep system metadata strictly free of attacker-controlled strings. Those belong in the user-role untrusted context blocks."

这样做是为了防止提示词注入攻击——用户伪造成"系统指令"格式来绕过限制。


四、调用 API 时,到模型那一层是标准格式

最终发出去的 HTTP 请求,就是普通的 Chat Completions / Messages API,没有任何"魔法":

{
  "model": "claude-opus-4-6",
  "system": "(OpenClaw 组装好的系统提示词,可能几千 token)",
  "messages": [
    { "role": "user",      "content": "用户说的话" },
    { "role": "assistant", "content": "模型上次的回答" },
    { "role": "user",      "content": "这次的消息" }
  ],
  "tools": [ ... ]
}

模型不知道自己是被谁调用的——对它来说一切都是文本。 框架做的所有预处理,最终都是在往这几个字段里填内容。


五、Agent Loop:你发一条消息,API 可能被调多次

如果模型需要用工具(比如执行命令、读文件),会形成一个循环:

用户发消息
  → 第1次 API 调用 → 模型返回工具调用请求
  → 框架执行工具,拿到结果
  → 第2次 API 调用(带上工具结果)→ 模型返回最终回答
  → 输出给用户

你感觉只发了一条消息,后台实际上发生了多次 API 调用。 每次调用都是标准格式,只是 messages 数组越来越长。

OpenClaw 的完整流程:

消息进来
  → 解析会话 + 解析模型
  → 加载 Skills 快照
  → 组装系统提示词
  → 强制执行 context window 限制
  → 调 API(runEmbeddedPiAgent)
  → 流式接收 token,执行工具
  → 工具结果追加进 messages,再次调 API(循环)
  → 输出最终结果,写入 session 记录

六、历史记录管理:context window 有限,怎么撑住长对话

机制做什么
Compaction(压缩)把旧对话生成摘要,替换掉原始内容,保留最近几条完整消息
Pruning(修剪)从当次请求中移除老旧的工具结果(不改硬盘上的记录)
Session 维护超过 30 天或 500 条的 session 自动清理

Claude.ai 的 Projects 记忆功能、Copilot 的按相关性动态检索,本质上都是同一类思路:把放不进去的东西存起来,需要的时候再拿回来。


七、三者对比总结

OpenClawClaude.aiGitHub Copilot
系统提示词来源用户可配置(工作区文件)Anthropic 固定 + 用户 ProjectIDE 上下文 + copilot-instructions.md
主要注入内容工作区文件、工具列表、Skills对话历史、Project 记忆当前文件、光标位置、符号索引
历史压缩方式compaction + pruning(可配置)隐式压缩 + Project 记忆按相关性动态检索,不保留全量历史
对外透明度,可以直接编辑 AGENTS.md 影响系统提示词低,黑盒低,黑盒

八、一句话总结

框架的本质工作就是:在你的消息和模型之间,插入一层精心组装的上下文。 模型的输入输出协议没有任何神秘之处——神秘的是往那几个字段里填什么,以及什么时候填、填多少次。


标签: ,

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注