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

2.1

The agent loopagent 循環,和你能插手的地方agent 循环,以及你能插手的地方

This chapter assumes you have used an AI coding agent, such as Copilot in agent mode. Nothing else.

讀這一章,只需要用過 AI 寫程式 agent,例如 Copilot 的 agent 模式。其他都不用。

读这一章,只需要用过写代码的 AI agent,比如 Copilot 的 agent 模式。别的都不需要。

The Knowledge經驗分享经验分享

In one sentence

An agent is a loop: ask the model, run the tool it asks for, feed the result back. You steer it at a few points in each turn.

一句話

agent 是一個迴圈:問模型,執行它要的工具,把結果餵回去。你可以在每一輪的幾個點上掌舵。

一句话

agent 是一个循环:问模型,运行它要的工具,把结果喂回去。你在每一轮的几个点上掌舵。

For leaders

  • The decision: when an agent misbehaves, ask which of four places to fix (what it reads, which tools it has, a check before a call, a check after a call) before asking for a bigger model or a longer prompt.
  • The risk: the loop itself almost never breaks. In my reading, mistakes come from what is in the context or from what is allowed, and a rule written in a prompt is only a request. I have no counts of how often each happens.
  • The cost of doing it right: mostly setup time. The cheapest lever, what the agent reads, is a few lines of written rules.
  • Ask the team: "For the last bad result, was the fix a better context, a tighter tool list, or a check outside the model?"

給主管

  • 要做的決定:agent 出錯時,先問該修四個地方的哪一個(它讀什麼、有哪些工具、呼叫前的檢查、呼叫後的檢查),再去要更大的模型或更長的 prompt。
  • 風險:迴圈本身幾乎不會壞。依我的觀察,錯誤來自上下文裡有什麼,或者來自允許做什麼,而寫在 prompt 裡的規則只是請求。每一種發生得多頻繁,我沒有計數。
  • 做對的成本:主要是設定的時間。最便宜的槓桿,也就是 agent 讀什麼,只是幾行寫下來的規則。
  • 可以問團隊:「上一次的壞結果,修法是更好的上下文、更緊的工具清單,還是模型外面的一道檢查?」

写给管理者

  • 要拍板的事:agent 出了问题,先问该改四个地方里的哪一个(它读什么、有哪些工具、调用前的检查、调用后的检查),再去考虑换更大的模型或写更长的 prompt。
  • 风险:循环本身几乎不会坏。按我的观察,错误来自上下文里有什么,或者来自允许它做什么,而写在 prompt 里的规则只是请求。每一种有多常见,我没有统计。
  • 做对的代价:主要是配置的时间。最便宜的杠杆,也就是 agent 读什么,只是几行写下来的规则。
  • 可以问团队:“上一次的坏结果,修的办法是更好的上下文、更紧的工具清单,还是模型之外的一道检查?”

The problem

When an agent does something wrong, it feels like a mind has made a bad choice. That feeling sends people to the wrong fix. They write a longer prompt, or add "be careful" in capital letters, or switch to a bigger model. Sometimes that helps. Often it does not, because the problem was not in the thinking.

It helps to know what the machine is. It is small and plain. This chapter draws it, and marks the few places where you can reach into it. Every later chapter in this part, and most chapters after it, work on one of those places.

Three words first.

  • A model is the AI itself. It reads text and writes text. It does nothing else.
  • A tool is one thing the agent can do in your project: read a file, search, change a file, run a command.
  • A tool call is the model's request to use a tool, written as text: "read this file".

問題

agent 做錯事的時候,感覺像是一個有想法的東西做了壞決定。這種感覺會把人帶去錯的修法。他們寫更長的 prompt,或者用大寫字母加上「小心點」,或者換成更大的模型。有時有用。常常沒用,因為問題不在思考裡。

知道這台機器是什麼,會有幫助。它很小,很普通。這一章把它畫出來,並標出幾個你能伸手進去的地方。這一部分後面的每一章,以及再往後的大部分章節,都在處理其中一個地方。

先講三個詞。

  • 模型就是 AI 本身。它讀文字、寫文字。除此之外什麼都不做。
  • 工具是 agent 在你的專案裡能做的一件事:讀檔案、搜尋、改檔案、執行指令。
  • 工具呼叫是模型要求用一個工具,寫成文字:「讀這個檔案」。

问题

agent 做错事的时候,感觉像是一个头脑做了个糟糕的选择。这种感觉会把人引向错误的修法。他们把 prompt 写得更长,或者用大写字母加上一句“小心”,或者换成更大的模型。有时候有用。往往没用,因为问题不出在思考上。

了解这台机器是什么,会有帮助。它又小又朴素。这一章把它画出来,并标出你可以伸手进去的几个地方。这一部分后面的每一章,以及再往后的大多数章,都是在其中某一个地方下功夫。

先说三个词。

  • 模型就是 AI 本身。它读文本、写文本,别的什么都不做。
  • 工具是 agent 在你的项目里能做的一件事:读文件、搜索、改文件、运行命令。
  • 工具调用是模型要求使用某个工具的请求,写成文本:“读这个文件”。

How it works

The loop

The model cannot touch your files. It can only write text. So the way an agent works is this. The model writes a tool call. A program around the model, the part that is not AI, reads that text and runs the tool for real. The program then pastes the result back in front of the model as more text. The model reads it, and writes the next tool call. This repeats until the model writes an answer with no tool call in it. That is the signal that it thinks it is done.

I call one trip around this loop a turn. Some people use the word for the whole exchange from your message to the final answer, and in one trip a model can ask for several tools at once; here a turn is one round of ask, run, feed back.

you + the rules the model only writes text tool call the program runs the tool result, as text ...until the model says it is done what it reads which tools exist hook: before a call hook: after a call Four places where you can steer, in every turn
  1. You send a request. The platform adds the rules, and the model reads both.
  2. The model does not touch your files. It writes a request for a tool call: read this file, run this command.
  3. The program around the model runs the tool for real.
  4. The result goes back to the model as text, and the model decides the next step. This repeats until it says it is done.
  5. You cannot change the model's thinking. You can change what it reads, which tools exist, and what happens just before and just after each call.
An agent is a loop. The model asks, the program acts, the result goes back. You steer at four places in each turn.

Two facts follow, and both matter later.

  1. Every action is a tool call. The model has no other way to do anything. So there is a door that every action passes through. (For the built-in tools and for subagents I have seen every call pass the hook point; I have not tested MCP tools. Hooks has the details.) Whoever stands at that door can see, and stop, everything the agent does. A hook stands there. It is a small program of yours that the platform runs just before and just after each tool call, at fixed points; Hooks explains it.
  2. The model sees only text. It does not see your screen or your disk. It sees what is in its context: the rules, your words, the results of earlier tool calls, its own earlier answers. Its context is its whole world, and it has a size limit. The context is a budget is about that.

When does it stop?

The ordinary stop is the model answering without asking for a tool. There are others: you press stop, the platform reaches a limit, or something refuses the stop.

That last one is useful. A hook can run when the agent is about to stop, look at the to-do list of the task (the steps it still has open), and say "not yet, step 4 is open". The loop then goes on. This is how an agent is made to work through a long list without you. Use subagents sparingly covers that case and its stop rules. I have not checked what limit on turns Copilot sets by default, so I do not give one here.

Where you can step in

Look at the loop again. You cannot change how the model thinks. You can change four things around it.

  1. What it reads. The rules and instructions that are put in its context, and the files it is pointed at. This is the most used lever, and the cheapest. Instructions and skills is about it.
  2. Which tools exist. An agent that has no tool for changing files cannot change files. And a tool's description and its error messages are the only manual the model gets for it (Tool calling). A custom agent is the way to give a role a shorter list (Custom agents).
  3. A hook before a call. Code that sees the call before it runs and can refuse it. The platform guarantees that every call passes through this point, so a rule here is code that runs, not a request. A tool list or a command the agent runs to check itself is code too, but each covers a narrower range (chapter 4.1). Permission settings and sandboxes are another kind of lever: they do not read the call, they limit what any command can do. I have not tested them one by one on Copilot, so I leave them out of the four.
  4. A hook after a call. Code that sees the result, can keep a record, and can add a note for the model. It cannot undo what happened.

A short way to remember it: two of the four decide what the model knows, and two decide what it is allowed to do. Words only change what it knows.

Why most mistakes are not in the loop

Here is how I read an agent's mistakes. The loop itself almost never breaks. The mistakes come from what is in the context, or from what is allowed. This is the list I keep, from my own use. It is what I have seen, not a statistic:

What you see Usually why
It changed what it should not It reasons about how to get the job done, not whether it is allowed. A rule in a prompt is only a request.
It forgot something from earlier The context was compressed or the chat was too long.
It checked its own work and said it was fine The same model with the same reasoning has the same blind spots.
It keeps retrying a refused action It wants to finish the task.
It said "done" and it was not In my experience models lean towards reporting success.

Notice that few of these are about the model being unintelligent. The first and the last need a check outside the model. The second needs a better context (Long chats get worse). The third needs a second view (Use subagents sparingly). Only the fourth is close to a habit of the model itself. I do not have counts of how often each happens, so this is my reading of what I have seen, not a measurement.

See the loop yourself

You do not need to install anything. In your agent's chat, ask: "Read the README and tell me what the project does." Watch the chat. You will see a line such as "Read README.md" before the answer. That line is one tool call, shown to you. Then ask a question that needs three files, and count the lines. Each is one turn of the loop, and each result is now in the context.

原理

迴圈

模型碰不到你的檔案。它只能寫文字。所以 agent 是這樣運作的。模型寫出一個工具呼叫。模型周圍有個程式,不是 AI 的那一部分,讀這段文字,真的去執行工具。然後程式把結果當成更多文字,貼回模型面前。模型讀它,再寫出下一個工具呼叫。這樣重複,直到模型寫出一個不含工具呼叫的回答。這就是它認為自己做完了的訊號。

我把繞這個迴圈一圈叫做一輪。有些人用這個詞指從你的訊息到最後回答的整段來回,而且模型一圈可以同時要求好幾個工具;這裡的一輪,是問、執行、餵回去這一個回合。

你的話 + 規則 模型 只會寫文字 工具呼叫 外面的程式 真正執行工具 結果(文字) ……直到模型說做完了 它讀到什麼 有哪些工具 hook:呼叫之前 hook:呼叫之後 每一圈裡,你能插手的四個地方
  1. 你送出需求。平台加上規則,模型把兩者一起讀進去。
  2. 模型不會直接碰你的檔案。它寫出一個工具呼叫的請求:讀這個檔案、執行這個指令。
  3. 模型外面的程式真正執行這個工具。
  4. 結果以文字送回模型,模型決定下一步。這樣一圈圈重複,直到它說做完了。
  5. 你改變不了模型怎麼想。你能改的是它讀到什麼、有哪些工具、以及每次呼叫的前後發生什麼。
agent 就是一個循環:模型提出要求,程式動手,結果送回去。每一圈裡你有四個地方可以插手。

由此得到兩個事實,之後都很重要。

  1. 每個動作都是工具呼叫。模型沒有別的辦法做任何事。所以有一扇每個動作都經過的門。(內建工具和子 agent,我看過每次呼叫都經過 hook 的位置;MCP 工具我沒有測過。細節見 Hooks:每次呼叫都會經過的執行點。)站在這扇門口的人,看得到、也擋得住 agent 做的一切。hook 就站在那裡。它是你的一個小程式,平台在每次工具呼叫之前和之後的固定位置執行它;Hooks:每次呼叫都會經過的執行點 有說明。
  2. 模型只看得到文字。它看不到你的螢幕,也看不到你的磁碟。它看到的是它的上下文裡有的:規則、你說的話、之前工具呼叫的結果、它自己之前的回答。上下文就是它的整個世界,而且有大小上限。上下文是一份預算 講的就是這個。

什麼時候停?

一般的停止,是模型沒有要求工具就直接回答。還有別的:你按了停止、平台到了上限,或者有東西拒絕了這次停止。

最後這個很有用。agent 要停下來的時候,hook 可以執行,看任務的待辦清單(它還開著的步驟),然後說「還沒,第 4 步還沒做完」。迴圈就繼續。這就是讓 agent 不靠你、把一長串清單做完的辦法。慎用子 agent 講了這種情況和它的停止規則。Copilot 預設把輪數限制在多少,我沒有查過,所以這裡不給數字。

你能在哪裡插手

再看一次迴圈。你改變不了模型怎麼思考。你可以改變它周圍的四樣東西。

  1. 它讀什麼。放進它上下文的規則和指令,以及指給它看的檔案。這是用得最多、也最便宜的槓桿。指令與 skill:只載入需要的 講的就是這個。
  2. 有哪些工具。沒有改檔案工具的 agent,就改不了檔案。而且工具的描述和錯誤訊息,是模型拿到的唯一手冊(Tool calling,和給 agent 設計工具)。custom agent 是給一個角色較短工具清單的辦法(Custom agent:固定模型和工具的角色)。
  3. 呼叫前的 hook。在呼叫執行前看到它、並且能拒絕它的程式碼。平台保證每次呼叫都會經過這一點,所以這裡的規則是會執行的程式碼,不是請求。工具清單,或 agent 用來檢查自己的指令,也都是程式碼,但各自涵蓋的範圍窄一些(提示裡的規則只是請求)。權限設定和沙箱是另一種槓桿:它們不讀呼叫,而是限制任何指令能做什麼。我沒有在 Copilot 上一項項測過,所以把它們放在這四個之外。
  4. 呼叫後的 hook。看到結果、可以留下紀錄、並且能為模型加一則備註的程式碼。它沒辦法撤銷已經發生的事。

一個簡單的記法:四個裡有兩個決定模型知道什麼,兩個決定它被允許做什麼。文字只改變它知道什麼。

為什麼大部分錯誤不在迴圈裡

我是這樣看 agent 的錯誤的。迴圈本身幾乎不會壞。錯誤來自上下文裡有什麼,或者來自允許做什麼。這是我根據自己的使用留下的清單。這是我看到的,不是統計:

你看到的 通常為什麼
它改了不該改的 它推理的是怎麼把事做完,不是被不被允許。prompt 裡的規則只是請求。
它忘了先前的某件事 上下文被壓縮了,或者對話太長。
它檢查了自己的工作,說沒問題 同一個模型、同樣的推理,有同樣的盲點。
它一直重試被拒絕的動作 它想把任務完成。
它說「做完了」,其實沒有 依我的經驗,模型傾向回報成功。

注意,這些裡面很少是模型不夠聰明。第一個和最後一個,需要模型外面的一道檢查。第二個需要更好的上下文(長對話會變差)。第三個需要第二個視角(慎用子 agent)。只有第四個接近模型自己的習慣。每一種發生得多頻繁,我沒有計數,所以這是我對所見的看法,不是量出來的。

自己看看迴圈

你不用安裝任何東西。在 agent 的對話裡問:「讀 README,告訴我這個專案做什麼。」看著對話。回答之前,你會看到一行像「Read README.md」的字。那一行就是一次工具呼叫,秀給你看的。然後問一個需要三個檔案的問題,數一數有幾行。每一行是迴圈的一輪,每個結果現在都在上下文裡了。

原理

循环

模型碰不到你的文件。它只能写文本。所以 agent 是这样工作的。模型写出一个工具调用。模型外面的一个程序,也就是不是 AI 的那部分,读到这段文本,真的去运行这个工具。然后程序把结果作为更多文本,贴到模型面前。模型读完,写出下一个工具调用。这样重复,直到模型写出一个不带工具调用的回答。这就是它认为自己做完了的信号。

我把绕这个循环走一圈,叫作一轮。有些人用这个词指从你发消息到最终回答的整个来回,而且一圈里模型可以同时要求用好几个工具;这里的一轮,指的是一次“问、运行、喂回去”。

你的话 + 规则 模型 只会写文本 工具调用 外面的程序 真正运行工具 结果(文本) ……直到模型说做完了 它读什么 有哪些工具 hook:调用之前 hook:调用之后 每一轮里,你能插手的四个地方
  1. 你发出需求。平台加上规则,模型把两者一起读进去。
  2. 模型不会直接碰你的文件。它写出一个工具调用的请求:读这个文件、运行这条命令。
  3. 模型外面的程序真正运行这个工具。
  4. 结果以文本送回模型,模型决定下一步。这样一轮轮重复,直到它说做完了。
  5. 你改变不了模型怎么想。你能改的是它读什么、有哪些工具,以及每次调用的前后发生什么。
agent 就是一个循环:模型提出要求,程序动手,结果送回去。每一轮里你有四个地方可以插手。

由此得出两个事实,后面都会用到。

  1. 每个动作都是一次工具调用。模型没有别的办法做任何事。所以有一扇门,每个动作都要从这里经过。(对内置工具和子 agent,我见过每次调用都经过 hook 点;MCP 工具我没有测过。细节见 Hook:每次调用都会经过的执行点。)站在这扇门口的人,能看到并拦住 agent 做的一切。hook 就站在那里。它是你的一个小程序,平台在每次工具调用的前后,在固定的点上运行它;Hook:每次调用都会经过的执行点会讲解它。
  2. 模型只看得到文本。它看不到你的屏幕,也看不到你的磁盘。它看到的是上下文里的内容:规则、你说的话、之前工具调用的结果、它自己之前的回答。上下文就是它的整个世界,而且有大小上限。上下文是一份预算讲的就是这个。

它什么时候停?

通常的停止,是模型不再要求工具,直接作答。还有别的情况:你按了停止,平台到了某个上限,或者有什么东西拒绝了这次停止。

最后这一种很有用。agent 要停的时候,可以运行一个 hook,去看任务的待办清单(它还没做完的步骤),然后说“还没完,第 4 步还开着”。循环就接着走下去。这就是让 agent 不用你守着、把一长串清单做完的办法。慎用子 agent讲了这种情况和它的停止规则。Copilot 默认给轮数设了什么上限,我没有查过,所以这里不给数字。

你能在哪里插手

再看一遍这个循环。你改变不了模型怎么思考。你能改的,是它周围的四样东西。

  1. 它读什么。放进它上下文的规则和指令,以及指给它的文件。这是用得最多的杠杆,也是最便宜的。指令与 skill:只加载需要的讲的是这个。
  2. 有哪些工具。没有改文件工具的 agent,就改不了文件。而且工具的描述和报错信息,是模型拿到的关于它的唯一说明书(Tool calling,以及怎么给 agent 设计工具)。自定义 agent 是给一个角色一份更短工具清单的办法(自定义 agent:固定模型和工具的角色)。
  3. 调用前的 hook。在调用运行之前就看到它、并且能拒绝它的代码。平台保证每次调用都经过这个点,所以这里的规则是会运行的代码,不是请求。一份工具清单,或者一条让 agent 自查用的命令,也是代码,但各自覆盖的范围更窄(提示里的规则只是请求)。权限设置和沙箱是另一类杠杆:它们不读调用本身,而是限制任何命令能做什么。我没有在 Copilot 上逐个测过它们,所以没把它们放进这四样里。
  4. 调用后的 hook。看到结果、能留下记录、能为模型加一条备注的代码。它撤销不了已经发生的事。

一个简便的记法:四样里,两样决定模型知道什么,两样决定它被允许做什么。光靠话,只能改变它知道什么。

为什么大多数错误不出在循环里

我是这样看 agent 的错误的。循环本身几乎不会坏。错误来自上下文里有什么,或者来自允许它做什么。下面这张清单是我自己用出来的,是我见过的情况,不是统计数据:

你看到的 通常的原因
它改了不该改的 它推理的是怎么把活干完,不是被不被允许。prompt 里的规则只是请求。
它忘了前面的某件事 上下文被压缩了,或者对话太长了。
它检查了自己的工作,说没问题 同一个模型,同样的推理,有同样的盲区。
它对一个被拒绝的操作不停重试 它想把任务做完。
它说“做完了”,其实没有 按我的经验,模型倾向于汇报成功。

注意,这几条里很少是因为模型不够聪明。第一条和最后一条,需要模型之外的一道检查。第二条,需要更好的上下文(长对话会变差)。第三条,需要另一个视角(慎用子 agent)。只有第四条,接近模型本身的习惯。每一种有多常见,我没有数据,所以这是我对所见的解读,不是测量。

自己看看这个循环

你不用安装任何东西。在 agent 的对话里问:“读一下 README,告诉我这个项目是做什么的。”看着对话。回答之前,你会看到一行类似“Read README.md”的字。这一行就是一次工具调用,展示给你看。然后再问一个需要三个文件的问题,数数有几行。每一行是循环的一轮,每个结果现在都在上下文里。

Costs and traps

Every turn rereads the context. Each time around, the model reads the whole context again, now with one more result in it. A task of thirty turns is thirty reads of a growing text. This is why what the agent reads matters more than how clever the model is, and why the model's price per token is the wrong number to look at alone. One correction: the reread is not always charged at full price. Providers cache a repeated beginning, and on Anthropic's API a cache read costs about a tenth of the base input price (Models and cost has the sums). I have not checked how Copilot bills it. What the cache does not fix is the second cost: the model still has to attend to all of that text.

Tool results are the biggest and the least watched part. You wrote the prompt, so you know it. The file the agent read, or the log a command printed, you may never have seen. It is still in the context, and the model may act on it.

An agent is not always the answer. A loop that decides its own next step costs more and is harder to predict than a fixed series of model calls, or a single call. Anthropic's advice is to find the simplest solution possible and add complexity only when needed (Building effective agents). If the steps of a job never change, a script that calls the model at fixed points may serve you better than an agent.

Do not read the loop as a plan. The model does not plan the whole path and then follow it. It decides one turn at a time from what it sees. When it goes down a wrong path, it is usually because the result of an earlier turn led it there.

代價和陷阱

每一輪都要重讀上下文。每繞一圈,模型都要把整個上下文再讀一遍,現在多了一個結果。三十輪的任務,就是三十次讀一份越來越長的文字。這就是為什麼 agent 讀什麼,比模型有多聰明更重要,也是為什麼只看每個 token 的單價,是看錯了數字。要更正一點:重讀不一定按全價收費。供應商會把重複的開頭快取起來,在 Anthropic 的 API 上,讀快取的價格大約是基本輸入價的十分之一(選模型:算的是每次成功的成本 有算式)。Copilot 怎麼計費,我沒有查過。快取解決不了的是第二種成本:模型還是得去注意那麼多文字。

工具結果是最大、也最少人看的部分。prompt 是你寫的,所以你知道。agent 讀的檔案,或指令印出的紀錄,你可能從來沒看過。它還是在上下文裡,而模型可能照著做。

agent 不一定是答案。自己決定下一步的迴圈,比一串固定的模型呼叫,或單次呼叫,花得更多,也更難預測。Anthropic 的建議是找到最簡單的解法,只在需要時才加複雜度(Building effective agents)。如果一件工作的步驟從來不變,一個在固定位置呼叫模型的腳本,可能比 agent 更適合你。

別把迴圈當成計劃來讀。模型不是先規劃好整條路再照著走。它每一輪都根據看到的東西決定。它走上錯的路,通常是因為前面某一輪的結果把它帶去的。

代价和坑

每一轮都要重读上下文。每绕一圈,模型都把整个上下文再读一遍,里面又多了一个结果。一个三十轮的任务,就是对一段越来越长的文本读三十遍。这就是为什么 agent 读什么,比模型多聪明更要紧;也是为什么只看模型每个 token 的价格是不对的。要更正一点:重读并不总是按全价收费。提供方会缓存重复的开头,在 Anthropic 的 API 上,读缓存的价格约是基础输入价的十分之一(算式见选模型:看每次成功的成本)。Copilot 怎么计这笔账,我没有查过。缓存解决不了的是第二笔开销:模型仍然得去关注那一大段文本。

工具结果是最大的、也是最少被盯着的部分。prompt 是你写的,所以你知道。agent 读的文件,或者一条命令打印的日志,你可能从没见过。它们仍然在上下文里,模型可能会照着它们去行动。

agent 不总是答案。自己决定下一步的循环,比一串固定的模型调用,或者单次调用,更贵,也更难预测。Anthropic 的建议是找最简单的解决办法,只在需要时才增加复杂度(Building effective agents)。如果一件事的步骤从不改变,一个在固定位置调用模型的脚本,可能比 agent 更适合你。

别把循环当成计划。模型不会先规划好整条路径再照着走。它每一轮,根据看到的东西来决定。它走上错路,通常是因为前面某一轮的结果把它带过去的。

When to delete it Industry practice

The reasoning about the loop does not retire while agents work by calling tools. The practical parts may. Today I steer at four places because that is what the platform gives me. If the platform starts to offer a native way to enforce a rule, a budget or a stop condition, I will use that instead of my own hooks. I would then drop the hooks, not the loop picture.

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

只要 agent 還是靠呼叫工具運作,關於迴圈的推理就不會退役。實務的部分可能會。今天我在四個地方掌舵,是因為平台給我的就是這些。如果平台開始提供原生的辦法,來執行規則、預算或停止條件,我會用那個,不用自己的 hook。那時我會丟掉 hook,不會丟掉迴圈的圖像。

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

只要 agent 还是靠调用工具来工作,关于循环的道理就不会退役。实际操作的那部分也许会。我今天在四个地方掌舵,是因为平台就给了我这些。如果平台开始提供原生的办法来强制执行规则、预算或停止条件,我会用它,而不是我自己的 hook。那时我会丢掉 hook,而不是循环这幅图。

Try it on your own setup

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

Read this project's instruction files and settings. For each rule or limit you find, tell me which of four places it works at: what the agent reads, which tools exist, a check before a tool call, or a check after a tool call. Mark every rule that is only a written request with nothing outside the model enforcing it. List the five rules where a break would matter most. Do not change any files.

在你自己的環境試試

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

讀這個專案的指令檔和設定。你找到的每條規則或限制,都告訴我它作用在四個地方的哪一個:agent 讀什麼、有哪些工具、工具呼叫前的檢查,或工具呼叫後的檢查。把每一條只是寫下來的請求、模型外面沒有任何東西執行它的規則標出來。列出五條壞掉時影響最大的規則。不要改任何檔案。

在你自己的环境里试试

不用装 harness。在你自己的仓库里打开 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

The loop is the same everywhere, but in the harness you can watch each turn from the side: what is injected into the context, and which call a hook saw and what it answered. After the setup above, type:

/harness-rookie-wondering I have read the chapter "The agent loop, and where you can step in". Teach me how this harness stands at the four places in the loop: what it puts into the context, which tools it allows, and what its hooks do before and after a call. Open the real files with me and draw how one turn passes through them.

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

在 harness 裡探索

迴圈到處都一樣,但在 harness 裡,你可以從旁邊看每一輪:什麼被注入了上下文,hook 看到了哪次呼叫、答了什麼。照上面的說明準備好之後,輸入:

/harness-rookie-wondering 我讀完了「agent 循環,和你能插手的地方」這一章。請教我這個 harness 怎麼站在迴圈的四個位置:它把什麼放進上下文、允許哪些工具,以及它的 hook 在呼叫之前和之後做什麼。帶我打開真實的檔案,並畫出一輪怎麼經過它們。

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

在 harness 里探索

循环在哪里都一样,但在 harness 里,你可以从旁边看每一轮:往上下文里注入了什么,hook 看到了哪次调用、回答了什么。按上面的说明准备好之后,输入:

/harness-rookie-wondering 我读完了“agent 循环,以及你能插手的地方”这一章。请教我这个 harness 是怎么站在循环的四个地方的:它往上下文里放了什么、允许哪些工具,以及它的 hook 在调用前后做什么。带我打开真实的文件,并画出一轮是怎么穿过它们的。

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