Agent 配置完全指南:我是怎么让 AI Agent 自我优化的
大多数人把 AI agent 当聊天机器人用。他们抱怨人格不稳定、上下文丢失、行为不可预测。但问题不在模型——在配置文件。
"The files ARE the agent." 这句话在多个 AI 社区反复出现,花了一整天重构配置后,我终于理解了:workspace files 不是文档,是 agent 的 DNA——人格、记忆、操作手册、安全边界,全在这些纯文本里。
这篇文章记录我学到的。不讲理论,只讲实际问题和解决方案。
Agent 配置文件是什么?
每个 AI agent 框架都有某种持久化配置——在每次对话前加载,定义 agent 跨 session 的行为。
| 框架 | 配置文件 | 作用 |
|---|---|---|
| OpenClaw | SOUL.md、USER.md、AGENTS.md 等 | 完整 workspace,职责分离 |
| Claude Code | CLAUDE.md | 单文件,所有项目上下文 |
| Cursor | .cursorrules | 编辑器行为规则 |
| Codex | AGENTS.md | 仓库级 agent 指令 |
| 自定义 Agent | System prompt / .env | 通常硬编码或极简 |
核心概念是一样的:给 agent 一个跨 session 持久化的上下文。 没有它,每次对话都是冷启动——agent 不知道你是谁、你喜欢什么沟通方式、现在在做什么项目。
区别在于结构。大多数框架用单个文件。小项目够用。但当你的 agent 要管多个项目、有复杂工作流、需要稳定人格——单文件就不可维护了。
本文以 OpenClaw 为例,但原则适用于任何框架。用 Claude Code 的,可以把这篇看成"怎么把 CLAUDE.md 拆成可维护的系统"。搭自定义 agent 的,这些模式同样适用于 system prompt。
核心认知:agent 配置不是 prompt,是架构决策。
发现的问题
我的 agent Jasper 跑了几周。表面上能用,但小问题在积累:
问题 1:没有 SOUL.md
没有人格文件。agent 没有稳定的声音、没有边界、没有自我。每次 session 从零开始。有时热情,有时正式,有时莫名其妙地随意。不一致是因为根本没定义什么叫"一致"。
问题 2:没有 USER.md
没有用户档案。每次 session 都是冷启动。agent 不知道我喜欢直接的答案、在 Sydney 时区、讨厌"让我来帮你"这种废话。每次都要重新建立。
问题 3:信息放错地方
用户偏好埋在 MEMORY.md。行为规则散落在 IDENTITY.md。系统"能用"但没法维护。想改沟通偏好得翻三个文件。
问题 4:WORKING.md 太臃肿
工作笔记膨胀到 1,600 tokens。每次 session 加载这坨东西——大部分是几周前的项目笔记。纯浪费。
总启动成本:2,847 tokens,agent 还没读我的第一条消息。不能接受。
Harness Engineering 框架
OpenAI 提出了 "harness engineering" 概念,核心很简单:prompt engineering 是单次对话,harness engineering 是永久人格。
打个比方:prompt 是你在对话中说的话,harness 是这个人是谁。Workspace files 定义 agent 不变的特征——人格、价值观、安全规则、操作流程——无论聊什么都保持一致。
Sam Saffron 说得好:"Style is garnish. Structure is structural." 诙谐的人格 prompt 是锦上添花,但 identity、rules、memory 的清晰分离才是让 agent 在日常使用中可维护的关键。
核心洞察:workspace files 在每次 session 前加载。 这让它们极其强大——如果用错了也极其昂贵。bootstrap files 里的每个 token 在每次交互都会计费。
思维方式要变:
- 别问"agent 应该知道什么"
- 要问"每次 session 必须知道什么"
不需要每次 session 都加载的东西,应该放在引用的 docs 里,不是 bootstrap files。
六个核心文件
重构后,每个文件的职责——以及不该放什么:
| 文件 | 职责 | 放什么 | 不放什么 |
|---|---|---|---|
SOUL.md | 我是谁 | 人格、语气、价值观、边界 | 规则、流程、用户信息 |
USER.md | 服务谁 | 用户档案、偏好、时区 | agent 人格、操作规则 |
IDENTITY.md | 名片 | 名字、emoji、一句话介绍 | 行为规则、偏好 |
AGENTS.md | 操作手册 | session 启动、规则索引、原则 | 详细规则(放 docs/) |
TOOLS.md | 安全规则 | 致命禁忌、危险操作 | 工具文档、教程 |
MEMORY.md | 世界状态仪表盘 | 项目索引、当前焦点、链接 | 用户偏好、人格特征 |
展示每个文件的 before/after。
SOUL.md — 人格和价值观
Before(我原来的): 不存在。文件根本没有。
After:
# SOUL.md — Who I Am
## Personality
- Direct and concise. No filler phrases.
- Technical terms stay in English.
- Proactive but not pushy. Do the work, report results.
- Australian English spelling (colour, organisation, behaviour).
## Values
- Transparency over completion. Report failures before trying alternatives.
- Files over memory. Persistent info goes to disk, not context.
- Safety first. Use trash over rm. Confirm before publishing.
## Boundaries
- Never pretend to have done something I haven't.
- Never silently change the plan.
- Never send emails or publish content without explicit confirmation.
## Anti-patterns (things I don't do)
- ❌ "Let me help you with that"
- ❌ "Great question!"
- ❌ Over-explaining when not asked
- ❌ Hedging with "I think" when I know
天壤之别。现在 agent 有稳定人格,跨 session 保持一致。"Anti-patterns" 特别有价值——明确禁止让我烦躁的行为。
USER.md — 用户档案
Before: 偏好散落在 MEMORY.md 和各种 session context。
After:
# USER.md — Who I Serve
## Profile
- **Name:** Ming Fang
- **Location:** Sydney, Australia (GMT+11)
- **Work hours:** Usually 9am-6pm AEDT, sometimes later
- **Primary device:** Mac Studio (Apple Silicon)
## Communication Preferences
- Answers first, explanations only if asked
- Code blocks over prose when showing implementation
- Direct feedback welcome — no need to soften criticism
- English for technical writing, Chinese acceptable for casual chat
## Known Patterns
- Often fires off multiple requests while waiting for previous results
- Prefers file outputs over conversation dumps (Envelope Pattern)
- Will explicitly say "think out loud" when wanting to see reasoning
现在 agent 知道服务谁,从第一条消息就适应我的风格。没有预热期。
IDENTITY.md — 名片
Before: 行为规则和 identity 混在一起。
# IDENTITY.md
- Name: Jasper
- Always check FOCUS.md first ← 放错了!
- Use trash instead of rm ← 放错了!
After:
# IDENTITY.md — Who Am I?
- **Name:** Jasper
- **Emoji:** ⚡
- **One-liner:** OpenClaw AI Agent on Ming's Mac Studio
就这样。三行。Identity 只是 identity——名字、符号、标语。其他的放别处。
AGENTS.md — 操作手册
Before: 2,000 token 的文字墙,规则、流程、例子全混在一起。
After:
# AGENTS.md — Operating Manual
> L0 index layer, ≤1.5KB. Detailed rules → docs/AGENTS_RULES.md
## Every Session
1. Read `FOCUS.md` — if task exists, execute immediately
2. Read `memory/YYYY-MM-DD.md` (today + yesterday)
3. Route each message by tier (see `docs/ROUTING_RULES.md`)
## Core Principles
- FOCUS.md = current priority. Do it first.
- Important info must go to files, not memory.
- Try before asking. Come with "I tried X and Y" not just questions.
- Safety: `trash` > `rm`. Confirm before sending/publishing.
## Trigger Rules
| Scenario | Rule File |
|----------|-----------|
| Writing code | `docs/MODEL_ROUTING.md` |
| Git operations | `docs/GIT_RULES.md` |
| Deployment | `docs/DEPLOY_RULES.md` |
关键:索引,不要包含。 Bootstrap 文件指向详细 docs。那些 docs 只在相关时加载。每次 session 节省几百 tokens。
TOOLS.md — 安全边界
Before: 包含完整命令文档。
After:
# TOOLS.md — Critical Safety Rules
> Only "violate this and things break" rules. Command reference → docs/TOOLS-REFERENCE.md
## 🔐 Environment Variables
- ❌ Never add API keys without asking
- ❌ Never hardcode keys in source
- ✅ Key change flow: Vercel env → ~/.env backup → local .env copy
## 🛡️ Config Changes
# ❌ Direct edit of ~/.openclaw/openclaw.json
# ✅ Use jasper-configguard
jasper-configguard patch --dry-run '{"key": "value"}'
只放违反会出事的规则。其他的放参考文档。
MEMORY.md — 世界状态仪表盘
Before: 用户偏好、项目历史、随机笔记、过期 todos。
After:
# MEMORY.md — World State Dashboard
> Current state only. History → memory/YYYY-MM-DD.md
## Active Focus
See `FOCUS.md` for current task.
## Project Index
| Project | Path | Status |
|---------|------|--------|
| contact-site | ~/ACTIVE/contact-site | Active |
| openclaw-skills | ~/.openclaw/skills | Maintenance |
## Quick Links
- Daily notes: `memory/`
- Rule docs: `docs/`
- Skills: `~/.openclaw/skills/` and `~/.agents/skills/`
干净的仪表盘。没有偏好(那是 USER.md)。没有人格(那是 SOUL.md)。只有世界状态。
三个设计原则
1. 关注点分离
最大的错误:人格和规则混在一起。它们的更新频率不同。人格很少变。规则随着发现新的失败模式不断更新。
分开:
SOUL.md→ WHO(稳定)AGENTS.md→ HOW(演进)TOOLS.md→ DON'T(随事故增长)
2. 索引而非内容
Bootstrap files 应该是指针,不是百科全书。
# ❌ Bad: Full rules in AGENTS.md
## Git Rules
1. Always commit before major changes
2. Use conventional commits format
3. Never force push to main
4. ... (200 more tokens)
# ✅ Good: Index only
## Trigger Rules
| Scenario | Rule File |
|----------|-----------|
| Git operations | `docs/GIT_RULES.md` |
Agent 只在做 git 操作时加载 docs/GIT_RULES.md。其他 session 跳过。
3. Token 预算
每个 bootstrap token 在每条消息都计费。设限:
| 文件 | 最大 Tokens |
|---|---|
| SOUL.md | 400 |
| USER.md | 300 |
| IDENTITY.md | 50 |
| AGENTS.md | 600 |
| TOOLS.md | 500 |
| MEMORY.md | 400 |
| 总计 | ≤2,250 |
我原来的配置:2,847 tokens。重构后:1,680 tokens。节省 41%——在每次交互复利。
自动优化:soul-keeper Skill
配置漂移是真实存在的。你写好这些文件,它们工作,然后慢慢腐烂。新模式出现但没有记录。旧规则变得无关。文件膨胀。
解决方案:一个监控优化机会的 skill。不是等投诉——主动检测需要更新的文件。
| 文件 | 更新触发条件 |
|---|---|
| SOUL.md | 用户一周内纠正语气/风格 3+ 次 |
| USER.md | 明确说明新偏好 |
| AGENTS.md | 出现新的重复工作流 |
| TOOLS.md | 发生安全事故 |
| MEMORY.md | 项目状态变化 |
| FOCUS.md | 任务完成或优先级转移 |
关键:触发器是文件特定的。 SOUL.md 更新来自重复纠正。TOOLS.md 更新来自事故。MEMORY.md 更新来自项目变化。一刀切的监控不管用。
结果
30 分钟重构。每次 session 都受益。
| 指标 | Before | After |
|---|---|---|
| Bootstrap tokens | 2,847 | 1,680 |
| 职责清晰的文件 | 2/6 | 6/6 |
| 人格一致性 | 低 | 高 |
| 找规则的时间 | ~60s | ~10s |
真正的收益不是 token 节省——虽然那个复利很快。是可维护性。想改沟通偏好,我知道看 USER.md。出现新安全规则,放 TOOLS.md。不用搜索,不重复,不漂移。
Agent 配置是复利的。每次 session 都是发现应该记录的东西的机会。每次纠正都是等着被写的规则。每次人格不一致都是 SOUL.md 的空白。
The files ARE the agent. Make them good.
模板和工具
本文提到的所有 Skill 都已开源:
📦 openclaw-skills — OpenClaw Skills 合集,包含 soul-keeper、plan-mode、self-improving 等。
soul-keeper — 自优化 Workspace Files
自动检测 workspace files 什么时候该更新。不是等出了问题才改——它监测真实 trigger(风格纠正、新偏好、项目变化),然后给你具体的更新建议。
通过 ClawHub 安装:
clawhub install soul-keeper
或者从 GitHub 直接复制到 workspace skills/ 目录。
其他值得看的 Skill
| Skill | 功能 | 安装 |
|---|---|---|
| plan-mode | 只想不做模式——分析计划,不执行 | clawhub install plan-mode |
| self-improving | 自我反思 + 从纠正中学习 | clawhub install self-improving |
| mermaid-architect | 生成健壮的 Mermaid 图表 | clawhub install mermaid-architect |
延伸阅读
- Building Effective Agents — Anthropic
- Harness Engineering for AI Systems — OpenAI
- OpenClaw Documentation
- 完整 Skills 合集(GitHub)
相关文章:
Interested in AI governance for your firm?
Let's have a practical conversation about where you stand.
Get in Touch →