编码代理 Token 成本优化 2026:70 倍差距从哪来
2026 年 8 月的两个 benchmark 把编码代理的账单问题摆上了台面:同一个模型、同一批任务,Aider 每解决一个任务大约花 3,500 token,OpenClaw 要花 292,000 token——差了 70 倍。Composio 的企业工作流测试里,每成功一次的成本从 Pi Agent 的 $0.028 到 Claude Code 的 $0.195,而 DeepAgents 用四分之一的价格拿到了完全相同的通过率。问题不在模型,在框架。本文拆解 token 成本的真实来源,并给出可落地的优化代码。
1. 数据先说话:70 倍差距从哪来
6 月的独立 benchmark 用 12 种配置跑了同一批 12 个 Python 任务,全部走 OpenRouter,保证模型和 API 完全一致。结果是每解决一个任务消耗 3,500 到 292,000 token,而且这个排序在换成另一个模型后几乎不动——说明差距来自框架软件本身,而不是模型行为。8 月 Composio 用 8 个框架跑 30 个企业工作流(Airtable、Gmail、Google Calendar、Google Sheets、GitHub、Slack、PostHog),用程序化验证器而不是 LLM 裁判打分,还埋了诱饵数据。240 次运行中 129 次成功,每次成功成本最低 $0.028、最高 $0.195。DeepAgents 与 Claude Code 通过率完全一致,但每次成功成本只有四分之一。
// The measurement that started it all: run the same model
// through different harnesses and count tokens per solved task.
// June 2026 benchmark — 12 configs, 2 models, 12 Python tasks,
// all through OpenRouter so the model and API were identical.
import { countTokens } from "./token-counter";
const harnesses = ["aider", "claude-code", "codex", "goose",
"hermes", "kilo", "kimi-code", "nanobot", "openclaw",
"opencode", "qwen-code"];
for (const h of harnesses) {
const result = await runSuite(h, {
model: "deepseek-v4-flash",
tasks: PYTHON_TASKS,
});
console.log(h, {
tokensPerSolved: Math.round(result.tokens / result.solved),
passRate: result.solved / result.total,
});
}
// Output (tokens per solved task):
// aider (architect mode): ~3,500
// openclaw: ~292,000 <- 70x gap, same model!2. 启动税:每一轮都在重复付的固定成本
差距的核心是一个简单的测量:启动税(startup tax)。在提示词开始干活之前,框架要先带上自己的行李——系统提示词、工具描述、环境设置。Aider 的 architect 模式大约 700 token,OpenClaw 大约 26,000 token。如果这笔钱只付一次还能忍,但重发模式(resend pattern)让它每轮都付:一个带着 26,000 token 底座的框架跑 15 轮,光脚手架就吃掉大约 390,000 个输入 token。
// The startup tax: before a prompt does any work, the harness
// ships its own baggage — system prompt + tool descriptions +
// environment setup. This floor is paid on EVERY turn.
export function measureStartupTax(harness: Harness): number {
const baseline = harness.tokenize(
harness.systemPrompt + harness.toolDescriptions.join("")
);
// aider architect mode: ~700 tokens
// openclaw: ~26,000 tokens
return baseline;
}
// Why it compounds: the resend pattern. A 26,000-token floor
// through 15 turns = ~390,000 input tokens on scaffolding alone,
// before the model writes a single line of code.
const floor = 26000;
const turns = 15;
console.log(floor * turns); // 390,0003. 为什么 DeepAgents 能用四分之一成本打平
Composio 的对比很有说服力:DeepAgents 和 Claude Code 的通过率完全相同,都是 54%,但每次成功的成本一个 $0.049、一个 $0.195。同样的任务、同样的验证标准,差距只可能来自提示词设计、工具调度和上下文管理。这说明优化杠杆不在模型而在框架——把框架的提示词地板压下来,账单直接骨折。
// Composio's August benchmark: 8 harnesses, 30 enterprise
// workflows (Airtable, Gmail, Calendar, Sheets, GitHub, Slack,
// PostHog). 900-second ceiling, programmatic verifier, decoys.
const results = [
{ harness: "pi-agent", costPerSuccess: 0.028, passRate: 0.43 },
{ harness: "deepagents", costPerSuccess: 0.049, passRate: 0.54 },
{ harness: "claude-code", costPerSuccess: 0.195, passRate: 0.54 },
// DeepAgents matched Claude Code's pass rate exactly
// while costing a quarter as much per success.
];
// The optimization lever is not the model — it's the harness.
// The same pass rate at 1/4 the cost means the bottleneck is
// prompt engineering of the harness, not model quality.4. 一个 R²=0.99 的预测模型
最实用的结论来自回归分析:启动税乘以轮次数,就能以 R²=0.99 的精度预测每任务的 token 消耗——这个关系在两个不同模型上都成立。换句话说,你想优化代理账单,第一刀应该砍在提示词地板和轮次数上,而不是急着换模型或者上花哨的缓存方案。先量,再砍。
// The predictive model: startup tax × turn count explains
// tokens per solved task with R² = 0.99 across BOTH models.
// That is the actionable insight.
export function predictTokensPerTask(harness: Harness, model: Model) {
const startupTax = measureStartupTax(harness);
const avgTurns = harness.avgTurnsPerTask(model);
return startupTax * avgTurns;
}
// Optimization playbook, in priority order:
// 1. Shrink the prompt floor (system prompt, tool schemas)
// 2. Reduce turn count (better planning, fewer retries)
// 3. Only then consider model swapping / caching
export const playbook = [
"trim tool descriptions to the minimum viable schema",
"remove rarely-used tools from the active toolset",
"add a planning step to cut failed-attempt turns",
"enable prompt caching for the stable prefix",
];5. 优化行动清单
按优先级排列:先把工具描述裁到最小可用 schema,删掉不常用的工具;再加一个规划步骤减少失败重试的轮数;最后才考虑 prompt caching 这类高级手段。框架的提示词地板乘轮次数,就是你账单的骨架。
6. 别忘了给任务设预算
生产环境里给每个任务设 token 预算,超了就 fail fast,别让代理在死循环里烧钱。代码里用一个简单的定时检查器监控当前上下文 token 数,超过上限立刻终止。省下的每一分钱都是利润。
// Practical harness-side budget guard: cap spend per task
// and fail fast instead of burning tokens on loops.
export async function runWithBudget(task: Task, budget: TokenBudget) {
const start = countTokens(await systemPrompt(task));
let used = start;
const guard = setInterval(() => {
used = countTokens(await currentContext());
if (used > budget.max) {
console.error("budget exceeded", used, ">", budget.max);
process.exit(1);
}
}, 1000);
try {
return await runAgent(task);
} finally {
clearInterval(guard);
}
}📌 常见问题 FAQ
为什么同一个模型在不同框架里 token 消耗差 70 倍?
因为每个框架自带不同的系统提示词、工具描述和环境设置。这些固定开销每一轮都会重复发送,称为启动税(startup tax)。Aider architect 模式约 700 token,OpenClaw 约 26,000 token,差距就这么来的。
启动税×轮次数为什么能预测总成本?
2026 年 6 月的 benchmark 显示,这个乘积在两个不同模型上预测每任务 token 消耗的 R² 达到 0.99。固定开销乘上发送次数,就是账单的基本盘。
DeepAgents 为什么能用四分之一成本打平 Claude Code?
Composio 的测试里两者通过率都是 54%,但 DeepAgents 每次成功成本 $0.049、Claude Code $0.195。差距来自框架的提示词设计与上下文管理效率,而不是模型能力。
优化代理 token 成本,第一步该做什么?
先测量启动税和轮次数,然后砍提示词地板(精简工具描述、删不常用工具)、减少轮次数(加规划步骤)。换模型和缓存是后面的事。
怎么防止代理在死循环里烧钱?
给每个任务设置 token 预算上限,用定时检查器监控上下文消耗,超限立即终止。fail fast 比让代理自我修复便宜得多。