> Harness EngineeringHarness EngineeringHarness Engineering The engineering outside the model模型之外的工程模型之外的工程

1.1

Choosing a model: cost per finished task選模型:算的是每次成功的成本选模型:看每次成功的成本

This chapter assumes you have used an AI coding agent and have seen a usage or billing page for it. Nothing else.

讀這一章,只需要用過 AI 寫程式 agent,並且看過它的用量或帳單頁面。其他都不用。

读这一章,只需要用过写代码的 AI agent,并且看过它的用量页面或账单页面。别的都不需要。

The Knowledge經驗分享经验分享

In one sentence

Judge a model by what it costs to get one task done, not by its price per token. Most of the cost is what the model reads, so reading less and redoing less saves the most.

一句話

評斷模型,看的是做完一個任務要花多少,不是每個 token 的單價。大部分的成本是模型在讀的東西,所以少讀、少重做,省得最多。

一句话

评价一个模型,看的是做完一个任务要花多少钱,不是每个 token 的单价。大部分成本是模型在读的东西,所以少读、少返工,省得最多。

For leaders

  • The decision: judge models and setups by what it costs to finish one task, not by the price per token. And when a result is bad, try the cheaper fixes (missing rule, missing check, lost thread) before paying for a bigger model.
  • The risk: in a long agent task most of the tokens are the model re-reading the same material. In my worked example, with round numbers I chose, reading was over 90% of the tokens. A cache can cut the bill a lot, but only while the start of the input stays unchanged.
  • The cost of doing it right: mostly setup time and a habit of measuring. A fair model comparison is a few tasks, run about three times each.
  • Ask the team: "What does one finished task cost us, and when did we last check that number?"

給主管

  • 要做的決定:評斷模型和設定,看做完一個任務的成本,不看每個 token 的單價。結果不好的時候,先試比較便宜的修法(缺一條規則、缺一道檢查、丟了線索),再考慮付錢換更大的模型。
  • 風險:長的 agent 任務裡,大部分 token 是模型在重讀同樣的材料。在我的例子裡,數字是我挑的整數,讀的部分占了 token 的九成以上。快取(cache)可以大幅降低帳單,但只在輸入的開頭保持不變時才有用。
  • 做對的成本:主要是設定的時間,加上量測的習慣。公平的模型比較,是幾個任務,每個跑大約三次。
  • 可以問團隊:「我們做完一個任務要花多少?上次確認這個數字是什麼時候?」

写给管理者

  • 要拍板的事:评价模型和配置,看做完一个任务的成本,不看每个 token 的单价。结果不好的时候,先试便宜的办法(缺一条规则、缺一道检查、线索丢了),再考虑花钱换更大的模型。
  • 风险:在一个长的 agent 任务里,大部分 token 是模型在反复重读同样的内容。在我的算例里(数字是我选的整数),读占了 90% 以上的 token。缓存能大幅降低账单,但只有输入的开头保持不变时才有用。
  • 做对的代价:主要是配置的时间,加上量化的习惯。公平地比较模型,是几个任务,每个跑大约三次。
  • 可以问团队:“做完一个任务,我们要花多少钱?上次核对这个数字是什么时候?”

The problem

A model comparison usually starts with a price list: so many dollars per million tokens. It is the number you can look up, so it is the number people use. But you never buy tokens for their own sake. You buy a task finished: a bug fixed, a report written. What matters is the total for that.

Two things make the price list a poor guide. A cheap model that fails the task and has to be asked again may cost more than a strong one that gets it right the first time. And in an agent, the number of tokens is not what you expect, as the next section shows.

Five words first

  • A token is a small piece of text, roughly a short word or part of one. Models read and write in tokens, and bills are counted in them.
  • Input tokens are what the model reads in a call. Output tokens are what it writes. They are priced differently, and output is usually the more expensive per token.
  • The context window is how much text the model can see at once: your request, the rules, the files it read, the results of earlier tools. It has a size limit. Chapter 3.1 is about managing it.
  • Reasoning effort is a setting on some models that controls how long they think before they answer. More thinking usually means more output tokens and more time.
  • The prompt cache lets the provider remember a long beginning of your input, so a later call that starts with the same text can be charged less for it.

Where the tokens go in an agent

In a chat, you send a question and get an answer. In an agent loop (chapter 2.1) the model is called again after every tool result. And every call sends the whole conversation so far: rules, request, every file it read, every result. The model has no memory between calls, so that is the only way it knows what happened.

rules + request writes a little turn 1 rules + request result 1 writes a little turn 2 rules + request result 1 result 2 writes a little turn 3 The repeated part is cheaper, from the prompt cache
  1. Turn 1: the model reads the rules and your request, and writes a short tool call.
  2. Turn 2: it reads all of that again, plus the result of the first call, and writes a little more.
  3. Turn 3: it reads everything again, plus the second result. What it reads grows each turn; what it writes stays small.
  4. The prompt cache charges less for the part that repeats exactly. This is why cache hits change the cost of a long task.
In a loop, every turn re-reads the whole context. Reading, not writing, is what a long task mostly consists of.

Here is the arithmetic with round numbers I chose to make it easy, not numbers I measured. Say the rules and your request are 10,000 tokens, and each tool result adds 5,000. Over 10 calls, the model reads about 10,000 + 15,000 + 20,000 + ... = 325,000 tokens, but writes perhaps a few hundred tokens per call. In this example reading is more than 90% of the tokens. The real share depends on your task, but the longer the loop, the more it leans toward reading.

Output costs more per token, so tokens are not the same as money. But the shape holds: a long agent task is mostly the model reading the same material again and again. That is why the chapter's second half is about reading less.

I can show one real case. In chapter 2.3 I gave the same four tasks to a team of three agents with job titles and to a single agent. Both got every task right. The team used about 4.5 times as much. The largest waste was re-reading: each new agent started with an empty context and had to read the same files again. It was four small tasks, each run once, so trust the direction and not the ratio.

The prompt cache changes the sum

Because so much of every call is the same beginning, providers offer a prompt cache. On Anthropic's API, reading cached input costs a tenth of the base input price on most models, and less on some. Writing to the cache costs more than a normal read: 1.25 times for a five-minute cache, 2 times for a one-hour cache. (Prompt caching)

Three things follow, and they change how you save money:

  • A stable beginning pays. The cache works on an exact match from the start of the input. If you change something near the top (an edited rule, a different tool list), everything after it misses the cache.
  • A pause can cost you. A cache lives for a short time. A conversation you leave for an hour and return to may pay full price to rebuild.
  • The cheapest token is one that is not sent. Even a cache read is not free. Reading less is still the first thing to try.

Here is the same example with a cache that hits every time, counting input only and using the five-minute prices. The first call writes 10,000 tokens at 1.25 times, which is 12,500. Each of the next nine calls reads the earlier text from the cache (10,000 up to 50,000 tokens, 270,000 in all, at 0.1 times, which is 27,000) and writes its 5,000 new tokens (45,000 in all, at 1.25 times, which is 56,250). The total is about 96,000 token-prices instead of 325,000, and now the repeated reading is less than a third of it. So the cache changes where the money goes: from re-reading to new material. That is why the earlier "more than 90%" is a count of tokens read, not a share of the bill, and why a team with a well-kept cache can read a lot and still pay little. Misses, which the first two bullets describe, bring the cost back toward the 325,000.

問題

比較模型,通常從價目表開始:每百萬個 token 多少美元。這是查得到的數字,所以大家用它。但你買 token,從來不是為了 token 本身。你買的是做完的任務:修好一個 bug、寫好一份報告。重要的是那件事的總花費。

價目表是個差勁的指引,有兩個原因。一個便宜的模型,任務失敗又得再問一次,可能比一個第一次就做對的強模型還貴。而且在 agent 裡,token 的數量和你預期的不一樣,下一節會說明。

先講五個詞

  • token 是一小段文字,大約是一個短字或字的一部分。模型用 token 讀和寫,帳單也用 token 算。
  • 輸入 token 是模型在一次呼叫裡讀的。輸出 token 是它寫的。兩者的價格不同,通常輸出每個 token 比較貴。
  • 上下文視窗是模型一次能看到多少文字:你的需求、規則、它讀過的檔案、之前工具的結果。它有大小上限。上下文是一份預算 講怎麼管它。
  • 推理強度是某些模型有的設定,控制它回答前想多久。想得多,通常輸出的 token 更多,時間也更長。
  • prompt 快取讓供應商記住你輸入的一大段開頭,這樣之後的呼叫,只要開頭是同樣的文字,那一段就可以收得便宜些。

在 agent 裡,token 去哪了

在對話裡,你送一個問題,得到一個回答。在 agent 迴圈裡(agent 循環,和你能插手的地方),每個工具結果出來之後,模型都會被再呼叫一次。而且每次呼叫,都要送出到目前為止的整段對話:規則、需求、它讀過的每個檔案、每個結果。模型在兩次呼叫之間沒有記憶,所以這是它知道發生過什麼的唯一辦法。

規則 + 需求 只寫一點點 第 1 輪 規則 + 需求 結果 1 只寫一點點 第 2 輪 規則 + 需求 結果 1 結果 2 只寫一點點 第 3 輪 重複的部分可以走 prompt cache,價錢較低
  1. 第 1 輪:模型讀規則和你的需求,寫出一個很短的工具呼叫。
  2. 第 2 輪:它把這些再讀一遍,加上第一次呼叫的結果,再寫一點點。
  3. 第 3 輪:全部再讀一遍,加上第二個結果。讀的越來越多,寫的一直很少。
  4. prompt cache 對完全重複的部分收費較低。所以快取命中會改變長任務的成本。
在迴圈裡,每一輪都把整個上下文重讀一遍。長任務主要是在讀,不是在寫。

下面用整數算一遍。這些數字是我為了好算而挑的,不是量出來的。假設規則加你的需求是 10,000 個 token,每個工具結果再加 5,000。呼叫 10 次,模型讀的大約是 10,000 + 15,000 + 20,000 + …… = 325,000 個 token,但每次呼叫大概只寫幾百個 token。在這個例子裡,讀的部分占了 token 的九成以上。實際的比例看你的任務,但迴圈越長,越偏向讀。

輸出每個 token 比較貴,所以 token 不等於錢。但形狀不變:長的 agent 任務,大多是模型一遍又一遍讀同樣的材料。這就是為什麼這一章的後半,講的是怎麼少讀。

我可以給一個真實的例子。在 慎用子 agent 裡,我把同樣的四個任務,交給有三個職位的三個 agent 組成的團隊,也交給單個 agent。兩邊每個任務都答對。團隊用掉的大約是 4.5 倍。最大的浪費是重讀:每個新的 agent 都從空的上下文開始,得把同樣的檔案再讀一遍。那是四個小任務,每個只跑一次,所以相信方向,不要相信倍數。

prompt 快取改變了算式

每次呼叫有這麼大一段開頭都一樣,所以供應商提供 prompt 快取。在 Anthropic 的 API 上,大多數模型讀快取的輸入,只收基本輸入價的十分之一,有些更低。寫入快取比一般的讀貴:五分鐘的快取是 1.25 倍,一小時的快取是 2 倍。(Prompt caching)

由此得到三件事,它們改變了你省錢的方式:

  • 穩定的開頭有回報。快取要求從輸入的開頭起完全一致。如果你改了靠近頂端的東西(改了一條規則、換了工具清單),後面的全都會沒命中。
  • 停一下可能要付錢。快取只活很短的時間。一段對話你離開一小時再回來,可能要付全價重建。
  • 最便宜的 token,是沒送出去的那個。就算是讀快取,也不是免費的。少讀,仍然是第一件該試的事。

下面是同一個例子,假設快取每次都命中,只算輸入,用五分鐘的價格。第一次呼叫寫入 10,000 個 token,乘 1.25 倍,是 12,500。接下來九次呼叫,每次從快取讀之前的文字(從 10,000 到 50,000 個 token,共 270,000,乘 0.1 倍,是 27,000),再寫入新的 5,000 個 token(共 45,000,乘 1.25 倍,是 56,250)。總共大約 96,000 個 token 的價格,而不是 325,000,重複讀取的部分現在不到三分之一。所以快取改變的是錢花在哪裡:從重讀變成新材料。這就是為什麼前面說的「九成以上」,算的是讀了多少 token,不是帳單的占比,也是為什麼快取維護得好的團隊,可以讀很多卻付得很少。沒命中(前兩點說的情況)會把成本拉回 325,000 附近。

问题

比较模型,通常从一张价目表开始:每百万 token 多少美元。这是查得到的数字,所以大家就用它。但你从来不是为 token 本身付钱。你买的是做完一件事:修好一个 bug,写完一份报告。要紧的是这件事的总价。

价目表不太靠谱,有两个原因。一个便宜的模型,任务失败了还得再问一遍,可能比一个一次就做对的强模型更贵。而且在 agent 里,token 的数量并不是你想的那样,下一节会讲。

先说五个词

  • token 是一小段文本,大致是一个短词或半个词。模型按 token 读写,账单也按 token 算。
  • 输入 token 是模型在一次调用里读的内容。输出 token 是它写的内容。两者价格不同,输出的单价通常更贵。
  • 上下文窗口是模型一次能看到多少文本:你的需求、规则、它读过的文件、之前工具的结果。它有大小上限。上下文是一份预算讲怎么管理它。
  • 推理强度是部分模型上的一个设置,控制它回答前想多久。想得多,通常意味着更多的输出 token 和更长的时间。
  • prompt 缓存让提供方记住你输入里很长的一段开头,这样后面的调用如果以同样的文本开头,这部分就能少收钱。

agent 里的 token 去了哪

在聊天里,你发一个问题,得到一个回答。在 agent 循环里(agent 循环,以及你能插手的地方),每拿到一个工具结果,模型就会被再调用一次。而每次调用都要把到目前为止的整个对话发过去:规则、需求、读过的每个文件、每个结果。模型在两次调用之间没有记忆,这是它知道发生了什么的唯一办法。

规则 + 需求 只写一点点 第 1 轮 规则 + 需求 结果 1 只写一点点 第 2 轮 规则 + 需求 结果 1 结果 2 只写一点点 第 3 轮 重复的部分走 prompt 缓存,价格更低
  1. 第 1 轮:模型读规则和你的需求,写出一个很短的工具调用。
  2. 第 2 轮:它把这些再读一遍,加上第一次调用的结果,再写一点点。
  3. 第 3 轮:全部再读一遍,加上第二个结果。读的越来越多,写的一直很少。
  4. prompt 缓存对完全重复的部分收费更低。所以缓存命中会改变长任务的成本。
在循环里,每一轮都把整个上下文重读一遍。长任务主要是在读,不是在写。

下面用整数来算,是我为了好算而选的,不是测出来的。假设规则加你的需求是 10,000 个 token,每个工具结果再加 5,000 个。10 次调用下来,模型读的大约是 10,000 + 15,000 + 20,000 + ... = 325,000 个 token,而每次调用写的可能只有几百个 token。在这个例子里,读占了 token 的 90% 以上。实际的比例取决于你的任务,但循环越长,越偏向读。

输出的单价更高,所以 token 数不等于钱。但这个形状是成立的:一个长的 agent 任务,大部分是模型在一遍遍地重读同样的内容。所以这一章的后半部分,讲的都是怎么少读。

我可以给一个真实的例子。在慎用子 agent里,我把同样的四个任务,分别交给了三个按岗位分工的 agent 组成的团队,和一个单干的 agent。两边每个任务都做对了。团队用掉的约是后者的 4.5 倍。最大的浪费就是重读:每个新开的 agent 都从空白的上下文开始,只好把同样的文件再读一遍。那是四个小任务,每个只跑了一次,所以方向可以信,倍数别当真。

prompt 缓存改变了这笔账

每次调用的很大一部分都是同样的开头,所以提供方推出了 prompt 缓存。在 Anthropic 的 API 上,大多数模型读缓存的输入,价格是基础输入价的十分之一,有些更低。写入缓存比普通读取贵:五分钟的缓存是 1.25 倍,一小时的缓存是 2 倍。(Prompt caching)

由此可以得出三点,它们会改变你省钱的办法:

  • 稳定的开头划算。缓存要求从输入的开头起完全一致才能命中。如果你改了靠前的东西(改了一条规则、换了工具列表),它后面的所有内容都没法命中缓存。
  • 停一下可能要花钱。缓存只活很短的时间。一个你放下一小时再回来的对话,可能要付全价来重建。
  • 最便宜的 token 是没发出去的那个。哪怕是读缓存也不是免费的。少读,仍然是第一个该试的办法。

下面是同一个例子,假设缓存每次都命中,只算输入,用五分钟缓存的价格。第一次调用写入 10,000 个 token,按 1.25 倍算,是 12,500。接下来九次调用,每次从缓存读之前的文本(从 10,000 个到 50,000 个 token,一共 270,000 个,按 0.1 倍算,是 27,000),再写入新增的 5,000 个 token(一共 45,000 个,按 1.25 倍算,是 56,250)。总共约合 96,000 个 token 的价钱,而不是 325,000,这时重复读取的部分已经不到三分之一。所以缓存改变的是钱花在哪:从重读,转到新内容。这也是为什么前面说的“90% 以上”,数的是读了多少 token,不是账单的占比;也是为什么缓存维护得好的团队,读很多也付得很少。缓存没命中的情况,就是前两条说的那些,会让成本回到 325,000 附近。

How it works

What a task costs

Think of a task's cost as three parts.

What it reads every call, minus cache hits + What it writes answers, code, thinking × Redoing how many attempts Cost of one finished task. Most savings are in the first and the last.
A task's cost is what the model reads plus what it writes, multiplied by how often the work is redone.

In detail:

  1. What the model reads, across every call, discounted where the cache hits.
  2. What the model writes: the answer, the code, the thinking.
  3. Redoing: a wrong answer, a lost thread, a repeated attempt. Redoing is a multiplier on parts 1 and 2.

Most savings come from the first and the third. A good rule of thumb is: do things in the cheapest way that is enough.

When a strong model is cheaper

A stronger model costs more per token. It can still cost less per finished task when it gets the answer in one pass and the weaker one needs three. It can also be a waste, if the task was easy.

I have no measurement of my own for where this flips. It is an argument, not a result, and the way to find the point is to test it on your own tasks: run the same few tasks on two models and compare the cost per success, not per call. Chapter 6.4 says how to do that honestly.

Pick by task, and check the setting

My rule of thumb, which I have not measured: use a strong model for thinking (a plan, an approach, a hard bug) and a medium model for doing (a change whose plan is already clear). Some tools let you set reasoning effort too. I have not tested whether Copilot lets you set it from a harness, so check your own tool.

How you are billed in Copilot (as of late October 2026)

This part changes often, so check the date and your own plan.

  • Monthly Copilot plans: since June 1, 2026 Copilot has been moving to usage-based billing. Premium request units were replaced by GitHub AI Credits, and usage is calculated from input, output and cached tokens, at the listed API rates of each model. Code completions and next edit suggestions stay included. (GitHub blog)
  • Annual Pro and Pro+ plans stay on the older premium request model until they expire. There, each prompt you send counts as a request multiplied by the model's multiplier, and the tool calls the agent makes do not count. (Copilot requests)

The two models reward different habits. Under per-request billing a long autonomous run is cheap to you, so doing big tasks in a single prompt can pay. Under token billing, a long run costs in proportion to how much it reads. Which one you are on decides how much the rest of this chapter matters. If your plan is billed by tokens, it matters a lot.

When the result is wrong, change the model last

The easy reaction to a bad result is to switch to a bigger model. It is the most expensive fix, and often the wrong one. Look at what went wrong first:

What went wrong Try first
It did not know a rule, a name or how the project does it Write it into the rules or a knowledge file. A bigger model does not know it either
It said "done" and it was not Add a check: run the tests (chapter 5.4)
It lost the thread in a long session Start a new session and carry the state in files (chapter 3.2)
It knew everything and still reasoned badly Now try a stronger model

Four ways to save, and how each can go too far

  1. Use the model less. If code can do it, do not ask a model. Counting, copying files, converting a Word file into text, checking a format: a script does these the same way every time and uses no tokens. The danger is moving judgment into code that cannot judge. Chapter 5.9 draws that line.
  2. Read less. Keep the rules short, load details only when needed, and ask for a diff and not the whole file. Danger: if it reads too little, it guesses, and the rework costs more than the reading saved.
  3. Write less. Ask for short answers, a one-line verdict, a short list. Danger: terse output the next step cannot use.
  4. Redo less. Agree the plan before the work starts, keep state in files so a lost session can be picked up, and cap the number of retries. Danger: caps that cut off useful work. Set them from what you saw, not from fear.

Also mind the standing costs. Every tool the agent has adds its name, description and parameters to the input of every call (chapter 1.2), and every rule you inject rides along on every turn.

Test your own setup

Three questions when a bill surprises you:

  1. Which call or turn read the most? Look at the usage page of your tool, and the biggest tasks.
  2. What was in the context that did not need to be there? Old tool results, a large file read in full, a long rules file?
  3. Did the task get redone? If so, why did the first attempt fail?

A small experiment. Take one task you know the right answer to. Run it on two models, three times each, and note the usage and whether it was right. Then compare cost per success. Do not decide from one run. A table with four columns is enough:

Run Model Usage (from your usage page) Right?
1 A
2 A
3 A
1 B
2 B
3 B

原理

一個任務花多少

把一個任務的成本,想成三個部分。

讀的 每次呼叫,扣掉快取命中 + 寫的 回答、程式碼、思考 × 返工 做了幾次 一件事做成的成本。多數省錢發生在第一項和最後一項。
一個任務的成本是模型讀的加寫的,再乘上返工的次數。

細看:

  1. 模型讀的,每次呼叫都算,快取命中的地方有折扣。
  2. 模型寫的:回答、程式碼、思考。
  3. 重做:答錯、丟了線索、再試一次。重做是第 1 和第 2 部的乘數。

大部分省下來的錢,來自第一和第三部分。一個好用的經驗法則是:用夠用的最便宜的方式做事。

強模型什麼時候比較便宜

更強的模型,每個 token 更貴。但它只要一次就得到答案,弱的要三次,這時它做完一個任務的成本仍然可能更低。如果任務很簡單,它也可能是浪費。

這個轉折點在哪裡,我自己沒有量過。這是論證,不是結果,找到那一點的辦法,是在你自己的任務上測:同樣幾個任務跑在兩個模型上,比較每次成功的成本,不是每次呼叫的成本。一次運行證明不了什麼 說了怎麼誠實地做。

依任務挑,並確認設定

我的經驗法則(沒有量過):用強模型來想(計劃、做法、難的 bug),用中等的模型來做(計劃已經清楚的改動)。有些工具也讓你設推理強度。Copilot 能不能從 harness 裡設定,我還沒測過,所以請查你自己的工具。

在 Copilot 裡怎麼計費(2026 年 10 月下旬)

這一段常常變,請確認日期和你自己的方案。

  • 按月的 Copilot 方案:從 2026 年 6 月 1 日起,Copilot 一直在轉向按用量計費。premium request unit 換成了 GitHub AI Credits,用量依輸入、輸出和快取的 token 算,按每個模型公開的 API 費率。程式碼補全和 next edit suggestions 仍包含在內。(GitHub blog)
  • 年繳的 Pro 和 Pro+ 方案,在到期之前仍用舊的 premium request 模式。在那裡,你送出的每個 prompt 算一次請求,再乘上該模型的倍率,agent 做的工具呼叫不算。(Copilot requests)

兩種模式獎勵的習慣不同。按請求計費時,一次長的自主執行對你很便宜,所以把大任務放進一個 prompt 做可能划算。按 token 計費時,長的執行花多少,和它讀了多少成比例。你用哪一種,決定了這一章其餘的部分有多重要。如果你的方案按 token 計費,它就很重要。

結果不對時,最後才換模型

結果不好時,最容易的反應是換成更大的模型。這是最貴的修法,而且常常是錯的。先看哪裡出了問題:

出了什麼問題 先試
它不知道一條規則、一個名字,或專案怎麼做這件事 把它寫進規則或知識檔。更大的模型也不知道
它說「做完了」,其實沒有 加一道檢查:執行測試(讓 agent 能檢查自己的活)
在長會話裡丟了線索 開一個新會話,把狀態放在檔案裡帶過去(長對話會變差)
它什麼都知道,推理還是不好 這時再試更強的模型

四種省法,以及各自怎樣過頭

  1. 少用模型。程式碼能做的,就不要問模型。計數、複製檔案、把 Word 檔轉成文字、檢查格式:腳本每次都做得一樣,也不用 token。危險是把判斷搬進一段不會判斷的程式。誰來判斷:程式、便宜模型、主模型、人 畫了那條線。
  2. 少讀。規則寫短,細節要用時才載入,要 diff 而不是整個檔案。危險:讀太少,它就會猜,而重做花的錢比省下來的讀取還多。
  3. 少寫。要簡短的回答、一行結論、一份短清單。危險:簡短到下一步用不上。
  4. 少重做。工作開始前先談好計劃,把狀態放在檔案裡,這樣丟掉的會話可以接著做,再限制重試的次數。危險:上限切斷了有用的工作。上限要依你看到的情況設,不要依恐懼設。

還要留意固定的成本。agent 有的每個工具,都會把它的名字、描述和參數,加進每次呼叫的輸入裡(Tool calling,和給 agent 設計工具),而你注入的每條規則,每一輪都跟著走。

測你自己的設定

帳單讓你吃驚的時候,問三個問題:

  1. 哪一次呼叫或哪一輪讀得最多?看你工具的用量頁面,和最大的幾個任務。
  2. 上下文裡有什麼是不必要在那裡的?舊的工具結果、整份讀進來的大檔案、很長的規則檔?
  3. 任務有沒有被重做?如果有,第一次為什麼失敗?

一個小實驗。挑一個你知道正確答案的任務。在兩個模型上各跑三次,記下用量和對不對。然後比較每次成功的成本。不要憑一次運行下決定。一個四欄的表就夠了:

運行 模型 用量(從你的用量頁面) 對嗎?
1 A
2 A
3 A
1 B
2 B
3 B

原理

一个任务的成本

把一个任务的成本看成三部分。

读的 每次调用,扣掉缓存命中 + 写的 回答、代码、思考 × 返工 做了几次 做完一个任务的成本。多数节省来自第一项和最后一项。
一个任务的成本,是模型读的加写的,再乘上返工的次数。

展开说:

  1. 模型读的,所有调用加起来,缓存命中的部分打折。
  2. 模型写的:回答、代码、思考。
  3. 返工:错误的回答、丢了的线索、重复的尝试。返工是第 1、2 部分的乘数。

大部分节省来自第一和第三部分。一条好用的经验法则是:用够用的办法里最便宜的那个。

什么时候强模型反而便宜

更强的模型,每个 token 更贵。但一次就答对,而弱的模型要试三次,那么做完一个任务,强模型花得可能更少。如果任务本来就简单,它也可能是浪费。

这个转折点在哪里,我没有自己的测量。这是一个推理,不是结果,找到这个点的办法,是在你自己的任务上测:同样的几个任务在两个模型上各跑一遍,比较每次成功的成本,而不是每次调用的成本。一次运行证明不了什么讲了怎么老老实实地做这件事。

按任务选,并检查设置

我的经验法则(没有测过):思考用强模型(计划、思路、难的 bug),动手用中等的模型(计划已经清楚的一处改动)。有些工具也能设置推理强度。Copilot 能不能从 harness 里设置,我没测过,请查你自己的工具。

Copilot 怎么计费(截至 2026 年 10 月下旬)

这部分经常变,请核对日期和你自己的套餐。

  • 按月的 Copilot 套餐:从 2026 年 6 月 1 日起,Copilot 在改成按用量计费。高级请求单位被 GitHub AI Credits 取代,用量按输入、输出和缓存的 token 计算,价格是每个模型公布的 API 费率。代码补全和下一处编辑建议仍然包含在内。(GitHub blog)
  • 年度的 Pro 和 Pro+ 套餐在到期前仍然用旧的高级请求模式。在这种模式里,你发的每个 prompt 算一次请求,再乘上该模型的倍率,agent 做的工具调用不算。(Copilot requests)

两种模式鼓励的习惯不同。按请求计费时,一次很长的自主运行对你来说很便宜,所以把大任务放在一个 prompt 里做,可能划算。按 token 计费时,一次长的运行,成本和它读了多少成正比。你用的是哪一种,决定了这一章其余部分对你有多要紧。如果你的套餐按 token 计费,那就非常要紧。

结果不对时,最后才换模型

结果不好,最容易想到的反应是换更大的模型。这是最贵的办法,也常常是错的。先看出了什么问题:

出了什么问题 先试什么
它不知道一条规则、一个名字,或者项目里的做法 写进规则或知识文件。更大的模型一样不知道
它说“做完了”,其实没有 加一道检查:跑测试(让 agent 能检查自己的活)
在长会话里它丢了线索 开一个新会话,把状态放在文件里带过去(长对话会变差)
它什么都知道,推理还是不行 这时再试更强的模型

四个省钱的办法,以及各自怎样会过头

  1. 少用模型。代码能做的,就别问模型。计数、复制文件、把 Word 文件转成文本、检查格式:脚本每次都做得一样,而且不用 token。危险在于把判断交给了判断不了的代码。谁来判断:代码、便宜模型、主模型、人划出了这条线。
  2. 少读。规则保持简短,细节需要时才加载,要 diff 而不是整个文件。危险:读得太少,它就会猜,返工的成本比省下的读取更高。
  3. 少写。要求简短的回答、一句话的结论、一个简短的清单。危险:输出太简略,下一步用不上。
  4. 少返工。动手前先商定计划,把状态放在文件里,丢了的会话才能接着做,并且给重试的次数设上限。危险:上限卡掉了有用的工作。上限要根据你见到的情况来定,不要根据恐惧来定。

还要留意固定成本。agent 拥有的每个工具,都会把名字、描述和参数加进每次调用的输入里(Tool calling,以及怎么给 agent 设计工具),你注入的每条规则,每一轮都会带着走。

测一测你自己的配置

账单让你意外的时候,问三个问题:

  1. 哪次调用或哪一轮读得最多?看你工具的用量页面,和最大的那几个任务。
  2. 上下文里有什么是不必在那里的?旧的工具结果、整个读进来的大文件、很长的规则文件?
  3. 任务有没有被重做?如果有,第一次为什么失败?

一个小实验。挑一个你知道正确答案的任务。在两个模型上各跑三次,记下用量和对不对。然后比较每次成功的成本。别凭一次运行下结论。一张四列的表就够了:

第几次 模型 用量(来自你的用量页面) 对吗?
1 A
2 A
3 A
1 B
2 B
3 B

Costs and traps

What I can and cannot see. My own harness records no token counts in its log. For how much a task really used, I read the platform's session record, which reports credits and premium requests and which I do not convert to money. So everything I say about savings is "by design", not a bill.

The savings are not free. A hook that runs on tool calls adds a small delay. Independent checks call the model again. Rules injected on every prompt cost tokens on every prompt. I accept those on purpose, for control, but they are costs.

Reading too little is the trap that looks like thrift. The cheapest call is the one that guesses wrong and is thrown away. If a task keeps failing for lack of context, the answer is to give it the right context, not to give it less.

Prices and plans change. The cache multipliers and the Copilot billing above are as of the dates in the sources, and I would expect them to move.

代價和陷阱

我看得到的和看不到的。我自己的 harness,日誌裡沒有記 token 數。一個任務真正用了多少,我讀的是平台的會話紀錄,它回報 credits 和 premium request,我不把它們換算成錢。所以我說的所有節省,都是「依設計」,不是帳單。

省下來的東西不是免費的。在工具呼叫上執行的 hook,會加一點小延遲。獨立檢查要再呼叫模型。每個 prompt 都注入的規則,每個 prompt 都花 token。我是為了控制才刻意接受這些,但它們是成本。

讀太少,是看起來像節儉的陷阱。最便宜的呼叫,是猜錯然後被丟掉的那一個。如果一個任務一直因為缺上下文而失敗,答案是給它對的上下文,不是給它更少。

價格和方案會變。上面的快取倍率和 Copilot 計費,是來源上那些日期的情況,我預期它們會變。

代价和坑

我看得到什么,看不到什么。我自己的 harness 在日志里不记录 token 数。一个任务实际用了多少,我看的是平台的会话记录,它报告的是 credits 和高级请求,我不把它们换算成钱。所以我说的所有节省,都是“按设计”的,不是账单上的数字。

节省不是免费的。在工具调用上运行的 hook 会带来一点延迟。独立检查要再调用一次模型。每个 prompt 都注入的规则,每个 prompt 都要花 token。我是为了控制,有意接受这些,但它们是成本。

读得太少,是看起来像节俭的坑。最便宜的调用,是猜错然后被扔掉的那一次。如果一个任务总是因为缺上下文而失败,答案是给它正确的上下文,不是给得更少。

价格和套餐会变。上面的缓存倍率和 Copilot 的计费,都是截至来源里写的日期,我预计它们会变。

What the industry says Industry practice

  • Anthropic prices cached reads at a fraction of normal input, and says tool definitions are counted as input tokens on every request. (Prompt caching, Tool use)
  • GitHub moved Copilot from counting requests to counting tokens. (GitHub blog)

The direction is the same: the market now charges for what the model reads and writes, which is the argument of this chapter.

業界怎麼說 業界做法

  • Anthropic 把讀取快取的價格,訂成一般輸入的一小部分,並說工具定義在每個請求裡都算輸入 token。(Prompt caching、Tool use)
  • GitHub 把 Copilot 從算請求,改成算 token。(GitHub blog)

方向一致:市場現在按模型讀和寫的東西收費,這正是這一章的論點。

业界怎么说 业界做法

  • Anthropic 把读缓存的价格定为普通输入的一小部分,并说工具定义在每次请求里都按输入 token 计算。(Prompt caching、Tool use)
  • GitHub 把 Copilot 从按请求计费改成了按 token 计费。(GitHub blog)

方向是一致的:市场现在按模型读和写的量收费,这正是这一章的论点。

When to delete it Industry practice

Much of this chapter is advice for a world where reading is expensive. If reading becomes cheap enough that the extra work of trimming context costs more than it saves, drop the tricks and keep the habit of measuring cost per finished task. I would also look again if a platform started showing per-task cost on its own, because then you would need to guess less.

什麼時候可以刪掉 業界做法

這一章有很多是為「讀取很貴」的世界寫的建議。如果讀取便宜到,修剪上下文多出來的工夫,比省下的還貴,就丟掉這些技巧,只留下量測每個完成任務成本的習慣。如果某個平台開始自己顯示每個任務的成本,我也會重新看一遍,因為那時你要猜的就少了。

什么时候可以删掉 业界做法

这一章的很多内容,是给读取很贵的世界的建议。如果读取便宜到,精简上下文的额外功夫比省下的更贵,就把这些技巧丢掉,只留下核算每个完成任务的成本这个习惯。如果哪个平台开始自己显示每个任务的成本,我也会重新考虑,因为那时你需要猜的就少了。

Try it on your own setup

No harness needed. Open your agent in your own repository and paste:

Estimate what is sent to the model on every call in this repository, before I type anything. List each always-loaded item: instruction files, rules, tool and server definitions, anything injected automatically. Give each an approximate size in tokens (a rough count is fine) and sort the list from largest to smallest. Mark items that look stale, duplicated or rarely useful. Do not change any files.

在你自己的環境試試

不用裝 harness。在你自己的倉庫裡打開 agent,貼上:

估算在這個倉庫裡,我還沒打任何字之前,每次呼叫都會送給模型什麼。列出每一個一直載入的項目:指令檔、規則、工具和 server 的定義、任何自動注入的東西。給每一項一個大概的 token 大小(粗略估計就好),並由大到小排序。標出看起來過期、重複或很少用到的項目。不要改任何檔案。

在你自己的环境里试试

不用装 harness。在你自己的仓库里打开 agent,粘贴:

估算一下在这个仓库里,我什么都没输入之前,每次调用都会发给模型的内容。列出每一项始终加载的东西:指令文件、规则、工具和 server 的定义,以及所有自动注入的内容。每项给一个大概的 token 数(粗略数一下即可),并按从大到小排序。把看起来过期、重复或很少有用的项标出来。不要修改任何文件。

Go deeper

延伸閱讀

延伸阅读

You can stop here: that is the whole idea. The next part shows where it runs, so you can look inside.讀到這裡就夠了,這就是整個想法。下一部分告訴你它在哪裡運行,可以進去看看。读到这里就够了,核心思路就是这些。下一部分告诉你它在哪里跑,可以进去看看。

In my practice (Copilot Harness)實作实战

Copilot Harness is my open-source harness for VS Code Copilot.

Repository: https://github.com/Smars-Bin-Hu/copilot-agentic-harness

Get started: clone it, open it in VS Code, and type /harness-rookie-get-me-ready in Copilot Chat. You need Python 3.9 or later and git.

Copilot Harness 是我開源的 VS Code Copilot harness。

倉庫: https://github.com/Smars-Bin-Hu/copilot-agentic-harness

開始:clone 下來,用 VS Code 打開,在 Copilot Chat 裡輸入 /harness-rookie-get-me-ready。需要 Python 3.9 以上和 git。

Copilot Harness 是我开源的 VS Code Copilot harness。

仓库: https://github.com/Smars-Bin-Hu/copilot-agentic-harness

上手:把仓库 clone 下来,用 VS Code 打开,在 Copilot Chat 里输入 /harness-rookie-get-me-ready。需要 Python 3.9 及以上版本和 git。

Explore it in the harness

My harness is built around the idea of reading less and redoing less: rules are loaded in layers, state lives in files, checks are off unless you ask. It also says plainly what it cannot see about cost. The harness can show you each of these from the inside, with the real files. After the setup above, type:

/harness-rookie-wondering I have read the chapter "Choosing a model: cost per finished task". Teach me how this harness reads less, writes less and redoes less, and what it can and cannot tell me about the real usage of a task. Open the real files with me and draw how the pieces fit together.

It teaches one small piece at a time, stops to ask if you are following, and opens the files with you.

在 harness 裡探索

我的 harness 是圍繞「少讀、少重做」這個想法搭的:規則分層載入、狀態放在檔案裡、檢查除非你要求否則是關的。它也直接說出它看不到哪些成本。harness 可以從裡面把這些一項項展示給你看,帶著真實的檔案。照上面的說明準備好之後,輸入:

/harness-rookie-wondering 我讀完了「選模型:算的是每次成功的成本」這一章。請教我這個 harness 怎麼少讀、少寫、少重做,以及它能和不能告訴我一個任務的真實用量。帶我打開真實的檔案,並畫出各部分怎麼接在一起。

它一次教一小塊,停下來確認你跟得上,然後和你一起打開檔案。

在 harness 里探索

我的 harness 围绕“少读、少返工”来搭:规则分层加载,状态放在文件里,检查不要求就不开。它也直说了在成本上它看不到什么。harness 可以从内部带你逐个看这些,配上真实的文件。按上面的说明准备好之后,输入:

/harness-rookie-wondering 我读完了“选模型:看每次成功的成本”这一章。请教我这个 harness 是怎么少读、少写、少返工的,以及它能告诉我、不能告诉我一个任务的真实用量的哪些方面。带我打开真实的文件,并画出各部分是怎么连起来的。

它一次只教一小块,停下来确认你跟上了,再和你一起打开文件。