ChatGPT、Claude 这类对话式 AI 让我们习惯了"你一句我一句"。但 Claude Code 和 OpenAI Codex CLI 是Agentic Coding 环境——你描述目标,它们自己读文件、跑命令、改代码、debug。听起来很爽,但用久了就会发现一个尴尬的真相:最贵的不是模型调用,而是被你浪费掉的上下文窗口。
Anthropic 官方把这一点挑得很明:
“Most best practices are based on one constraint: Claude’s context window fills up fast, and performance degrades as it fills.”
上下文窗口就像你雇了一位记忆力有限的工程师。前 20 轮对话它记得清清楚楚;到第 80 轮,你早期叮嘱的"不要改这个文件"可能已经被遗忘了。于是 AI 开始重复犯错、重新探索你已经解释过的东西、给出越来越不稳的答案。
“偷懒"其实不是让 AI 少干活,而是让你花的每一分钱都产生复利:一次配置,长期复用;一次验证,自动巡航;一次清晰的指令,少三轮返工。
这篇文章从我们实际使用 Claude Code 和 Codex CLI 的经验出发,总结 6 个真正能省 token、省时间、省血压的技巧。
一、给 AI 写一本"上岗手册”:CLAUDE.md / AGENTS.md
如果你每次开新会话都要重复"我们用 ESLint、测试跑 npm test、提交前要打 tag",那你已经在浪费了。
Claude Code 会读取项目根目录下的 CLAUDE.md,Codex CLI 则会读 AGENTS.md。它们相当于给 AI 的入职培训手册,放在代码仓库里还能和团队共享。
但不要把它写成维基百科。Claude 官方对 CLAUDE.md 的取舍建议非常直白:
| ✅ 应该写 | ❌ 不要写 |
|---|---|
| AI 自己猜不到的 Bash 命令 | 标准语言规范(AI 本来就知道) |
| 项目特有的代码风格 | 长篇 API 文档 |
| 测试命令和偏好的 runner | 逐文件描述代码库 |
| 环境变量、密钥配置、分支命名规则 | “写干净代码"这种废话 |
| 常见坑和非显而易见的约定 | 频繁变动的信息 |
一个够用的 CLAUDE.md 通常只有 100~300 行。原则是:删掉任何一行,如果 AI 依然不会做错,就删。Bloated 的上下文不如没有上下文。
# CLAUDE.md
## Commands
- Type check: `npm run typecheck`
- Run single test: `npm test -- --grep "pattern"` (prefer single tests over full suite)
- Build: `npm run build`
## Code style
- Use ES modules, not CommonJS
- Prefer destructuring imports
- Use `const` until proven otherwise
## Workflow
- Create a failing test before fixing a bug
- Run lint and typecheck before committing
- Don't modify files outside the requested scope
Codex CLI 的 AGENTS.md 格式类似。关键是:每次新会话自动加载,而不是你每次口述。
写完可以用 /context(Claude)或 codex --context 检查 AI 是否真的加载了。
二、把线索给足:具体 prompt 是最高效的省钱方式
模糊的指令不是省字数,而是 expensive 的返工启动器。
Claude Code 官方举过一个典型例子:
| 低效 prompt | 高效 prompt |
|---|---|
| implement a function that validates email addresses | write a validateEmail function. example test cases: user@example.com is true, invalid is false, user@.com is false. run the tests after implementing |
| fix the login bug | users report login fails after session timeout. check the auth flow in src/auth/, especially token refresh. write a failing test that reproduces the issue, then fix it |
| the build is failing | the build fails with this error: [paste error]. fix it and verify the build succeeds. address the root cause, don’t suppress the error |
对 Codex CLI 也是一样。如果你只写 codex "refactor this",它会自己探索。但如果你写:
codex "refactor src/auth/login.ts to extract token refresh into a pure function. keep the public API unchanged. update tests in src/auth/__tests__/login.test.ts. run the test suite at the end."
AI 的探索范围就被你框住了,token 花在刀刃上。
另一个省上下文的小技巧是:用 @ 引用文件,而不是描述文件在哪。Claude Code 会提前读取文件,保证你不必在 prompt 里重复其内容。Codex CLI 也支持用 @file 把文件直接喂进去。
claude "@src/auth/login.ts extract the token refresh logic"
codex "@src/auth/login.ts @src/auth/__tests__/login.test.ts refactor token refresh"
三、让 AI 自己当 QA:给每条路径一个 pass/fail 检查
AI 写代码和人类写代码有一个共同点:写完都觉得能跑。但没有检查,“看起来对"就是最容易翻车的地方。
Claude Code 的 best practice 里有一句话值得贴在显示器上:
“Give Claude a check it can run: tests, a build, a screenshot to compare. It’s the difference between a session you watch and one you can walk away from.”
比如你做前端改动,prompt 里加上:
take a screenshot of the result and compare it to the original.
list differences and fix them.
不同力度的检查方式:
| 力度 | 适合场景 |
|---|---|
| 单 prompt 检查 | 小改动:“写完后跑 npm test” |
/goal 条件 | 整个会话的目标约束,AI 每轮后自检查 |
| Stop hook | 每次编辑后自动跑 lint / typecheck / tests |
| 子代理 review | 重要改动让另一个独立上下文复核 |
Codex CLI 也支持 --approval-mode full-auto 配合测试命令运行,但建议你只在有验证闭环的地方开 full-auto。没有 stop gate 的 autonomy 不叫效率,叫赌博。
我们内部一个简单但有效的习惯是:每个 feature 修改,prompt 结尾都带一句 “run tests and confirm all green before finishing”。AI 会自己跑,错误信息直接进入下一轮上下文,省掉你手动复制粘贴 debug 日志的步骤。
四、任务拆分:用子代理/worktree 保护主上下文
上下文窗口是最大的稀缺资源。你让 AI 去探索一个陌生模块,它可能读 50 个文件才回来——这些文件内容全部塞进你的主会话,后续你想让它写代码时,上下文已经脏了。
Claude Code 的解法是 subagents(子代理)。比如:
use subagents to investigate how our authentication system handles token refresh,
and whether we have any existing OAuth utilities I should reuse.
子代理在独立的上下文窗口里完成调研,只把结论带回来。你的主会话保持干净,专注于实现。
Codex CLI 目前没有内置 subagent,但它的强项是可脚本化、可并行化:
# 用 worktree 隔离并行实验
git worktree add ../codex-experiment feature-x
cd ../codex-experiment
codex -p "try approach A: replace sync loop with async batching. run tests."
更复杂的批量任务可以写 shell loop:
for file in $(cat files-to-migrate.txt); do
codex -p "Migrate $file from commonjs to esm. Return OK or FAIL." \
--allowedTools "Edit,Bash(git commit *)"
done
这里有两个省钱的点:
- 做研究的会话和做实现的会话分开。
- 并行跑多个独立任务,而不是让一个会话串行处理。
五、狂暴清理上下文:/clear、/compact、/rewind 是好朋友
如果你已经和这个 AI 聊了 50 轮,前面还有 5 个不相关的任务没清掉,那现在的性能下降不是 AI 的问题,是你会话管理的问题。
Claude Code 提供一组上下文管理工具:
| 命令 | 作用 | 什么时候用 |
|---|---|---|
/clear | 清空整个会话上下文 | 切换到不相关的任务前 |
/compact | 把历史对话压缩成摘要 | 需要保留部分上下文但窗口快满 |
/compact focus on API changes | 指定压缩时要保留的重点 | 长编码会话中期 |
/rewind | 回滚到历史 checkpoint | 发现 AI 走偏,要回退 |
/btw | 临时提问,不进入上下文 | 临时查个函数签名 |
规则很简单:只要任务不相关,就先 /clear。
我们发现一个有趣的指标:如果一个会话里你纠正了 AI 超过两次还没好,大概率是上下文被失败尝试污染了。这时候最划算的动作不是继续修,而是 /clear 后写一段更清晰、整合了教训的新 prompt。
Codex CLI 的会话是 stateless 的,每次 codex -p 就是一个干净的上下文。这本身就是它的优势:一个问题一个命令,不要在一个长对话里解决所有事。
六、能自动化就别交互:full-auto / -p / CI
最后的高级版"偷懒”,是让 AI 在后台自己跑。
Claude Code 支持非交互模式:
claude -p "list all API endpoints in this repo" --output-format json
claude -p "analyze this log file" --output-format stream-json --verbose
Codex CLI 本来就是命令优先:
codex -p "fix all lint errors" --approval-mode auto
并且可以用 --allowedTools 限定权限:
codex -p "migrate src/*.ts to esm" --allowedTools "Edit,Bash(npm test)"
把这些接入 CI:
# .github/workflows/codex.yml
- name: Auto-fix lint
run: codex -p "fix lint errors and commit if changed" --approval-mode auto
但再次强调:自动化只在有验证闭环时值得。如果 AI 改完没有测试反馈,它可能会把 bug 也自动化进去。
Claude Code vs Codex CLI:两张图的对比
| 维度 | Claude Code | Codex CLI |
|---|---|---|
| 项目级指令 | CLAUDE.md | AGENTS.md |
| 非交互模式 | claude -p | codex -p |
| 自动批准 | auto mode 分类器 | --approval-mode auto/full-auto |
| 子代理 | 内置 .claude/agents/ | 暂无内置,可用脚本+worktree 模拟 |
| 会话管理 | /clear, /compact, /rewind, resume | 每次 -p 新开上下文 |
| 可扩展点 | skills, hooks, MCP servers, plugins | skills, slash commands, execpolicy |
| 最适合 | 深度、长周期、需要记忆的复杂任务 | 可脚本化、批量、CI 集成 |
两种工具不是非此即彼。我们的实际用法:复杂 feature 用 Claude Code 长期对话完成;标准化批量任务用 Codex CLI 脚本跑。
就拿我们维护这个 Hugo 站点来说:
- 写长文、改模板、多语言同步 → Claude Code,因为需要持续上下文。
- 批量更新 front matter、跑 lint、生成缩略 → Codex CLI
-p脚本,一次执行,不占用主会话。
总结:偷懒的底层公式
回到开头那个约束:上下文窗口会满,满了就会变蠢。所有技巧本质上都在做三件事:
- 少看点:用
CLAUDE.md/AGENTS.md和精确 prompt,让 AI 不需要反复猜。 - 少返工:给每个任务一个可运行的检查,让错误就地解决。
- 少堆在一个篮子里:session 隔离、subagent、worktree、CI 脚本,把上下文当稀缺资源来分配。
这不是让你变得更"懒”,而是让你从一个盯着 AI 干活的监工,变成一个设计好系统后让它自己跑的人。
如果你也在用 AI Coding Agent
你最想省的是 token、时间、还是血压?欢迎在评论区分享你的实战技巧。
