GitHub Copilot 工程复盘:优化 AI 编码成本与质量平衡
How we make AI coding more cost efficient without sacrificing task quality
Agent 开发者必读的硬核工程复盘,详细拆解了如何避免“局部省 token 导致全局更贵”的陷阱,给出了具体的压缩策略与评估指标,可直接参考用于优化自身的 Agent 链路。
Output quality is important when working with AI coding agents, but true efficiency comes from getting work done quickly, efficiently, and with the right context.
与 AI 编码代理协作时,输出质量很重要,但真正的效率来自于快速、高效地完成任务并提供正确的上下文。
That’s why token count of individual interactions alone isn’t a meaningful measure of efficiency. The goal shouldn’t be to use fewer tokens, but to tap into the right amount of context to move a task forward. A concise tool response can sometimes require additional calls or work if it leaves out information the agent needs, ultimately making the task slower and more expensive.
这就是为什么仅凭单次交互的 token 数量并不能有效衡量效率。目标不应是减少 token 使用量,而是利用适量的上下文来推动任务进展。如果简洁的工具响应遗漏了代理所需的信息,有时可能需要额外的调用或工作,最终导致任务更慢且成本更高。
That’s why we want to optimize for the outcome rather than the tool call. This post examines four changes in GitHub Copilot that put that principle into practice:
因此,我们希望优化的是结果而非工具调用次数。本文探讨了 GitHub Copilot 中体现这一原则的四项变更:
- Preserve useful context while reducing repetitive output.
- Remove formatting that adds no value to the task.
- Shorten instructions without changing useful behavior.
- Deliver completed background work without an extra retrieval step.
- 在减少重复输出的同时保留有用的上下文。
- 移除对任务无价值的格式内容。
- 在不改变有用行为的前提下缩短指令。
- 无需额外的检索步骤即可交付已完成的后台工作。
Possible changes were evaluated offline using agentic coding benchmarks. The most promising changes were then validated through controlled online experiments before shipping. The examples in this post come from GitHub Copilot CLI. Multiple other Copilot products, such as the GitHub Copilot app and Copilot code review, use the same underlying harness and also become more efficient through these improvements.
我们使用代理编码基准测试在离线环境中评估了可能的变更。最有前景的变更随后通过受控的在线实验进行验证,然后才上线。本文中的示例来自 GitHub Copilot CLI。GitHub Copilot 应用和 Copilot 代码审查等多个其他 Copilot 产品也使用相同的底层框架,并通过这些改进变得更加高效。
Figure 1: Four independent A/B experiments using the same AI-credit metric. The segments are shown together for comparison; their effects are not necessarily strictly additive.
图 1:使用相同 AI 积分指标进行的四项独立 A/B 实验。各部分并列展示以便比较;其效果不一定严格具有可加性。
The local metric trap
局部指标的陷阱
It’s common to shorten the output from each tool call as a way to reduce agent costs. RTK (Rust Token Killer) is a utility that shortens shell output before an agent reads it. We evaluated its effect on GitHub Copilot using our agentic coding benchmarks.
缩短每次工具调用的输出以降低代理成本是一种常见做法。RTK(Rust Token Killer)是一个实用工具,可在代理读取之前缩短 shell 输出。我们使用代理编码基准测试评估了它对 GitHub Copilot 的影响。
In our harness and benchmark configuration, RTK shortened some responses, but when the omitted text mattered, the model sometimes reopened the original output or reran the command to recover what it needed.
在我们的框架和基准配置中,RTK 缩短了某些响应,但当被省略的文本至关重要时,模型有时会重新打开原始输出或重新运行命令以恢复所需信息。
Those recovery steps added turns and carried more context forward. The individual tool response was shorter, but on average, the task used more tokens and took longer. We saved tokens locally and spent more globally.
这些恢复步骤增加了轮次并携带了更多上下文。单个工具响应变短了,但从平均来看,任务使用了更多的 token 并花费了更长时间。我们在局部节省了 token,却在整体上花费了更多。
Figure 2: A shorter tool response can make the completed task more expensive when missing details force the agent to reread output, rerun commands, and carry more context forward.
图 2:当缺失细节迫使代理重新阅读输出、重新运行命令并携带更多上下文时,较短的工具响应可能导致完成的任务成本更高。
This result applies to the integration and workloads we tested, not to every RTK configuration or to output compression in general. This meant that tokens per tool call is the wrong objective. An efficiency change has to be evaluated across the complete task, from the user’s request through the final result.
该结果仅适用于我们测试的集成和工作负载,并不适用于所有的 RTK 配置或一般的输出压缩。这意味着将“每次工具调用的 token 数”作为优化目标是错误的。效率的提升必须从用户请求到最终结果的整个任务流程中进行评估。
More useful was to look at what can we remove without making the model repeat work.
更有价值的做法是观察我们可以移除哪些内容,同时避免模型重复执行工作。
Compress noise, preserve useful information
压缩噪声,保留有用信息
The goal was to shorten repetitive output while preserving the context an agent needs to complete its task without retracing steps.
目标是在不重蹈覆辙的前提下,缩短重复性输出,同时保留智能体完成任务所需的上下文。
Analysis of benchmark runs showed that install, build, test, and lint output often contains repetitive noise, while source-like output and arbitrary command results are more likely to contain the information an agent needs. That analysis informed a selective output compressor, informed in part by RTK and similar approaches.
对基准测试运行的分析显示,install、build、test 和 lint 的输出通常包含重复性噪声,而类似源码的输出和任意命令的结果更可能包含智能体所需的信息。这一分析催生了一种选择性输出压缩器,其设计部分参考了 RTK 及类似方法。
The prototype was evaluated on agentic coding benchmarks and a range of open source repositories, exercising their build, test, and lint systems.
该原型在智能体编码基准测试以及一系列开源代码库上进行了评估,并运行了它们的 build、test 和 lint 系统。
Early versions were too aggressive. They made the model repeat work or read the full saved output, increasing end-to-end cost and reducing task success. For example, we initially compressed git diff but removed that filter after benchmark tasks showed agents reopening the original output to recover missing information.
早期版本过于激进。它们导致模型重复工作或读取完整的保存输出,从而增加了端到端成本并降低了任务成功率。例如,我们最初压缩了 git diff 的输出,但在基准测试任务显示智能体重开原始输出来恢复缺失信息后,移除了该过滤器。
Those early failures led to a three-part policy:
这些早期的失败促使我们制定了一项三分策略:
- Preserve source-like and arbitrary output. Commands such as cat, git diff, git show, and arbitrary scripts are returned unchanged.
- Reorganize search results without dropping content. Matches and file lists from tools such as grep can be grouped more efficiently while retaining every result.
- Compress repetitive noise selectively. Install, build, test, and progress output is compressed only when the savings are substantial.
- 保留类似源码的输出和任意输出。cat、git diff、git show 等命令以及任意脚本的输出均原样返回。
- 重组搜索结果而不丢失内容。来自 grep 等工具的匹配项和文件列表可以更高效地分组,同时保留所有结果。
- 有选择性地压缩重复性噪声。install、build、test 和进度输出的压缩仅在节省效果显著时才进行。
The shipped version emerged through repeated evaluation and refinement. It is conservative not because the goal was to build a conservative compressor, but because that is what the evaluations supported.
最终发布的版本是通过反复评估和优化得出的。它之所以保守,并非因为目标是构建一个保守的压缩器,而是因为评估结果支持这种保守做法。
When output is compressed, the agent can still retrieve the complete original through a direct recovery path.
当输出被压缩时,智能体仍可通过直接恢复路径获取完整的原始内容。
Figure 3: The shipped compressor preserves source-like output, reorganizes search results without loss, and compresses only predictable repetitive noise while retaining the full original.
图 3:发布的压缩器保留类似源码的输出,无损地重组搜索结果,并仅压缩可预测的重复性噪声,同时保留完整的原始内容。
That recovery path is both a safety mechanism and an evaluation signal. We tracked whether the agent opened the saved original, reran commands, repeated exploration, narrowed its searches, or took additional turns. Frequent recovery would indicate that the compressor had removed something valuable.
该恢复路径既是安全机制,也是评估信号。我们追踪了代理是否打开了保存的原始文件、重新运行命令、重复探索、缩小搜索范围或采取额外步骤。频繁的恢复表明压缩器可能移除了有价值的信息。
On offline tasks where output compression triggered, no statistically significant task-success regression was detected, and agents extremely rarely opened the saved originals. In the online experiment, average cost decreased slightly with no material regression detected in the tracked quality metrics.
在触发输出压缩的离线任务中,未检测到具有统计学意义的任务成功率下降,且代理极少打开保存的原始文件。在在线实验中,平均成本略有下降,且在追踪的质量指标中未检测到实质性倒退。
Remove formatting before removing information
在移除信息之前先移除格式
One clean token optimization came from the view tool, which agents use to read file contents into context.
一项来自 view 工具的干净令牌优化,该工具用于将文件内容读取到上下文中。
Previously, view prefixed every line with a number before showing the contents to the model. Earlier file-editing tools used those numbers to target changes, but current tools instead match surrounding code and do not use line numbers. The line-number prefixes remained even though the normal workflow no longer used them.
此前,view 在向模型展示内容前会在每行前添加数字前缀。早期的文件编辑工具利用这些数字来定位更改,但当前工具改为匹配周围代码,不再使用行号。尽管正常工作流程已不再使用它们,但行号前缀仍然保留。
Each prefix was small. Repeated across every line and every file read, however, that unused formatting accumulated throughout a session. So, we removed it.
每个前缀都很小。然而,当它出现在每次读取的每一行和每个文件中时,这种未使用的格式在整个会话中不断累积。因此,我们将其移除。
Figure 4: Removing line-number prefixes preserves the source exactly while eliminating formatting that was repeated across every file read.
图 4:移除行号前缀在精确保留源代码的同时,消除了每次读取文件时重复出现的格式。
Line numbers remain useful in diffs and short snippets. They were wasteful here because they were attached to every file read without serving the current editing workflow.
行号在差异比较(diffs)和简短片段中仍然有用。在此处它们是浪费的,因为它们附加于每次文件读取,却未服务于当前的编辑工作流程。
Removing them caused model-inference cost to fall by roughly 5% in offline agentic coding benchmarks. Success rates stayed within the expected run-to-run variance, and edit failures did not increase.
移除它们使得离线智能编码基准测试中的模型推理成本下降了约 5%。成功率保持在预期的运行间波动范围内,且编辑失败率并未增加。
We then tested the change with Copilot CLI users. The online experiment reduced average daily model-inference cost per user by about 3%, with no material regression detected in the quality or satisfaction metrics we tracked.
随后,我们在 Copilot CLI 用户中测试了该更改。在线实验使每位用户的平均每日模型推理成本降低了约 3%,且在追踪的质量或满意度指标中未检测到实质性倒退。
For developers, that means more of the context window is available for the work itself rather than formatting the agent does not use.
对于开发者而言,这意味着上下文窗口中有更多空间可用于实际工作,而非用于代理不使用的格式。
This was the ideal change: no new instructions for the model, no source of information to recover, and no additional decision to make. The file contents reached the model unchanged.
这是理想的更改:无需为模型提供新指令,无需恢复信息来源,也无需做出额外决策。文件内容以不变的形式送达模型。
Compress prompts without compressing intent
压缩提示词而不压缩意图
Prompts carry instructions that shape how an agent works, and they are sent to the model on every turn. Shortening them only improves efficiency if the agent keeps the behaviors developers depend on.
提示词包含塑造智能体工作方式的指令,并在每一轮交互中发送给模型。只有当智能体保持开发者所依赖的行为时,缩短提示词才能提高效率。
In GitHub Copilot, the task tool launches specialized agents for parallel work. Its guidance had accumulated across tool descriptions, schemas, agent definitions, system instructions, and companion tools.
在 GitHub Copilot 中,任务工具会启动专门的智能体以并行工作。其指导原则已累积在工具描述、模式定义、智能体定义、系统指令和辅助工具中。
A meta-prompting loop, in which Copilot iteratively wrote its own prompt, reduced that prompt by roughly half. Copilot produced and refined smaller candidates, and targeted behavioral tests checked the requirements we wanted to preserve.
通过元提示循环(Copilot 迭代地编写自己的提示词),将提示词缩减了约一半。Copilot 生成并优化更小的候选提示词,并通过针对性的行为测试来检查我们想要保留的要求。
更进一步:量化金融体系
看懂新闻只是起点——沿量化金融路径,把它变成能交付的工程能力