3.1
Context is a budget上下文是一份預算上下文是一份预算
This chapter assumes you have read The agent loop, or know that an agent reads text and answers with text.
讀這一章,請先讀過 agent 循環,和你能插手的地方,或至少知道 agent 讀文字、再用文字回答。
读这一章,请先读过 agent 循环,以及你能插手的地方,或者至少知道 agent 读文本、再用文本回答。
The Knowledge經驗分享经验分享
In one sentence
Rules, knowledge, files and tool results all share one window, and each one crowds the others out. So manage context like a budget: fetch it when needed instead of loading it all.
一句話
規則、知識、檔案和工具結果共用同一個視窗,每一樣都會擠掉別的。所以要把上下文當預算來管:需要時再去取,不要一次全部載入。
一句话
规则、知识、文件和工具结果共用同一个窗口,每一样都在挤占其他几样的位置。所以要像管预算一样管上下文:需要时再取,不要一次全装进去。
For leaders
- The decision: treat what the agent reads as a budget. Decide what is always loaded and what is fetched only when a task needs it.
- The risk: everything shares one window, and a window with room left is not one that performs well, so a rule buried in a long, noisy context is more likely to be missed. I have no number for how much; the advice comes from the platform makers and my own design, not from a test of mine.
- The cost of doing it right: mostly setup time: layering the rules, shortening tool output, and keeping any index true. Loading too little has its own price: the agent guesses.
- Ask the team: "What is loaded on every turn, and how much of it applies to the task in front of us?"
給主管
- 要做的決定:把 agent 讀的東西當預算。決定什麼東西一直載入,什麼東西只在任務需要時才去取。
- 風險:所有東西共用一個視窗,而且還有空位的視窗不等於表現好的視窗,所以埋在又長又雜的上下文裡的規則,更容易被漏掉。漏掉的程度我沒有數字;這些建議來自平台廠商和我自己的設計,不是我做的測試。
- 做對的成本:主要是設定的時間:把規則分層、縮短工具輸出、讓任何索引保持正確。載入太少也有代價:agent 會猜。
- 可以問團隊:「每一輪都載入了什麼?其中有多少和眼前的任務有關?」
写给管理者
- 要拍板的事:把 agent 读到的内容当成预算。决定哪些每次都加载,哪些只在任务需要时才取。
- 风险:所有内容共用一个窗口,窗口还有空位不等于表现就好,所以埋在又长又杂的上下文里的规则,更容易被漏掉。漏掉多少,我没有数字;这条建议来自平台厂商和我自己的设计,不是我做过的测试。
- 做对的代价:主要是配置的时间:给规则分层、缩短工具输出、让索引始终保持准确。装得太少也有代价:agent 会去猜。
- 可以问团队:“每一轮都加载了什么?其中有多少和眼前这个任务有关?”
The problem
The usual advice for a weak answer is "give the AI more information". Paste the whole style guide. Add every rule. Let it read all the files. It feels safe: more facts, fewer mistakes.
It often goes the other way. Think of a desk. You can lay out every document for a project, and then you cannot find the one you need, and there is no room to write. An agent has a desk like that. It is called the context window: all the text the model can look at in one turn. It holds the rules, your words, the files the agent read, the results of its tool calls and its own earlier answers. It has a size limit. And, as I will show, it has a second limit that is not about size.
What I can say comes from two places. The first is from the people who build these tools. Anthropic writes that context "must be treated as a finite resource with diminishing marginal returns" (Effective context engineering). VS Code's guide says to "avoid context overload that can dilute focus" (Context engineering guide). The second is the way I built my own setup: almost every design choice in it is a way to put less into the window. The cost side of the same idea is in Models and cost: most of what a task costs is what the model reads. I did not run a clean experiment for this chapter.
問題
回答不好時,常見的建議是「給 AI 更多資訊」。貼上整份風格指南。加上每一條規則。讓它讀所有檔案。這感覺很安全:事實越多,錯誤越少。
結果常常相反。想想一張書桌。你可以把一個專案的所有文件都攤開,然後你找不到要的那一份,也沒有地方寫字。agent 就有這樣一張書桌。它叫上下文視窗:模型在一輪裡能看到的全部文字。裡面有規則、你說的話、agent 讀過的檔案、工具呼叫的結果,還有它自己之前的回答。它有大小的上限。而且,我接下來會說,它還有第二個限制,跟大小無關。
我能說的,來自兩個地方。第一個是做這些工具的人。Anthropic 寫道,上下文「必須被當成一種有限的資源,邊際報酬會遞減」(Effective context engineering)。VS Code 的指南說要「避免上下文過載而分散注意力」(Context engineering guide)。第二個是我自己搭設定的方式:裡面幾乎每個設計選擇,都是一種讓視窗裡少放一點的辦法。同一個想法在成本上的一面,在選模型:算的是每次成功的成本:一個任務的成本,大部分是模型讀的東西。我沒有為這一章做過乾淨的實驗。
问题
AI 答得不好,常见的建议是“多给它点信息”。把整份风格指南贴进去,把所有规则都加上,让它读完所有文件。这样做感觉很稳:事实越多,错误越少。
结果往往相反。想想一张书桌。你可以把一个项目的所有文档都摊开,然后就找不到要用的那一份,也没地方写字了。agent 也有这样一张书桌,叫上下文窗口:模型在一轮里能看到的全部文本。里面有规则、你说的话、agent 读过的文件、工具调用的结果,以及它自己之前的回答。它有大小上限。而且,我后面会讲到,它还有第二个上限,和大小无关。
我能说的,来自两个地方。第一个是做这些工具的人。Anthropic 写道,上下文“必须当作一种有限的资源来对待,边际收益会递减”(Effective context engineering)。VS Code 的指南说要“避免上下文过载而分散注意力”(Context engineering guide)。第二个是我自己搭配置的方式:里面几乎每个设计选择,都是想办法让窗口里少放一点。同一个想法在成本上的一面,在选模型:看每次成功的成本里:一个任务的成本,大部分来自模型读的东西。这一章我没有做过干净的实验。
How it works
What is in the window
- The window is a fixed size. Before you type anything, the rules and the list of tools already take a part of it.
- Each file the agent reads goes into the window as whole text.
- Every tool result goes in too: a search listing, a command's output, a test log.
- Add the conversation itself, and the window is full. Every new item now pushes something older out or squeezes it into a summary.
Six things share it:
- Rules and instructions that the platform puts in every time.
- The list of tools, with each tool's description. The model needs it to know what it can ask for. With many tools, for example from several MCP servers (MCP), this list can be a large part of the window. I have not measured how large.
- Your messages.
- Files the agent read. A file goes in as whole text, however much of it matters.
- Tool results: search listings, command output, test logs. These are often the biggest part, and the part nobody looks at.
- The agent's own earlier answers.
Each turn of the loop the model reads all of it again, plus one more result (the cost of this is in Models and cost). So the window only grows, until it is full. Then the platform has to cut or summarise the oldest parts, and details are lost. Long chats get worse is about that.
Two limits, not one
Size is the obvious limit. The second one is attention. Anthropic describes it as an "attention budget" that the model draws on when it reads a lot of text. The practical meaning: a window that still has room is not a window that performs well. A rule buried in a long, noisy context is more likely to be missed than the same rule in a short one. I take this as a reason to put less in, even when it all fits. That step is my own inference, not something Anthropic says. How big the effect is depends on the model and the task, and I have not measured it.
Load everything, load an index, or both
Here is the choice behind most of the decisions in this part of the book.
Load everything first. The agent starts with all the rules and all the notes in the window. It never has to look anything up. But the window is spent before work starts, most of it on things this task does not need.
Load an index, fetch what the task needs. The agent starts with a short list: names and one-line descriptions of the things that exist. When the task needs one of them, it opens that one. Anthropic calls this a "just in time" approach: the agent keeps lightweight identifiers (a file path, a name) and loads the data at run time.
Both. Anthropic's own suggestion is a mix: "the most effective agents might employ a hybrid strategy, retrieving some data up front for speed" and exploring further on their own. Their example is Claude Code, which drops its instruction file into the context up front and uses search tools to find the rest as it goes.
The other side wins in some cases. For a small task, or a small set of rules, loading everything is simpler and costs little. And where a missed fact is expensive, I would rather pay for it to be in the window than bet on the agent opening the right file.
One more point on cost. A beginning that is loaded every time and never changes can be cached, and a cache read is much cheaper than a normal read (Models and cost). So "load everything" is cheaper than the token count suggests, if the beginning stays stable. The attention limit above does not go away with a cache. I have not tested the cache on Copilot.
It is the same as a library. You do not carry the whole library to your desk. You use the catalog, then fetch the one book.
For this to work, three things must be true.
- The index entry must say when to open the thing. The agent decides from the one line alone. A vague line means it opens the wrong thing or never opens the right one. This is why the description of a skill matters so much (Instructions and skills).
- The fetch must be cheap and exact. If fetching means reading a whole folder, the index saved nothing.
- The agent must know it does not know. An index cannot help with a thing the agent does not realise is missing. That is the real risk, and I come back to it below.
Ways to read less
These are the ones I use. Each is a way to keep a thing out of the window, or to take it out. The two cheapest to start with are layering your rules and shortening tool output.
- Rules by layers. Keep a small set of rules always loaded. Put the rest where they are read only for the files or the job they apply to. Rules that never change the next step are only noise. (How: Instructions and skills.)
- Search first, then read. Ask the agent to find where something is, then read only that part of the file, not the whole file. Or give it a short map of the project written ahead of time: an index in the sense above. It only wins if reading it beats searching, and it only stays true if someone keeps it true. I have not tested whether mine wins (A knowledge base from code).
- Make tool output short. A command that prints 2,000 lines puts 2,000 lines in the window. Filter it, or print only the failures.
- Remind with a few lines, not a page. In my setup, only a short piece of the rules is added to each prompt. With a task (Manage tasks, not chats), it says how far the task has got. It is a reminder, and the full text is read when it is needed.
- Hand a big read to a subagent (Use subagents sparingly, Long chats get worse).
- Write state into files, so a new chat reads one short file and not the old conversation. A new chat is an empty window; when to start one, and what to carry over, is in Long chats get worse.
Test your own setup
Three questions:
- What is loaded every time? Count the lines of your always-on rules. How much of that applies to the task you are doing now? (In Claude Code, the
/contextcommand shows what is using the window. I have not checked what Copilot offers.) - What was the biggest thing the agent read last time? A file? A log? Was all of it needed?
- If the agent started a new chat now, what would it have to read to carry on? Is that in a file?
A test you can run today. Take a task you know the answer to. Run it twice in a new chat each time. In the first, paste the whole file or doc in the prompt. In the second, name the file and the part, and let the agent read only that. Compare the answer, the time and the usage. To trust the difference, repeat it a few times each, and score both runs the same way. For each run, write down four things: whether the answer was right, the time, the usage your tool shows, and anything the agent missed.
原理
視窗裡有什麼
- 窗口的大小是固定的。你還沒打字,規則和工具清單就已經占了一部分。
- agent 讀的每個檔案,都以整段文字放進窗口。
- 每個工具結果也要放進去:搜尋列表、指令輸出、測試日誌。
- 再加上對話本身,窗口就滿了。之後每多放一樣,就要把更早的擠出去,或壓成摘要。
有六樣東西共用它:
- 規則和指令,平台每次都會放進去。
- 工具清單,附上每個工具的描述。模型要靠它知道自己可以請求什麼。工具很多時,例如來自好幾個 MCP server(MCP server:插工具的標準接口),這份清單可能佔視窗的一大塊。有多大,我沒量過。
- 你的訊息。
- agent 讀過的檔案。檔案是整份文字放進去的,不管其中有多少是相關的。
- 工具結果:搜尋的清單、指令的輸出、測試紀錄。它們常常是最大的一塊,也是沒人看的一塊。
- agent 自己之前的回答。
迴圈每一輪,模型都會把這一切重讀一遍,再加上多出來的一個結果(這的成本在選模型:算的是每次成功的成本)。所以視窗只會越來越大,直到滿了。然後平台只能裁掉或摘要最舊的部分,細節就丟了。長對話會變差講的就是這個。
是兩個限制,不是一個
大小是明顯的限制。第二個是注意力。Anthropic 把它描述成模型在讀大量文字時消耗的一份「注意力預算」。實際的意思是:還有空位的視窗,不等於表現好的視窗。埋在又長又雜的上下文裡的規則,比在短上下文裡的同一條規則,更容易被漏掉。我拿這個當理由:就算全都放得下,也要少放一點。這一步是我自己的推論,不是 Anthropic 說的。影響有多大,取決於模型和任務,我沒有量過。
全部載入、載入索引,或兩者都用
這是這一部分大多數決定背後的選擇。
先全部載入。agent 一開始,視窗裡就有所有規則和所有筆記。它不需要查任何東西。但是工作還沒開始,視窗就花掉了,而且大部分花在這個任務用不到的東西上。
載入索引,需要什麼再去取。agent 一開始只有一份簡短的清單:現有的每樣東西的名稱和一行描述。任務需要其中一樣時,它才打開那一樣。Anthropic 把這叫做「即時」做法:agent 帶著輕量的識別(檔案路徑、名稱),到執行時才載入資料。
兩者都用。Anthropic 自己的建議是混合:「最有效的 agent 也許會採用混合策略,先取一部分資料求速度」,然後讓它自己繼續探索。他們的例子是 Claude Code:先把它的指令檔放進上下文,其餘的在過程中用搜尋工具去找。
有些情況是另一邊贏。任務小、或規則少時,全部載入比較簡單,成本也低。而漏掉一個事實代價很高的地方,我寧可花錢把它放在視窗裡,也不賭 agent 會打開對的檔案。
再說一點成本。每次都載入、又從不變的開頭,可以被快取,而讀快取比一般的讀便宜很多(選模型:算的是每次成功的成本)。所以只要開頭保持穩定,「全部載入」會比 token 數看起來便宜。上面說的注意力限制,不會因為快取而消失。我沒有在 Copilot 上測過快取。
這和圖書館一樣。你不會把整座圖書館搬到書桌上。你用目錄,然後取那一本書。
要讓這個做法行得通,三件事必須成立。
- 索引的條目必須說明什麼時候該打開它。agent 只憑那一行就做決定。含糊的一行,會讓它打開錯的東西,或是永遠不打開對的。這就是 skill 的描述為什麼這麼重要(指令與 skill:只載入需要的)。
- 取用必須便宜而且精確。取用如果要讀整個資料夾,索引就什麼也沒省。
- agent 必須知道自己不知道。對於 agent 沒意識到缺了的東西,索引幫不上忙。這才是真正的風險,下面再談。
少讀一點的辦法
這些是我在用的。每一個都是讓某樣東西不進視窗,或把它拿出來的辦法。最便宜、最適合先做的兩個,是把規則分層,和縮短工具輸出。
- 規則分層。一小組規則一直載入。其餘的放在只有讀到對應的檔案或工作時才會讀的地方。不會改變下一步的規則,只是雜訊。(做法:指令與 skill:只載入需要的。)
- 先搜尋,再讀。請 agent 先找出東西在哪,然後只讀檔案中那一部分,不讀整個檔案。或者給它一份事先寫好的專案簡短地圖:就是上面說的那種索引。只有讀地圖比搜尋划算時,它才贏;也只有有人讓它保持正確,它才一直是對的。我沒有測過我的地圖是不是划算(知識庫:程式碼的誠實索引)。
- 讓工具輸出變短。印出 2,000 行的指令,就把 2,000 行放進視窗。把它過濾掉,或只印失敗的部分。
- 用幾行來提醒,不用一整頁。在我的設定裡,每個 prompt 只加上規則的一小段。有任務時(管任務,不管對話),它說的是任務做到哪了。這只是提醒,完整的內容在需要時才讀。
- 把大量閱讀交給子 agent(慎用子 agent、長對話會變差)。
- 把狀態寫進檔案,這樣新的對話讀一個簡短的檔案就好,不用讀舊的對話。新的對話就是空的視窗;什麼時候開一個、帶什麼過去,在長對話會變差。
測一測你自己的設定
三個問題:
- 每次都載入了什麼?數一數你一直開著的規則有幾行。其中有多少和你現在做的任務有關?(在 Claude Code 裡,
/context指令會顯示什麼在佔用視窗。Copilot 有什麼,我沒查過。) - agent 上一次讀的最大一樣東西是什麼?一個檔案?一份紀錄?全部都需要嗎?
- 如果 agent 現在開一個新的對話,它得讀什麼才能接著做?那些在檔案裡嗎?
今天就能跑的測試。挑一個你知道答案的任務。各開一個新對話,跑兩遍。第一遍,把整個檔案或文件貼在 prompt 裡。第二遍,只說出檔案和那一部分,讓 agent 只讀那裡。比較答案、時間和用量。要相信這個差別,就各重複幾次,兩邊用同樣的方式打分。每次記下四樣:答案對不對、時間、你的工具顯示的用量,以及 agent 漏掉了什麼。
原理
窗口里有什么
- 窗口的大小是固定的。你还没打字,规则和工具列表就已经占了一部分。
- agent 读的每个文件,都以整段文本放进窗口。
- 每个工具结果也要放进去:搜索列表、命令输出、测试日志。
- 再加上对话本身,窗口就满了。之后每多放一样,就要把更早的挤出去,或者压成摘要。
有六样东西共用它:
- 规则和指令,由平台每次都放进去。
- 工具列表,附上每个工具的描述。模型靠它知道自己能请求什么。工具很多时,比如来自好几个 MCP server(MCP server:接工具的标准接口),这份列表可能占窗口很大一块。有多大,我没量过。
- 你发的消息。
- agent 读过的文件。 文件是整段文本放进去的,不管其中真正有用的有多少。
- 工具结果:搜索列表、命令输出、测试日志。它们常常是最大的一块,也是没人去看的一块。
- agent 自己之前的回答。
循环每转一轮,模型都要把这些内容重新读一遍,再加上新增的一条结果(这笔账在选模型:看每次成功的成本里)。所以窗口只会越来越满,直到装不下。到那时,平台只能截掉或总结最早的部分,细节就丢了。长对话会变差讲的就是这件事。
是两个上限,不是一个
大小是显而易见的上限。第二个是注意力。Anthropic 把它叫作“注意力预算”,模型读很多文本时要从里面扣。实际的意思是:窗口还有空位,不等于表现就好。一条规则埋在又长又杂的上下文里,比同一条规则放在短上下文里,更容易被漏掉。我把这当成少放的理由,哪怕东西全都装得下。这一步是我自己的推断,不是 Anthropic 说的。影响有多大,取决于模型和任务,我没量过。
全装进去、只放索引,还是两者结合
这一部分里大多数决定,背后都是这个选择。
先全装进去。 agent 一开始,窗口里就有所有规则、所有笔记。它不用查任何东西。但是工作还没开始,窗口就已经花掉了,而且大部分花在这个任务用不上的东西上。
只放索引,需要什么再取。 agent 一开始只有一份简短的列表:现有各样东西的名字和一行说明。任务需要其中某一样时,它再打开那一样。Anthropic 把这叫作“即时”(just in time)做法:agent 只保留轻量的标识(文件路径、名字),在运行时再加载数据。
两者结合。 Anthropic 自己的建议是混合:“最有效的 agent 可能采用混合策略,先取一部分数据来保证速度”,再自己继续探索。他们举的例子是 Claude Code:它事先把指令文件放进上下文,其余的在工作中用搜索工具去找。
另一边在有些情况下更合适。任务小,或者规则少,全装进去更简单,花不了多少。而在漏掉一条事实代价很高的地方,我宁愿花钱把它放进窗口,也不愿赌 agent 会打开对的文件。
再说一点成本。每次加载、又从不变化的开头,可以缓存,而读缓存比正常读便宜得多(选模型:看每次成功的成本)。所以只要开头保持稳定,“全装进去”比 token 数看起来的要便宜。但缓存解决不了上面说的注意力上限。我没有在 Copilot 上测过缓存。
这和图书馆一样。你不会把整座图书馆搬到书桌上。你先查目录,再去取那一本书。
要让这个办法管用,有三件事必须成立。
- 索引里的条目必须说清什么时候该打开它。 agent 只凭那一行就做决定。说得含糊,它就会打开错的东西,或者永远不打开对的那个。这就是 skill 的描述为什么那么重要(指令与 skill:只加载需要的)。
- 取用必须便宜而且准确。 如果取一样东西要读整个文件夹,索引就什么也没省下。
- agent 必须知道自己不知道。 索引帮不了 agent 没意识到缺了的东西。这才是真正的风险,下面我再回到这一点。
少读一点的办法
下面是我在用的办法。每一种,要么不让东西进窗口,要么把它拿出来。最容易上手的两个是:给规则分层,缩短工具输出。
- 规则分层。 留一小组规则始终加载。其余的放在只有处理相关文件或相关工作时才会读到的地方。不会改变下一步动作的规则,只是噪音。(做法:指令与 skill:只加载需要的。)
- 先搜索,再阅读。 让 agent 先找到东西在哪,再只读文件的那一部分,而不是整个文件。或者事先写好一份简短的项目地图给它:就是上面说的那种索引。只有读地图比搜索划算,它才算赢;也只有有人让它保持准确,它才一直可信。我没有测过我的地图是不是划算(知识库:代码的诚实索引)。
- 让工具输出变短。 一条命令打印 2,000 行,窗口里就多了 2,000 行。把它过滤一下,或者只打印失败的部分。
- 用几行提醒,不要一整页。 在我的配置里,每条 prompt 只附加规则里很短的一小段。有任务时(管任务,不管对话),它说明任务进行到了哪里。这只是提醒,需要时才会读完整的内容。
- 把大块的阅读交给子 agent(慎用子 agent、长对话会变差)。
- 把状态写进文件,这样新对话读一个简短的文件就行,不用读旧的对话。新对话就是一个空窗口;什么时候开新对话、带什么过去,见长对话会变差。
检验你自己的配置
三个问题:
- 每次都加载了什么?数一数你那些始终开启的规则有多少行。其中有多少和你现在做的任务有关?(在 Claude Code 里,
/context命令能显示什么在占用窗口。Copilot 提供什么,我没查过。) - agent 上一次读的最大的一样东西是什么?是文件?日志?全都需要吗?
- 如果 agent 现在开一个新对话,它要读什么才能接着干?这些东西在文件里吗?
今天就能跑的一个测试。 找一个你知道答案的任务。各开一个新对话,跑两次。第一次,把整个文件或文档贴在 prompt 里。第二次,只说出文件名和相关的部分,让 agent 只读那一部分。比较答案、时间和用量。想让差异可信,每种多跑几次,而且两种做法用同样的方式打分。每次运行记下四样:答案对不对、时间、你的工具显示的用量,以及 agent 漏掉了什么。
Costs and traps
Loading less can lose something the agent needed. An index is a bet that the agent will open the right thing. When it does not, it works from a guess. The fix is better descriptions, and a check that the thing was opened when it should be. To check, run a task you know needs a certain file, look at the tool's log of what was opened, and count how often it was not. A new chat, a rule or a skill is lost the same way.
Summaries lose details. A subagent's report and a platform's compression both shrink text, and both can drop the one line that mattered. Use them where a short version is enough.
An index must be kept true. A map that is out of date costs more than it saves, because the agent trusts it. Keep it true is about that.
No numbers here. I know of no clean measure of how much attention is lost as a window fills, for Copilot's models. The advice in this chapter comes from the platform makers and from my own design, not from a test of mine.
代價和陷阱
載入得少,可能丟掉 agent 需要的東西。索引是一場賭注:賭 agent 會打開對的東西。它沒打開,就只能憑猜測做事。補救是更好的描述,以及一個確認該打開時有沒有打開的檢查。要檢查,就跑一個你知道需要某個檔案的任務,看工具紀錄裡打開了哪些,數它沒打開的次數。新的對話、規則或 skill,丟失的方式也一樣。
摘要會丟細節。子 agent 的報告和平台的壓縮,都會把文字縮短,也都可能丟掉最要緊的那一行。用在簡短的版本就夠的地方。
索引必須保持正確。過期的地圖,花的比省的多,因為 agent 會相信它。讓知識庫一直是對的講的就是這個。
這裡沒有數字。對於 Copilot 的模型,視窗填滿時失去多少注意力,我不知道有任何乾淨的量法。這一章的建議來自平台廠商和我自己的設計,不是我做的測試。
代价和坑
装得少,可能丢掉 agent 需要的东西。 索引是一场赌注:赌 agent 会打开对的东西。它没打开时,就只能凭猜测干活。补救办法是把描述写得更好,再检查该打开的时候有没有打开。检查的方法:跑一个你知道需要某个文件的任务,看工具里记录的打开了什么,数一数有多少次没打开。新对话、规则或 skill 也是同样的道理,也会这样丢掉。
摘要会丢细节。 子 agent 的报告和平台的压缩,都会把文本缩短,也都可能丢掉最关键的那一行。只在简短版本够用的地方用它们。
索引必须保持准确。 过期的地图,花的比省的多,因为 agent 会信它。让知识库一直是对的讲的就是这个。
这里没有数字。 对于 Copilot 的模型,窗口越满、注意力丢掉多少,我不知道有什么干净的衡量办法。这一章的建议来自平台厂商和我自己的设计,不是我做过的测试。
What the industry says Industry practice
- Anthropic: context is "a finite resource with diminishing marginal returns", and agents can load data "just in time" by keeping light references. (Effective context engineering)
- VS Code: "Start with concise context and expand only when needed." (Context engineering guide)
The two agree on the direction. Neither gives a number I can quote for how much to load.
業界怎麼說 業界做法
- Anthropic:上下文是「有限的資源,邊際報酬遞減」,agent 可以靠帶著輕量的參照,「即時」地載入資料。(Effective context engineering)
- VS Code:「從簡短的上下文開始,只在需要時才擴充。」(Context engineering guide)
兩家在方向上一致。兩家都沒有給我能引用的數字,說明該載入多少。
业界怎么说 业界做法
- Anthropic:上下文是“有限的资源,边际收益递减”,agent 可以通过保留轻量的引用来“即时”加载数据。(Effective context engineering)
- VS Code:“从简洁的上下文开始,需要时再扩充。”(Context engineering guide)
两家在方向上一致。但都没有给出我能引用的数字,说明该加载多少。
When to delete it Industry practice
The reasoning stays as long as the window has a size and the model reads it every turn. What may go is my own effort: manual layering of rules and manual new chats. If the platform loads rules by relevance well, and compresses without dropping what matters, I will stop doing it by hand. A very large window does not remove the second limit by itself. I would want a test of that first.
什麼時候可以刪掉 業界做法
只要視窗有大小、模型每一輪都要讀它,這套道理就還在。可能會消失的,是我自己花的力氣:手動把規則分層、手動開新對話。如果平台能按相關性把規則載入得很好,壓縮時又不丟掉要緊的東西,我就不再手動做這些。很大的視窗本身,不會讓第二個限制消失。我會想先看到這方面的測試。
什么时候可以删掉 业界做法
只要窗口有大小,模型每一轮都要读它,这套道理就在。可能会去掉的,是我自己的功夫:手动给规则分层,手动开新对话。如果平台能按相关性把规则加载好,压缩时又不丢重要的内容,我就不再手动做了。窗口再大,也不会自动消除第二个上限。我要先看到这方面的测试。
Try it on your own setup
No harness needed. Open your agent in your own repository and paste:
Look at the commands this project uses to test, lint, build and search. For each, tell me roughly how much text it prints when it succeeds and when it fails, and whether that output would flood your context window if you ran it. Suggest a quieter way to run each one, such as printing only failures or a summary. Rank the three noisiest. Do not change any files, and do not run anything that changes state.在你自己的環境試試
不用裝 harness。在你自己的倉庫裡打開 agent,貼上:
看一下這個專案用來測試、lint、建置和搜尋的指令。每一個,告訴我它成功時和失敗時大約印出多少文字,以及你執行它時,輸出會不會灌爆你的上下文視窗。為每一個提出比較安靜的跑法,例如只印失敗的部分或一份摘要。把最吵的三個排出來。不要改任何檔案,也不要執行任何會改變狀態的東西。在你自己的环境里试试
不用装 harness。在你自己的仓库里打开 agent,粘贴:
看看这个项目用来测试、lint、构建和搜索的命令。逐个告诉我,它成功时和失败时大约会输出多少文本,如果你运行它,这些输出会不会把你的上下文窗口塞满。为每个命令提出一种更安静的运行方式,比如只打印失败项或摘要。把最吵的三个排个序。不要修改任何文件,也不要运行任何会改变状态的东西。Go deeper
- Anthropic: Effective context engineering for AI agents
- VS Code docs: Context engineering guide
- Long chats get worse: what happens as the window fills
延伸閱讀
- Anthropic:Effective context engineering for AI agents
- VS Code 文件:Context engineering guide
- 長對話會變差:視窗填滿時會發生什麼
延伸阅读
- Anthropic:Effective context engineering for AI agents
- VS Code 文档:Context engineering guide
- 长对话会变差:窗口变满时会发生什么
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
The harness is built around loading little: a short always-on rulebook, a README that is only an index, and skills whose name and description are read first. You can see each one and how much it loads. After the setup above, type:
/harness-rookie-wondering I have read the chapter "Context is a budget". Teach me what this harness loads into the context every time, and what it leaves to be read when needed. Open the real files with me, count what is always on, and show me how a new session picks up a task.It teaches one small piece at a time, stops to ask if you are following, and opens the files with you.
在 harness 裡探索
harness 圍繞著「載入得少」來搭:一份簡短、一直開著的規則書,一個只當索引的 README,還有先讀名稱和描述的 skill。你可以看到每一樣,和它載入多少。照上面的說明準備好之後,輸入:
/harness-rookie-wondering 我讀完了「上下文是一份預算」這一章。請教我這個 harness 每次都把什麼載入上下文,又把什麼留到需要時才讀。帶我打開真實的檔案,數一數一直開著的有多少,並告訴我新的會話怎麼接手一個任務。它一次教一小塊,停下來確認你跟得上,然後和你一起打開檔案。
在 harness 里探索
这个 harness 是围绕“少装”搭起来的:一份简短的、始终开启的规则手册,一个只当索引用的 README,以及先读名字和描述的 skill。每一样你都能看到,也能看到它加载了多少。按上面的说明准备好之后,输入:
/harness-rookie-wondering 我读完了“上下文是一份预算”这一章。请教我这个 harness 每次都往上下文里加载了什么,又把什么留到需要时才读。带我打开真实的文件,数一数始终开启的有多少,再演示一个新会话是怎么接上一个任务的。它一次只教一小块,停下来确认你跟上了,再和你一起打开文件。