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

3.2

Long chats get worse長對話會變差长对话会变差

This chapter assumes you have used an AI coding agent in a long chat, and read the context budget (the window has a size limit, and everything in it competes for room).

讀這一章,請先在長對話裡用過寫程式的 AI agent,並讀過上下文是一份預算(視窗有大小上限,裡面所有東西都在搶位子)。

读这一章,需要你用过 AI 编程 agent 开过很长的对话,并且读过上下文是一份预算(窗口有大小上限,里面的东西互相争地方)。

The Knowledge經驗分享经验分享

In one sentence

Long chats get worse. Do not trust the model to remember: write what matters into files, clear old tool results, or start a fresh chat.

一句話

長對話會變差。別指望模型記得:把要緊的寫進檔案、清掉舊的工具結果,或開一個新的對話。

一句话

对话越长,表现越差。别指望模型自己记住:把重要的写进文件,清掉旧的工具结果,或者开一个新对话。

For leaders

  • The decision: require that anything that matters lives in a file, not only in the chat, and agree when to start a fresh chat.
  • The risk: long chats get worse, and a platform's summary of a chat drops details such as your exact words and the reason for a choice. I have not measured how fast quality falls in my own setup, so I give no rate.
  • The cost of doing it right: a short notes file per piece of work, updated after each finished step. If you cannot write the handover in half a page, the work is not yet clear enough.
  • Ask the team: "If this chat disappeared now, what would we lose?"

給主管

  • 要做的決定:要求凡是要緊的東西都放在檔案裡,不能只在對話裡;並且約好什麼時候開新的對話。
  • 風險:長對話會變差,而平台對對話做的摘要會丟細節,例如你原話的措辭和做某個選擇的理由。在我自己的設定裡,品質掉得多快我沒量過,所以不給比率。
  • 做對的成本:每件工作一個簡短的筆記檔,每做完一步就更新。如果交接寫不滿半頁,說明這件工作還不夠清楚。
  • 可以問團隊:「如果這個對話現在消失了,我們會丟掉什麼?」

写给管理者

  • 要拍板的事:要求重要的东西都放在文件里,而不是只留在对话中,并且约定好什么时候开新对话。
  • 风险:对话变长会变差,而平台对对话做的摘要会丢细节,比如你的原话和做某个选择的原因。在我自己的配置里,质量下降得多快,我没量过,所以不给速率。
  • 做对的代价:每项工作一个简短的笔记文件,每做完一步就更新一次。如果交接内容半页纸写不下,说明这件事还没想清楚。
  • 可以问团队:“如果这个对话现在没了,我们会丢什么?”

The problem

You have worked with an agent for two hours. It started sharp. Now it asks about something you settled at the start, repeats a mistake it fixed, or edits the wrong file. Nothing broke. The chat just got long.

Three words first. The first two are defined in Context is a budget; here they are in brief.

  • The context window is the text the model can see at one time. It has a size limit.
  • A tool result is what comes back when the agent uses a tool. These are usually the biggest items in the window.
  • Compaction is what a platform does when the window is nearly full. It asks a model to summarise the chat so far, and continues from the summary.

I have not measured how fast quality falls in my own setup, and I will not give you a number. The shape of the problem is clear enough to act on, and the people who build the models describe it the same way (see what the industry says).

Why a long chat gets worse

It is easiest to see as a picture: the same chat at three moments.

The same chat, three moments (the box is the context window) start later much later bright: needed now. grey: old tool results and turns. new chat
  1. At the start the window holds the rules, the request and little else.
  2. Every file read and command output stays. What the task needs now is the same size, but it sits in more and more old text.
  3. Near the limit the window is full, and the platform has to summarise or drop things. The useful part is a small share.
  4. A new chat starts with only what you carry over: the rules and a short note from a file.
A long chat does not make the useful part bigger. It makes the rest bigger.

Four things happen together.

  1. The useful part does not grow; the rest does. What the task needs right now (the request, the rule you gave, the file being edited) stays about the same size. Everything else piles up around it: files read an hour ago, a test output that was long and is now irrelevant, three wrong attempts.
  2. The model reads all of it, every time. It has no filing cabinet. On every turn the whole window is read again. The more old text there is, the more the one line that matters competes with it. (A cache can make the rereading cheaper in money, Models and cost, but not less crowded.) The name for this is context rot: as the window fills, the model's recall of what is in it gets less reliable.
  3. Early things are far from where they are needed. A rule from the first message is now thousands of lines behind. It is still there, but it is far from the step where it applies. This one is my own reading of what I see; I have no source for it.
  4. Compaction loses detail. A summary keeps what its writer judged important. Your exact words, the reason for a choice and the one odd constraint are the first things a summary drops, and you cannot know which it dropped.

None of this is a bug in one product. It comes from how the window works, so it applies to every agent.

問題

你和 agent 已經工作了兩小時。一開始它很敏銳。現在它問起你一開始就定好的事,重複一個已經修掉的錯誤,或是改錯檔案。什麼都沒壞。只是對話變長了。

先說三個詞。前兩個在上下文是一份預算裡定義過,這裡簡單講一下。

  • 上下文視窗是模型一次能看到的文字。它有大小上限。
  • 工具結果是 agent 使用工具後回來的東西。它們通常是視窗裡最大的項目。
  • 壓縮(compaction)是視窗快滿時平台做的事。它請模型把到目前為止的對話摘要一遍,然後從摘要繼續。

在我自己的設定裡,品質掉得多快我沒量過,所以我不會給你一個數字。問題的形狀已經清楚到可以行動,做模型的人也用同樣的方式描述它(見業界怎麼說)。

為什麼長對話會變差

用一張圖最容易看懂:同一個對話的三個時刻。

同一個對話,三個時間點(方框是上下文視窗) 剛開始 之後 很久之後 亮:任務現在需要的。灰:舊的工具結果和舊的對話。 新對話
  1. 剛開始,視窗裡只有規則、需求,沒什麼別的。
  2. 讀過的每個檔案、跑過的每個指令輸出都留著。任務現在需要的東西一樣大,卻埋在越來越多的舊文字裡。
  3. 接近上限時視窗滿了,平台只好摘要或丟掉一些。有用的部分只佔一小塊。
  4. 新對話只帶你轉交過去的東西:規則,加上從檔案讀來的一小段筆記。
對話變長,不會讓有用的部分變大,只會讓其他部分變大。

有四件事同時發生。

  1. 有用的部分不增長,其餘的一直長。任務現在需要的東西(需求、你給的規則、正在編輯的檔案)大小差不多不變。其他的都堆在它周圍:一小時前讀的檔案、一份很長、現在已經無關的測試輸出、三次錯誤的嘗試。
  2. 模型每次都把全部讀一遍。它沒有檔案櫃。每一輪,整個視窗都會被重讀。舊的文字越多,那一行要緊的就越要和它們競爭。(快取可以讓重讀在花費上便宜一點,見選模型:算的是每次成功的成本,但不會讓視窗不那麼擠。)這個現象的名字叫上下文腐化:視窗越滿,模型對裡面內容的回想越不可靠。
  3. 早期的東西離用到它的地方很遠。第一則訊息裡的規則,現在落在幾千行之後。它還在,但離適用它的那一步很遠。這一點是我自己對所見的解讀,我沒有來源。
  4. 壓縮會丟細節。摘要留下的是寫摘要的那方認為重要的東西。你原話的措辭、做某個選擇的理由、那一條古怪的限制,是摘要最先丟掉的,而你無從知道它丟了哪些。

這些都不是某一個產品的 bug。它來自視窗的運作方式,所以適用於每一個 agent。

问题

你和 agent 已经合作了两个小时。一开始它很利落。现在它却来问你开头就定下的事,重复犯一个已经修掉的错,或者改错了文件。什么都没坏。只是对话变长了。

先说三个词。前两个在上下文是一份预算里定义过,这里简单提一下。

  • 上下文窗口是模型一次能看到的文本。它有大小上限。
  • 工具结果是 agent 用工具后返回的内容。它们通常是窗口里最大的几项。
  • 压缩是平台在窗口快满时做的事:让模型把到目前为止的对话总结一下,然后从摘要接着往下走。

在我自己的配置里,质量下降得多快,我没量过,所以不会给你一个数字。问题的形状已经足够清楚,可以据此行动,做模型的人也是这么描述的(见业界怎么说)。

长对话为什么会变差

看图最容易懂:同一个对话的三个时刻。

同一个对话,三个时刻(方框是上下文窗口) 刚开始 之后 很久之后 亮:任务现在需要的。灰:旧的工具结果和旧的对话。 新对话
  1. 刚开始,窗口里只有规则、需求,没什么别的。
  2. 读过的每个文件、跑过的每条命令输出都留着。任务现在需要的东西一样大,却埋在越来越多的旧文本里。
  3. 接近上限时窗口满了,平台只好做摘要或丢掉一些。有用的部分只占一小块。
  4. 新对话只带你转交过去的东西:规则,加上从文件读来的一小段笔记。
对话变长,不会让有用的部分变大,只会让其余部分变大。

四件事同时发生。

  1. 有用的部分不会变大,其余的会。 任务眼下需要的(需求、你给的规则、正在改的文件)大小基本不变。其余的都堆在它周围:一小时前读的文件,一份又长又已经无关的测试输出,三次错误的尝试。
  2. 模型每次都要把所有内容读一遍。 它没有文件柜。每一轮,整个窗口都要重新读一遍。旧文本越多,那一行要紧的内容要争的对手就越多。(缓存能让重复读取省钱,见选模型:看每次成功的成本,但不会让窗口变得不那么挤。)这种现象叫上下文腐化:窗口越满,模型对里面内容的回忆就越不可靠。
  3. 早先的内容离用到它的地方很远。 第一条消息里的规则,现在已经在几千行之前。它还在,但离用到它的那一步很远。这一条是我自己对所见现象的理解,没有出处。
  4. 压缩会丢细节。 摘要留下的,是写摘要的一方认为重要的。你的原话、做某个选择的原因、那条古怪的约束,都是摘要最先丢掉的,而且你没法知道它丢了哪些。

这些都不是某个产品的 bug。它来自窗口本身的工作方式,所以对每个 agent 都适用。

How it works

The answer is not "a bigger window" or "a smarter summary". It is to stop depending on the chat as the place where important things live. There are four moves. The first is a habit to keep all the time. The other three you use as the chat grows, from lightest to heaviest.

cheapest most drastic write it in a file clear old tool results a big read to a subagent start a fresh chat nothing lost less clutter only the answer a clean window Each move keeps what matters outside the chat.
Four moves against a long chat, from cheapest to most drastic. The first one makes the others safe.

1. Write down what matters, in a file

Ask: if this chat disappeared now, what would I lose? Whatever the answer is should not live only in the chat.

  • What was decided and why.
  • What is done and what is next.
  • Where things are (which files, which commands).

These go in a file the agent can read again: a short notes file in the task's folder, or its to-do list (both are explained in Manage tasks, not chats). A file does not get compacted, and a new chat can read it in a second. When it is read again, its text sits near the end of the window, close to where it is needed.

In my setup, every piece of work has a folder with a handover file: how far it is, the next step, and what is decided. A new chat reads that file first and continues. Because of this, losing a chat costs me a few minutes, not the work.

How to write the note. Short. State what is true now, not the story of how you got there. Put the next step at the top. One page is better than ten, because the new chat has to read it, and it takes room too.

Make the agent do it. Ask it to update the notes after each finished step, not at the end. The end is when you are tired and the chat is longest. You can say it in one sentence: "After each finished step, update the notes file: what is decided, what is done, the next step."

Anthropic describes the same habit for long-running agents: a progress file and the git history, which the agent reads first when a new session starts (Effective harnesses for long-running agents). Here is the template for the handover file in my setup:

A handover file template: status, what is happening now, the next step, the files to read, what is decided, and open questions.
# TASK: {name}

> The handover file. A new session reads this file first, then what it points to. Keep it within one screen. Update it every time before you stop.

- **Kind**: {kind}
- **Tier**: {tier}
- **Created**: {created}
- **Updated**: <yyyy-mm-dd>
- **Status**: <in progress / waiting for the user to approve the plan / waiting for the user to accept / done>
- **Doing now**: <one sentence>
- **Next step**: <the one thing to do next>

## Files to read

<The files a new session should read, in order, with links. Write "none" if there is nothing>

## Decided

<What the user has decided, one per line, with the date. Write "none" if there is nothing>

## Open questions

<Five at most. Delete each one once it is solved. Write "none" if there is nothing>

2. Clear old tool results

Most of the window is usually old tool output. The agent read a file to find one function; the whole file is still there. A test run printed pages of output; only the one failing line mattered.

Dropping these is the lightest form of compaction, and the safest: the decisions and the conclusions stay, and only the raw material goes. If the agent needs the file again, it can read it again. On Anthropic's API this is a feature called context editing: a strategy named clear_tool_uses removes the oldest tool results once the input passes a size you set. It is in beta, and clearing invalidates the cached beginning, so it has a price (Context editing). Whether your tool does something like it, and when, is something to check, not assume. I have not tested what Copilot does today (see Costs and traps).

You can get the same effect by hand. When a long read is done, ask the agent to write down only what it found, and then continue in a new chat that has the finding but not the reading.

3. Send a big read to a subagent

If the next step is to read a lot, do not read it in your main chat. A subagent is a second agent that starts with an empty window and sends back only a short report. The reading stays in its window, and your main chat gets the conclusion. This is the "parallel reading" case in Use subagents sparingly, with the same limits: it only works when a short report is enough, and it costs more in total.

Some platforms make this a setting. In VS Code, a skill can declare context: fork in its header, and it then runs in a subagent of its own instead of in your chat (Agent skills; the docs mark it experimental). A skill that reads a lot and reports a little is a good fit. I have not tried it, so I do not know how it counts usage or how hooks see it.

4. Start a fresh chat

Sometimes the right move is to stop. Signs it is time:

  • The agent repeats something you already settled.
  • It undoes a fix from an hour ago.
  • You catch yourself re-explaining.
  • The task changed (a new task is a new chat).

A fresh chat starts with an empty window and the rules. Give it only what it needs to continue: point it at the notes file. In my experience it often does better than the old chat did at the end, because it is reading a clean page. I have not measured this.

Compare the cost. Starting fresh feels like losing progress. But the progress is in the files and in the code; the chat held only the memory of it, and that memory was already worn.

When to start fresh in a planned way. For long work that I split into steps, I do not wait for the chat to degrade. Learning a large code base, for example, goes in batches, and I begin a new chat every two or three batches. The state is in the task, not the chat, so nothing is lost at the boundary.

What to hand over

When you change chats, the new one needs a small, fixed set:

  1. The request, in a few lines.
  2. What is decided, and what is not.
  3. What is done, and the next step.
  4. Where the files are.

If you cannot write these four in half a page, that is the real finding: the work is not yet clear enough to hand over to anyone, including yourself tomorrow.

Test your own setup

A cheap test, with no extra setup, that one person can do in an hour. Pick a rule you gave early in a long chat, one with two or three parts. After an hour of work, ask the agent to state it without looking back. Then start a new chat, give it only your notes file, and ask the same. Score each answer by how many of the parts it got right. If the second is more accurate, your chat is doing worse than your files.

Also check where your instructions live. A rule given only in the conversation is lost when the chat is compacted; a rule in a file the platform reads again is not. Claude Code's documentation says exactly this about its project instruction file. If you want a rule to survive, put it in a file.

原理

答案不是「更大的視窗」,也不是「更聰明的摘要」。是不再把對話當成要緊東西存放的地方。有四個動作。第一個是要一直保持的習慣。另外三個,隨著對話變長再用,由輕到重。

最便宜 最徹底 寫進 檔案 清掉舊的 工具結果 大量閱讀 交給子 agent 開一個 新對話 什麼都不丟 少些雜物 只帶回答案 乾淨的視窗 每一招都把要緊的東西放在對話外面。
對付長對話的四招,從最便宜到最徹底。第一招讓後面幾招都安全。

1. 把要緊的寫進檔案

問自己:如果這個對話現在消失了,我會丟掉什麼?不管答案是什麼,它都不該只存在於對話裡。

  • 決定了什麼,和為什麼。
  • 做完了什麼、接下來是什麼。
  • 東西在哪裡(哪些檔案、哪些指令)。

這些放在 agent 能再讀的檔案裡:任務資料夾裡一份簡短的筆記檔,或是它的待辦清單(兩者都在管任務,不管對話裡說明)。檔案不會被壓縮,新的對話一秒鐘就能讀完。再讀的時候,它的文字會落在視窗的末端,靠近用到它的地方。

在我的設定裡,每件工作都有一個資料夾,裡面有一份交接檔:做到哪了、下一步、已經決定的事。新的對話先讀這個檔案,然後接著做。因為這樣,丟掉一個對話只花我幾分鐘,不會丟掉工作。

怎麼寫筆記。要短。寫現在是什麼樣,不寫是怎麼走到這一步的。把下一步放在最上面。一頁比十頁好,因為新的對話要讀它,它也佔位子。

讓 agent 去做。請它每做完一步就更新筆記,不要等到最後。最後是你累了、對話最長的時候。一句話就能說完:「每做完一步,就更新筆記檔:已決定的事、已完成的事、下一步。」

Anthropic 為長時間運行的 agent 描述了同樣的習慣:一份進度檔加上 git 歷史,新的會話一開始,agent 先讀這些(Effective harnesses for long-running agents)。下面是我的設定裡交接檔的範本:

交接檔的範本:狀態、現在在做什麼、下一步、要讀的檔案、已決定的事,和未解決的問題。
# TASK: {name}

> The handover file. A new session reads this file first, then what it points to. Keep it within one screen. Update it every time before you stop.

- **Kind**: {kind}
- **Tier**: {tier}
- **Created**: {created}
- **Updated**: <yyyy-mm-dd>
- **Status**: <in progress / waiting for the user to approve the plan / waiting for the user to accept / done>
- **Doing now**: <one sentence>
- **Next step**: <the one thing to do next>

## Files to read

<The files a new session should read, in order, with links. Write "none" if there is nothing>

## Decided

<What the user has decided, one per line, with the date. Write "none" if there is nothing>

## Open questions

<Five at most. Delete each one once it is solved. Write "none" if there is nothing>

2. 清掉舊的工具結果

視窗的大部分通常是舊的工具輸出。agent 為了找一個函式讀了一個檔案;整個檔案還留在那裡。一次測試跑出幾頁輸出;要緊的只有失敗的那一行。

丟掉這些,是最輕的壓縮,也是最安全的:決定和結論留著,只有原始材料走掉。如果 agent 又需要那個檔案,它可以再讀一次。在 Anthropic 的 API 上,這是一個叫 context editing 的功能:一個叫 clear_tool_uses 的策略,會在輸入超過你設定的大小之後,移除最舊的工具結果。它還在 beta,而清除會讓被快取的開頭失效,所以有代價(Context editing)。你的工具有沒有做類似的事、什麼時候做,要去查,不要假設。我沒有測過 Copilot 今天怎麼做(見代價和陷阱)。

你也可以手動得到同樣的效果。一次很長的閱讀做完後,請 agent 只寫下它發現了什麼,然後在新的對話裡繼續,新的對話有這個發現,但沒有那些閱讀。

3. 把大量閱讀交給子 agent

如果下一步要讀很多東西,別在主對話裡讀。子 agent 是第二個 agent,它從空的視窗開始,只交回一份簡短的報告。閱讀留在它的視窗裡,你的主對話拿到結論。這就是慎用子 agent裡的「並行閱讀」,限制也一樣:只有一份簡短的報告就夠的時候才行得通,而且總共花得更多。

有些平台把這件事做成了一個設定。在 VS Code 裡,skill 可以在檔頭寫 context: fork,它就會在自己的子 agent 裡執行,不在你的對話裡(Agent skills;文件標明是實驗性的)。讀得多、回報得少的 skill 很適合這樣做。我沒試過,所以不知道它的用量怎麼算,hook 又怎麼看到它。

4. 開一個新的對話

有時候對的做法是停下來。該停的跡象:

  • agent 重複你已經定好的事。
  • 它推翻一小時前的一個修正。
  • 你發現自己在重新解釋。
  • 任務變了(新的任務就是新的對話)。

新的對話從空的視窗和規則開始。只給它繼續所需要的:指向筆記檔就行。依我的經驗,它常常比舊的對話在結尾時做得更好,因為它讀的是一張乾淨的紙。我沒有量過這一點。

比較一下成本。重新開始感覺像丟掉進度。但進度在檔案裡和程式碼裡;對話只是記著這些進度,而那份記憶已經磨損了。

有計劃地重新開始。對於我拆成幾步的長工作,我不等對話變差。例如學習一個大的程式碼庫,是分批做的,每兩三批我就開一個新的對話。狀態在任務裡,不在對話裡,所以在交界處什麼都不會丟。

交接什麼

換對話時,新的對話需要一小組固定的東西:

  1. 需求,幾行就好。
  2. 決定了什麼,沒決定什麼。
  3. 做完了什麼,和下一步。
  4. 檔案在哪裡。

如果這四樣你寫不滿半頁,這才是真正的發現:這件工作還不夠清楚,沒法交給任何人,包括明天的你自己。

測一測你自己的設定

一個便宜的測試,不用額外設定,一個人一小時就能做。挑一條你在長對話早期給的規則,有兩三個部分的。工作一小時後,請 agent 不回頭看,把它說出來。然後開一個新的對話,只給它你的筆記檔,問同樣的問題。每個答案按它答對了幾個部分打分。如果第二個更準,你的對話就比你的檔案做得差。

也檢查一下你的指令放在哪裡。只在對話裡給的規則,對話被壓縮時就丟了;放在平台會再讀的檔案裡的規則,不會。Claude Code 的文件對它的專案指令檔說的就是這個。要讓一條規則留下來,就把它放進檔案。

原理

答案不是“更大的窗口”,也不是“更聪明的摘要”。而是别再把对话当成放重要东西的地方。一共有四招。第一招是要一直保持的习惯。另外三招在对话变长时用,从最轻到最重。

最便宜 最彻底 写进 文件 清掉旧的 工具结果 大块阅读 交给子 agent 开一个 新对话 什么都不丢 少些杂物 只带回答案 干净的窗口 每一招都把要紧的东西放在对话外面。
对付长对话的四招,从最便宜到最彻底。第一招让后面几招都安全。

1. 把要紧的写进文件

问自己:如果这个对话现在没了,我会丢什么?不管答案是什么,那些东西都不该只存在于对话里。

  • 定了什么,以及为什么。
  • 做完了什么,下一步是什么。
  • 东西在哪(哪些文件,哪些命令)。

这些写进 agent 能重新读到的文件:任务文件夹里的一份简短笔记,或者它的待办清单(两者都在管任务,不管对话里讲)。文件不会被压缩,新对话一秒钟就能读完。重新读到时,它的文本在窗口的靠后位置,离用到它的地方很近。

在我的配置里,每项工作都有一个文件夹,里面有一份交接文件:做到哪了、下一步、定了什么。新对话先读这份文件,然后接着干。因此丢一个对话,我损失的是几分钟,不是整份工作。

笔记怎么写。 要短。写现在是什么情况,不要讲怎么走到这一步的故事。下一步放在最上面。一页比十页好,因为新对话得读它,而它也占地方。

让 agent 来做。 让它每做完一步就更新笔记,不要等到最后。到最后你已经累了,对话也最长。一句话就能说清:“每做完一步,更新笔记文件:定了什么、做完了什么、下一步。”

Anthropic 为长时间运行的 agent 描述过同样的习惯:一份进度文件加上 git 历史,新会话开始时 agent 先读它们(Effective harnesses for long-running agents)。下面是我配置里交接文件的模板:

交接文件模板:状态、现在在做什么、下一步、要读的文件、已经定下的事,以及待解决的问题。
# TASK: {name}

> The handover file. A new session reads this file first, then what it points to. Keep it within one screen. Update it every time before you stop.

- **Kind**: {kind}
- **Tier**: {tier}
- **Created**: {created}
- **Updated**: <yyyy-mm-dd>
- **Status**: <in progress / waiting for the user to approve the plan / waiting for the user to accept / done>
- **Doing now**: <one sentence>
- **Next step**: <the one thing to do next>

## Files to read

<The files a new session should read, in order, with links. Write "none" if there is nothing>

## Decided

<What the user has decided, one per line, with the date. Write "none" if there is nothing>

## Open questions

<Five at most. Delete each one once it is solved. Write "none" if there is nothing>

2. 清掉旧的工具结果

窗口里的大部分,通常是旧的工具输出。agent 为了找一个函数读了整个文件,整个文件还留在那里。一次测试跑出了好几页输出,真正有用的只有失败的那一行。

扔掉这些,是最轻的一种压缩,也最安全:决定和结论留下,只去掉原始材料。agent 如果还要用那个文件,可以再读一遍。在 Anthropic 的 API 上,这是一个叫上下文编辑(context editing)的功能:名为 clear_tool_uses 的策略,会在输入超过你设定的大小之后,把最旧的工具结果清掉。它还在 beta 阶段,而且清除会让缓存的开头失效,所以是有代价的(Context editing)。你的工具有没有类似的功能、什么时候触发,需要去查,不能想当然。Copilot 今天怎么做,我没测过(见代价和坑)。

你也可以手动做到同样的效果。一段长时间的阅读结束后,让 agent 只写下它的发现,然后在一个新对话里接着干,新对话里有发现,没有阅读的过程。

3. 把大块的阅读交给子 agent

如果下一步要读很多东西,别在主对话里读。子 agent 是第二个 agent,它从空窗口开始,只把一份简短的报告送回来。阅读的内容留在它的窗口里,你的主对话只拿到结论。这就是慎用子 agent里的“并行阅读”那种情况,限制也一样:只有一份简短的报告就够用时才行,而且总共花得更多。

有的平台把它做成了设置。在 VS Code 里,skill 可以在头部声明 context: fork,这样它就在自己的子 agent 里运行,而不是在你的对话里(Agent skills;文档标明它是实验性的)。读得多、报告得少的 skill 很适合。我没试过,所以不知道它怎么计用量,也不知道 hook 怎么看待它。

4. 开一个新对话

有时候正确的做法是停下来。该停的信号:

  • agent 重复你已经定下的事。
  • 它撤销了一小时前的修复。
  • 你发现自己在重新解释。
  • 任务变了(新任务就开新对话)。

新对话从空窗口加规则开始。只给它继续干活所需的东西:让它去读笔记文件。按我的经验,它常常比旧对话到最后时做得更好,因为它读到的是一张干净的纸。这一点我没量过。

比一比成本。重新开始让人觉得进度丢了。但进度在文件里、在代码里;对话里只留着对进度的记忆,而这份记忆早就磨损了。

有计划地重新开始。 对我拆成多步的长任务,我不会等对话变差。比如学一个大代码库,是分批做的,每两三批我就开一个新对话。状态在任务里,不在对话里,所以交界处什么也不会丢。

交接什么

换对话时,新对话需要一小套固定的内容:

  1. 需求,几行就行。
  2. 哪些定了,哪些没定。
  3. 做完了什么,下一步是什么。
  4. 文件在哪。

这四样如果半页纸写不下,那才是真正的发现:这件事还没想清楚,没法交给任何人,包括明天的你自己。

检验你自己的配置

一个便宜的测试,不需要额外配置,一个人一小时就能做完。挑一条你在长对话早期给的规则,要有两三个部分。工作一小时后,让 agent 不回头看,直接复述这条规则。然后开一个新对话,只给它你的笔记文件,再问同样的问题。按每个回答答对了几个部分来打分。如果第二个更准确,说明你的对话还不如你的文件靠谱。

还要查一下你的指令放在哪。只在对话里给的规则,对话被压缩时就丢了;放在平台会重新读取的文件里的规则就不会。Claude Code 的文档对它的项目指令文件,说的就是这一点。想让一条规则留下来,就把它放进文件。

Costs and traps

Do not hide a bad habit. If chats get long because the task is too big, the fix is a smaller task, not better note-taking.

A note can be wrong. If the agent writes "tests pass" in the notes and they did not, the next chat starts from a false fact. Notes are the agent's report; check the ones that matter. Memory is about this problem at length.

Notes cost room too. A notes file that grows without trimming becomes the thing that fills the window. Keep it short and cut what is done. And a notes file can hold things you would not want shared, so think before it goes into a shared repository.

A fresh chat loses the feel. The old chat knew how you phrase things and what you meant by "the usual way". If that matters, write it down; it is likely a rule.

What I did not test. I have not measured how my platform compacts: what it keeps, what it drops, and whether it clears old tool results by itself. Treat compaction as lossy until you have checked it for your tool.

代價和陷阱

不要掩蓋壞習慣。如果對話變長是因為任務太大,補救是更小的任務,不是更好的筆記。

筆記可能是錯的。如果 agent 在筆記裡寫「測試通過」而實際沒有,下一個對話就從一個錯誤的事實開始。筆記是 agent 的報告;要緊的,要檢查。agent 該記住什麼用較長的篇幅談這個問題。

筆記也佔位子。一個不修剪、一直長大的筆記檔,會變成填滿視窗的東西。保持簡短,把做完的剪掉。而且筆記檔裡可能有你不想分享的東西,放進共用的倉庫之前要想一想。

新的對話會失去手感。舊的對話知道你怎麼說話,也知道你說「照平常的做法」是什麼意思。如果這很重要,就寫下來;這很可能就是一條規則。

我沒測的。我沒有量過我的平台怎麼壓縮:留下什麼、丟掉什麼,以及它會不會自己清掉舊的工具結果。在你為自己的工具查清楚之前,把壓縮當成有損的。

代价和坑

别掩盖坏习惯。 如果对话变长是因为任务太大,办法是把任务拆小,而不是更好地记笔记。

笔记可能是错的。 如果 agent 在笔记里写“测试通过”,而实际没有,下一个对话就是从一个假的事实出发。笔记是 agent 的报告;重要的要核对。agent 该记住什么详细讲的就是这个问题。

笔记也占地方。 一份不断变长又不修剪的笔记文件,会变成填满窗口的那个东西。保持简短,把已经做完的删掉。另外,笔记文件里可能有你不想共享的内容,放进共享仓库之前要想一想。

新对话会丢掉“默契”。 旧对话知道你怎么措辞,也知道你说的“老办法”是什么意思。如果这很重要,就写下来;它多半就是一条规则。

我没测的。 我没量过我用的平台是怎么压缩的:留下什么,丢掉什么,会不会自己清理旧的工具结果。在你为自己的工具查过之前,把压缩当作有损的。

What the industry says Industry practice

Anthropic's engineering team describes this problem and the same fixes. They use the term context rot: as the window fills, "the model's ability to accurately recall information from that context decreases". They name compaction ("taking a conversation nearing the context window limit, summarizing its contents"), call tool result clearing "one of the safest lightest touch forms of compaction", and describe note-taking, where "the agent regularly writes notes persisted to memory outside of the context window". (Effective context engineering for AI agents)

Claude Code's documentation adds a practical detail: the project instruction file at the root is read again from disk after compaction, while an instruction given only in the conversation is gone. (How Claude remembers your project)

For long tasks that need many sessions, Anthropic describes a progress file plus git history that each new session reads first. (Effective harnesses for long-running agents) On the API, tool result clearing is available as context editing, in beta. (Context editing)

They agree with each other and with this chapter. None gives a rule for when a chat is too long, and I do not either; judge by the signs above.

業界怎麼說 業界做法

Anthropic 的工程團隊描述了這個問題和同樣的補救。他們用上下文腐化這個詞:視窗越滿,「模型準確回想那段上下文中資訊的能力就越低」。他們談到壓縮,也就是「把一段接近上下文視窗上限的對話,把內容摘要」,把清除工具結果稱為「最安全、最輕的壓縮方式之一」,也描述了做筆記,即「agent 定期把筆記寫到上下文視窗之外、持久保存的記憶裡」。(Effective context engineering for AI agents)

Claude Code 的文件補充了一個實用的細節:根目錄的專案指令檔在壓縮後會從磁碟重新讀取,而只在對話裡給的指令就沒了。(How Claude remembers your project)

對於需要很多個會話的長任務,Anthropic 描述了一份進度檔加上 git 歷史,每個新的會話先讀它們。(Effective harnesses for long-running agents)在 API 上,清除工具結果以 context editing 的形式提供,還在 beta。(Context editing)

它們彼此一致,也和這一章一致。沒有一家給出對話多長才算太長的規則,我也不給;請看上面的跡象來判斷。

业界怎么说 业界做法

Anthropic 的工程团队描述过这个问题和同样的办法。他们用的术语是上下文腐化(context rot):窗口越满,“模型准确回忆那段上下文里信息的能力就越下降”。他们提出了压缩(“把一段接近上下文窗口上限的对话,总结其内容”),称清除工具结果是“最安全、最轻量的压缩方式之一”,还描述了记笔记,即“agent 定期写下笔记,保存在上下文窗口之外的内存里”。(Effective context engineering for AI agents)

Claude Code 的文档补充了一个实用细节:根目录的项目指令文件在压缩之后会从磁盘重新读取,而只在对话里给的指令就没了。(How Claude remembers your project)

对需要很多个会话才能完成的长任务,Anthropic 描述了一份进度文件加 git 历史,每个新会话先读它们。(Effective harnesses for long-running agents)在 API 上,清除工具结果以上下文编辑的形式提供,处于 beta 阶段。(Context editing)

它们彼此一致,也和这一章一致。没有一家给出对话多长算太长的规则,我也没有;凭上面的信号来判断。

When to delete it Industry practice

Part of this is the platform's job and will be done better by it: clearing old tool results, compacting, and carrying key facts across. When your tool does those well, and you have checked what it keeps, stop doing them by hand.

The core stays: if the work matters, its state belongs in a file you can read, not in a chat. A bigger window and better compaction make chats last longer. They do not make a summary as good as the original, and they do not give a new chat your last two hours. I would look again if a platform showed that its compacted chats lose nothing I care about, measured on my own tasks.

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

其中一部分是平台的工作,平台會做得更好:清掉舊的工具結果、壓縮,以及把關鍵事實帶過去。你的工具把這些做得好,而且你查過它留下了什麼,就不用再手動做。

核心不變:工作如果要緊,它的狀態就屬於一個你能讀的檔案,不屬於對話。更大的視窗和更好的壓縮,讓對話撐得更久。它們不會讓摘要和原文一樣好,也不會讓新的對話擁有你最後那兩小時。如果有平台能證明,它壓縮過的對話不會丟掉我在乎的東西,而且是在我自己的任務上量的,我會再看一次。

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

这里有一部分是平台的活,平台会做得更好:清掉旧的工具结果、压缩、把关键事实带到下一轮。等你的工具把这些做好了,而且你查过它留下什么,就不用再手动做了。

核心不变:如果这项工作重要,它的状态就该放在你能读到的文件里,而不是对话里。窗口更大、压缩更好,能让对话撑得更久。但它们不会让摘要和原文一样好,也不会让新对话拥有你最近两个小时的经历。如果有平台能证明,它压缩后的对话没有丢掉我在意的任何东西,并且是在我自己的任务上量出来的,我会重新考虑。

Try it on your own setup

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

Imagine this chat is lost and a new agent must carry on. From what you can see in this repository (recent commits, open changes, any notes or task files, the instruction files), write the handover that agent would need: the request in a few lines, what is decided, what is done and the next step, and where the files are. Then list what you could not tell from the repository, as the gaps a person must fill. Print it only. Do not change any files.

在你自己的環境試試

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

想像這個對話丟了,必須由一個新的 agent 接手。根據你在這個倉庫裡看得到的(最近的 commit、未提交的改動、任何筆記或任務檔、指令檔),寫出那個 agent 需要的交接:幾行的需求、已決定的事、已完成的事和下一步,以及檔案在哪裡。然後列出你從倉庫裡看不出來的部分,作為需要人來補的缺口。只印出來,不要改任何檔案。

在你自己的环境里试试

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

假设这个对话丢了,要由一个新的 agent 接着干。根据你在这个仓库里能看到的东西(最近的提交、未提交的改动、任何笔记或任务文件、指令文件),写出那个 agent 需要的交接内容:几行说清需求、哪些已经定了、做完了什么和下一步、文件在哪。然后列出你从仓库里看不出来的东西,作为必须由人来补上的空缺。只打印出来,不要修改任何文件。

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

Copilot Harness keeps the state of every task in files, so a chat can end at any time. The harness can show you where those files are and how a new chat picks up from them. After the setup above, type:

/harness-rookie-wondering I have read the chapter "Long chats get worse". Teach me how this harness keeps the state of a task in files: what the handover file holds, how a new session reads it to carry on, and what it does about a window that fills up (topics 1 and 9). Open the real files with me.

It teaches one small piece at a time and opens the files with you.

在 harness 裡探索

Copilot Harness 把每個任務的狀態都存在檔案裡,所以對話隨時可以結束。harness 可以讓你看到這些檔案在哪,以及新的對話怎麼從它們接手。照上面的說明準備好之後,輸入:

/harness-rookie-wondering 我讀完了「長對話會變差」這一章。請教我這個 harness 怎麼把任務的狀態存在檔案裡:交接檔裡有什麼、新的會話怎麼讀它來接著做,以及視窗快滿時它怎麼處理(主題 1 和 9)。帶我打開真實的檔案。

它一次教一小塊,並和你一起打開檔案。

在 harness 里探索

Copilot Harness 把每个任务的状态都放在文件里,所以对话随时可以结束。harness 能给你看这些文件在哪,以及新对话是怎么从它们接上的。按上面的说明准备好之后,输入:

/harness-rookie-wondering 我读完了“长对话会变差”这一章。请教我这个 harness 是怎么把任务的状态放在文件里的:交接文件里有什么,新会话怎么读它来接着干,以及窗口快满时它是怎么处理的(主题 1 和 9)。带我打开真实的文件。

它一次只教一小块,并和你一起打开文件。