How we make AI coding more cost efficient without sacrificing task quality

Why shorter outputs can cost more, and how GitHub Copilot reduces wasted work across the complete coding task.

Copilot hovering above a mosaic of green squares in a decorative scene.
| 11 minutes

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。其他多个 Copilot 产品(如 GitHub Copilot 应用程序和 Copilot 代码审查)也使用了相同的底层框架,并通过这些改进提升了效率。

Chart showing 3.1% 'Remove view previxes', 5.5% 'Selective output compaction', 2.9% 'Compact task-tool prompt', and 2.3% 'Reduce notification roundtrips'.
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,却在全局付出了更高成本。

Flow chart showing: RTK, compresses shell output > Local win, tool output gets shorter > Useful detail is missing > Recovery, reread or rerun > More turns and context carried forward. Then the option of finishing at 'End-to-end result, Tokens and cost up, Task duration up, Task completion: steady,' or 'Recovery repeats' going back to 'useful detail is missing'.
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.对基准测试运行结果的分析显示,安装、构建、测试和 Lint 输出中往往包含重复的噪声,而源代码类输出和任意命令结果则更可能包含智能体所需的信息。该分析为选择性输出压缩器提供了依据,部分参考了 RTK 及类似方法。

The prototype was evaluated on agentic coding benchmarks and a range of open source repositories, exercising their build, test, and lint systems.该原型在智能体编码基准测试和一系列开源仓库上进行了评估,涵盖了它们的构建、测试和 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:这些早期的失败促成了三项原则:

  1. Preserve source-like and arbitrary output. Commands such as cat, git diff, git show, and arbitrary scripts are returned unchanged.保留源代码类和任意输出。cat、git diff、git show 和任意脚本等命令的输出将原样返回。
  2. Reorganize search results without dropping content. Matches and file lists from tools such as grep can be grouped more efficiently while retaining every result.重组搜索结果但不丢失内容。来自 grep 等工具的匹配项和文件列表可以更高效地分组,同时保留所有结果。
  3. Compress repetitive noise selectively. Install, build, test, and progress output is compressed only when the savings are substantial.选择性压缩重复噪声。仅在节省效果显著时,才对安装、构建、测试和进度输出进行压缩。

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.当输出被压缩时,智能体仍可通过直接恢复路径获取完整的原始信息。

Flowchart showing how GitHub Copilot handles shell-command output. Copilot calls a shell command, classifies the output, then chooses one of three paths: keep arbitrary/source output unchanged, reorganize search results without losing any matches, or selectively compress repetitive noise (like install/build/test logs) while preserving full output and providing a recovery path. The processed result is returned to Copilot.
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.一项简洁的 Token 优化源于 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.每个前缀虽然很小,但在每一行、每一个读取的文件中重复出现,会在整个会话中累积。因此,我们将其移除了。

Before-and-after image of code snippets. The line-number prefixes re removed from the 'After' image.
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.行号在 diff 和简短代码片段中依然有用。但在当前场景下,由于它们附加在每次文件读取中且不服务于当前的编辑工作流,因此显得多余。

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 中,task 工具会启动专门的智能体来处理并行工作。其指导信息在工具描述、模式、智能体定义、系统指令和配套工具中不断累积。

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 生成并改进了更小的候选版本,并通过针对性的行为测试检查了我们需要保留的需求。

The first online experiment found a regression that the initial offline evaluations had missed. The meta-prompting loop had rewritten cautious parallelism guidance into a hard scheduling policy, causing independent custom agents to run sequentially.首次在线实验发现了一个初始离线评估漏掉的倒退问题。元提示词循环将谨慎的并行指导重写成了强制调度策略,导致独立的自定义智能体变成了串行运行。

We stopped the experiment. Before changing the prompt again, we wrote a regression evaluation for the behavior users had exposed. The eventual fix replaced an explicit allowlist and denylist with one sentence:我们停止了实验。在再次更改提示词之前,我们针对用户暴露出的行为编写了一项倒退评估测试。最终的修复方案用一句话替换了显式的允许列表和拒绝列表:

Independent agents can run in parallel; consider side effects.独立智能体可以并行运行;请考虑副作用。

That sentence was shorter and less restrictive; it deferred the choice of whether to run sub-agents in parallel to the model instead of the previous explicit guidance. With it, our new behavior test passed without causing any existing behavioral tests to fail.这句话更短且限制更少;它将是否并行运行子智能体的选择权交给了模型,而非之前的显式指导。有了它,我们的新行为测试通过了,且没有导致任何现有的行为测试失败。

Prompt behavior needs tests. If a behavior is not tested, a shorter prompt can remove it without anyone noticing. 提示词行为需要测试。如果某种行为未被测试,更短的提示词可能会在无人察觉的情况下将其移除。

Three-stage diagram labeled Compression → Regression + fix → Completed. Left panel shows an original prompt compressed by about 50%. Middle panel highlights a regression where agents became serialized, then a fix by editing one sentence to restore parallelism. Right panel shows final shipped prompt with restored behavior and cumulative savings of about 1,300 fewer tokens per turn across steps.
Figure 5 Prompt compression became safe only after a regression test exposed serialized agents and a one-sentence fix restored parallelism; the resulting token savings recur on every model turn.图 5:提示词压缩只有在倒退测试发现串行化问题并用一句话修复恢复并行后才是安全的;由此带来的 Token 节省在每一轮模型交互中都会产生。

The shipped prompt removes about 1,300 task-tool prompt tokens per turn, corresponding to approximately 1.8% fewer total prompt tokens per session and 2.9% lower normalized cost per active hour, with no quality regression detected in the measured evaluations.发布的提示词每轮减少了约 1,300 个 task 工具提示词 Token,相当于每个会话的总提示词 Token 减少了约 1.8%,每个活跃小时的标准化成本降低了 2.9%,且在已测评估中未发现质量倒退。

Deliver completed background work without an extra retrieval turn无需额外检索轮次即可交付已完成的后台工作

Agents often run independent work in the background, such as a long-running shell command alongside a sub-agent investigation. Notifications let the agent continue until that work is ready without spending a tool call waiting.智能体经常在后台运行独立工作,例如在子智能体调查的同时运行耗时较长的 Shell 命令。通知机制让智能体可以在等待该工作完成时继续执行其他任务,而无需浪费一次工具调用。

If the agent does not explicitly wait for either task, the harness wakes the model and notifies it when the shell command or sub-agent finishes.如果智能体没有明确等待某项任务,框架会在 Shell 命令或子智能体完成时唤醒模型并通知它。

Previously, that notification did not include the completed result, so the agent had to spend another turn retrieving output Copilot had already received. When several tasks finished close together, that detour could repeat. Copilot now batches eligible completion notifications and delivers completed results directly in the existing tool-result format. The agent can continue with the information it needs, without spending an extra turn asking for it again. Explicit reads for work that is still running behave as before.此前,该通知不包含完成结果,因此智能体必须花费另一轮交互来检索 Copilot 已经收到的输出。当多项任务接连完成时,这种绕路可能会重复发生。现在,Copilot 会批量处理符合条件的完成通知,并以现有的工具结果格式直接交付完成结果。智能体可以继续使用所需信息,无需花费额外轮次再次询问。对于仍在运行的工作,显式读取行为保持不变。

Before-and-after sequence diagram comparing orchestration behavior.

Before: model waits on separate shell and sub-agent completions, causing retrieval detours and four LLM calls to process two results.
After: a harness batches related completions and emits synthetic tool events so background work continues while waiting; both results are processed together in a single LLM call.
The visual emphasizes reduced latency and fewer model round trips.
Figure 6 Before, each background completion could wake a retrieval-only model turn. After, the harness batches eligible completions and delivers completed results in the existing tool-result format.图 6:改进前,每次后台任务完成都可能唤醒一轮仅用于检索的模型交互。改进后,框架批量处理符合条件的完成项,并以现有的工具结果格式交付结果。

Before this change, each completed task required one model call to request its result and another to process it. For the shell command and sub-agent shown above, that meant four model calls before work could continue.在此项更改之前,每项已完成的任务都需要一次模型调用来请求结果,另一次调用来处理结果。对于上述的 Shell 命令和子智能体,这意味着在工作继续前需要四次模型调用。

Now, the harness batches both completions and supplies their results together, so a single model call can process both. Removing those retrieval detours also avoids carrying the full session context through unnecessary calls.现在,框架会批量处理这两项完成项并一并提供结果,因此单次模型调用即可处理两者。消除这些检索绕路也避免了在不必要的调用中携带完整的会话上下文。

By delivering completed results directly, without compressing, summarizing, or withholding anything, the harness reduced average token-related usage, as measured in AI Credits, by about 2.3%.通过直接交付已完成的结果(不进行压缩、总结或保留任何内容),框架将以 AI 额度衡量的平均 Token 相关使用量降低了约 2.3%。

Measure changes in context衡量上下文中的变化

A change that saves tokens in one Copilot workflow can increase costs in another.在一个 Copilot 工作流中节省 Token 的更改,可能会在另一个工作流中增加成本。

For example, a tighter set of file-tool instructions was inspired by positive results in Copilot code review. In a Copilot CLI online experiment, it increased cost, so we did not ship it.例如,一套更紧凑的文件工具指令是受到 Copilot 代码审查中积极结果的启发。但在 Copilot CLI 的在线实验中,它增加了成本,因此我们没有发布它。

By contrast, removing line-number prefixes and selectively compressing output each reduced average prompt tokens per review by roughly 5% in independent evaluations across a large set of Copilot code review tasks using the production model. We detected no material change in the tracked review-quality metrics.相比之下,移除行号前缀和选择性压缩输出在大量 Copilot 代码审查任务的独立评估中,均使每次审查的平均提示词 Token 减少了约 5%。我们未发现所追踪的审查质量指标有任何实质性变化。

These findings are separate from the earlier migration of Copilot code review to the shared file tools, which, together with review-instruction tuning, reduced code review cost by about 20%.这些发现与之前将 Copilot 代码审查迁移到共享文件工具的举措是分开的,后者与审查指令调优一起,将代码审查成本降低了约 20%。

Each change needs to be measured in the workflow where it runs.每一项更改都需要在其运行的工作流中进行衡量。

Five lessons for building efficient AI coding agents构建高效 AI 编码智能体的五条经验

  1. Optimize the completed task, not the tool call. Shorter output is not cheaper if the agent spends more turns recovering what was removed.优化已完成的任务,而非工具调用。如果智能体花费更多轮次来恢复被移除的内容,更短的输出并不意味着更便宜。
  2. Optimize orchestration, not just model output. Eliminate model turns that perform work the harness can complete deterministically.优化编排,而不只是模型输出。消除模型执行框架可以确定性完成的工作的交互轮次。
  3. Compress by what the output represents. Preserve exact content, prefer lossless transformations, and measure how often agents use the recovery path.根据输出的含义进行压缩。保留精确内容,优先使用无损转换,并衡量智能体使用恢复路径的频率。
  4. Prompt rewrites sometimes have unintended consequences. Validate that intended behavior is preserved.重写提示词有时会产生意想不到的后果。务必验证预期的行为是否得到保留。
  5. Evidence is local to the workload. Re-evaluate changes in offline benchmarks, online experiments, and every product surface where they ship.证据具有工作负载局部性。在离线基准测试、在线实验以及所有发布该更改的产品界面中重新评估这些更改。

None of these changes made the model smarter. They removed work the model never needed to do.这些更改都没有让模型变得更聪明,它们只是移除了模型本不需要做的工作。

The changes described in this post are shipping across GitHub Copilot experiences that use the same underlying harness.本文描述的更改正在使用相同底层框架的 GitHub Copilot 产品中逐步发布。

Bring agentic workflows to your terminal
with GitHub Copilot CLI >
通过 GitHub Copilot CLI 将智能体工作流带入你的终端 >

Tags:标签:

Written by 作者

Erik Kristensen

Erik Kristensen

@erik-krogh

Erik Krogh Kristensen is a Staff Software Engineer at GitHub building at the intersection of AI, coding, and security. In the last few years at GitHub he's worked on an assortment of AI products such as Copilot Autofix, Copilot Code Review, and most recently Copilot CLI. He's a trusted source of how to make AI coding products better and more efficient.

Napalys Klicius

Napalys Klicius

@Napalys

Napalys Klicius is a Software Engineer at GitHub building agentic systems. His career has taken him from model checking to low-level C++ drone systems and static analysis, and more recently to teaching agents how to inspect code without getting lost.

Related posts