逐字稿 · CC BY 4.0

MIT Missing Semester 2026,第 7 講:代理式編碼 — 逐字稿

本頁標題、字幕、摘要與閱讀筆記為已儲存的 AI 譯文,可能有誤。時間戳與來源一致,可展開查看原始字幕,並對照原影片核對。

MIT Missing Semester 示範代理式編碼工具,說明 LLM 工具呼叫迴圈、情境與隱私取捨、測試工作流程及常見陷阱。

Missing Semester · 發佈於 · 1 小時 01 分 · 英文 · 授權:CC BY 4.0 · 來源:在 YouTube 觀看

逐字稿來源:創作者上傳至 YouTube 的字幕。AI 標點由 Vidleaf 加入;原始措辭與時間戳錨點均保留。原始逐字稿見下方。

AI 產生的摘要

本講延續「開發環境與工具」一講中的 AI 輔助開發主題,從自動補全與行內聊天,進一步談到被形容為更精密、更新形態的編碼代理。編碼代理基本上就是一個對話式 AI 模型,能取用讀寫檔案、執行 shell 命令等工具;示範中使用 Claude Code,把一個簡單的 Python 腳本 dl.py 改寫成命令列程式,採用 argparse,並以 mypy 檢查型別註解。講者討論安全性與成本,建議逐項核准會改變電腦狀態的工具呼叫、把所有內容都納入 Git 追蹤,並提到這次示範花費約 19 美分。代理大致有兩個部分:底層的語言模型,可視為對「以提示字串為條件的完成字串機率分布」進行建模;以及包裝它的代理框架,負責處理情境視窗、回合標記,以及會呼叫工具的输出。框架很可能是確定性的,而模型則是機率性的;模型不像人一樣聰明,有時會輸出不太正確的內容,不過實務上這些東西通常運作得還算可以。本講也涵蓋提示、透過清除、倒轉與壓縮來管理情境、llms.txt 與 agents.md、子代理,以及一些注意事項:LLM 是機率性的,輸出應檢查正確性與安全漏洞;AI 可能失控或陷入除錯迴圈,因此必須加以留意。

由逐字稿產生。請對照原始影片檢查連結的段落。

含時間戳的章節摘要

使用 Claude Code 進行代理式程式設計

從 AI 輔助程式設計到程式設計代理

  • [0:04] 本講座延續「開發環境與工具」講座中的 AI 驅動開發主題,從自動補全與行內聊天,進一步談到程式設計代理。
  • [0:21] 程式設計代理被呈現為比大約五年前存在的形式更精細且更新穎的形態。
  • [0:36] 計畫是先示範代理如何運作,接著說明它們在底層如何運作,然後介紹基礎與進階主題。

示範任務:將 dl.py 改為命令列程式

  • [1:21] 起點是一個簡單的 Python 腳本,它會從課程網站下載一個頁面並擷取其中所有連結。
  • [1:33] 目標是將這個拼湊而成的腳本改為更完善的命令列程式,例如加入引數解析,讓要擷取的頁面成為命令列引數。
  • [1:48] 可以要求程式設計代理端到端完成整個任務。

工具選擇與程式設計代理的定義

  • [1:54] 示範使用 Claude Code,這是一種特定的程式設計代理;其他命令列代理列於講座筆記的推薦軟體章節。
  • [2:08] 程式設計代理也內建於 VS Code 或 Cursor 等 IDE 中,提供大致相同的功能。
  • [2:32] 程式設計代理基本上就是一個對話式 AI 模型,能存取有助於撰寫程式的工具,例如讀寫檔案與執行 shell 命令。
  • [2:50] 有些模型經過特別調校,在撰寫軟體方面特別有效。

自然語言提示與工具使用

  • [3:05] 提示要求代理使用 argparse 將 dl.py 改為完善的命令列程式,加上適當的型別註解,並用 mypy 檢查。
  • [3:49] 代理會像 ChatGPT 一樣產生文字輸出,但也能呼叫工具;在這裡它決定先讀取 dl.py。
  • [4:04] 對這個簡單任務,它一次就更新了 dl.py,加入函式以及含命令列引數解析的 main 函式。
  • [4:32] 依照指示,它接著透過 shell 呼叫 mypy,並以腳本路徑作為引數來檢查型別註解。

權限與自主運作

  • [4:41] 代理未經詢問權限就編輯了檔案,因為講者就是這樣設定的。
  • [4:50] 講者覺得每次小小檔案變更都要跳出權限提示很煩,希望代理能更自主地運作,並大致上用 Git 追蹤所有變更。

程式設計代理:安全性、成本與互動

為了安全而核准工具呼叫

  • [5:00] 講者讓程式設計代理進行大量變更,之後再一次性審查全部。
  • [5:07] 對於 bash 這類工具,講者希望檢查代理執行的每個指令,並確認其安全。
  • [5:17] 完全自主的代理會引發安全疑慮,因為它們可能呼叫你不想要的指令,例如刪掉你在意的檔案。
  • [5:28] 講者個人推薦一種模式,要求逐一核准會改變電腦狀態的工具呼叫。

代理迴圈與多輪互動

  • [5:36] 工具呼叫獲得核准後,測試框架執行了該工具、把結果回傳給模型,模型便繼續執行腳本並驗證它能否運作。
  • [8:50] 在單次示範中,單一指令就讓代理呼叫工具、檢視結果、再呼叫更多工具,最後回報完成。
  • [9:04] 代理也支援多輪互動:它會保留先前對話的脈絡,並能針對後續訊息採取行動,例如移除型別註解。

控制與監控 AI 支出

  • [6:14] 支出可以設限,不過具體的設定選項取決於所用工具。
  • [6:22] 選擇模型是其中一種控制方式:能解決較難任務的高容量模型費用較高,而較小、較快的模型則較便宜。
  • [6:35] 有些服務可讓你設定支出上限,例如每月最多 $10。
  • [6:47] 講者個人不設支出上限,但會監控用量,例如透過 Claude Code 的 slash cost 指令,示範中花費約 19 美分。
  • [7:02] 對學生而言,每月 $20、含一定用量的訂閱方案,或許是不錯的起步方式。

成本取捨與本機模型

  • [7:17] 在許多公司裡,工程師喜歡這類工具,而生產力的提升讓工具成本相對於工程師的時間顯得相當低,因此有些人每月花上好幾百美元。
  • [7:45] 程式設計代理要花錢,而且粗略來說需要網路連線。
  • [7:50] 把模型放在自己筆電上的本機程式設計代理是可行的,但你必須使用容量低得多的模型,這些模型或許能處理簡單任務,但面對較難的任務就表現不佳。
  • [8:11] 實際上,講者認為現今使用託管模型並付費是合理的。
  • [8:21] 定價通常採用訂閱制,因為推論成本取決於使用量,而各家供應商的定價模式各不相同。

迭代式程式設計代理工作流程與防護措施

迭代式精進與中斷代理

  • [9:27] 你評估結果,並告訴代理以某種方式調整,反覆使用這些工具。
  • [9:37] 如果程式設計代理出錯、混淆,或無意義地呼叫工具,你可以中斷它,例如在所使用工具中按 Control-C,然後給它後續訊息。
  • [9:53] 例如,如果它因為 mypy 安裝在異常位置而呼叫了錯誤的工具,你可以中斷並告訴它 mypy 在哪裡以及如何呼叫。

保護檔案免於非預期的變更

  • [10:17] 在所使用的那個特定工具中,顯然無法硬性限制不得碰觸檔案的某個部分。
  • [10:27] 實務做法是將所有內容都納入 Git 追蹤,並還原任何你不想要的變更。
  • [10:34] 你也可以用自然語言下指令,例如不要變更某段程式碼,而代理通常能相當不錯地遵守。

與代理進行測試驅動開發

  • [10:54] 講者有使用代理進行測試驅動開發的經驗,後續在講座中還會有更多內容。
  • [11:01] 一個好方法是自己撰寫測試,並告訴代理實作功能使其通過測試,或先讓代理協助撰寫測試,再實作功能。
  • [11:15] 這些工具在回饋循環中運作得非常好,例如給代理一個錯誤和重現該錯誤的指令,讓它持續運作直到錯誤被修正。

AI 程式設計代理的運作方式:語言模型與代理框架

說明範圍與目的

  • [11:38] 講者計畫說明底層 AI 模型與代理框架的運作方式,抽象層級足以有效使用這些工具,而非重新實作它們。
  • [12:18] 目標是傳達這些工具擅長什麼、其限制,以及為何代理框架要實作某些功能。

語言模型作為條件機率分布

  • [12:33] AI 程式設計代理大致可拆解為兩個主要部分:底層語言模型,以及包裝該模型的代理框架。
  • [13:04] 大型語言模型可視為對完成字串 y 以提示字串 x 為條件的機率分布進行建模,寫作 pi theta of y given x。
  • [13:55] 使用這些模型意味著從這個參數化分布中,根據給定的輸入取樣輸出字串 y hat,而其他所有功能都建立在這項核心基本操作之上。
  • [14:28] 實務上,這些模型具有有限的上下文視窗,這表示提示字串與完成字串的長度都有上限。

透過輪次標記與完整歷史進行多輪對話

  • [15:19] 對話式聊天之所以可行,是因為它使用輪次標記,並將整段對話歷史作為輸入餵給模型,模型再從分布中取樣輸出下一段內容。
  • [16:07] 像把一替換成二這樣的後續問題本身並不具意義,必須有先前的上下文才能正確回答。
  • [16:28] 對話式聊天應用程式會將整段對話(包含特殊的輪次標記編碼)作為提示,再次從語言模型取樣來處理這個問題。

在聊天之上加入工具呼叫

  • [17:25] 除了輪次標記之外,代理框架還能對 LLM 的輸出加上特殊解讀,讓模型不僅能輸出助理文字,也能輸出像是「呼叫工具」這樣的內容。

透過代理框架訓練 LLM 呼叫工具

工具呼叫輸出格式

  • [17:52] 計畫是訓練一個語言模型,讓它能輸出在你的機器上執行動作的請求。
  • [18:08] 一個工具呼叫的範例是請求執行指令,例如對 dl.py 執行 mypy。

代理框架執行迴圈

  • [18:20] 包裝語言模型的程式碼,類似於將完整訊息歷史串接成後續輸入的對話式聊天應用程式,扮演代理框架的角色,也稱為鷹架。
  • [18:43] 框架會解讀 LLM 的輸出(y-hat),在機器上執行它,並將結果作為後續輸入餵回語言模型。

將工具輸出餵回模型

  • [18:57] mypy 工具可能會回傳文字輸出,例如「checking dl.py」,或是在第一行回報期望 int 的錯誤。
  • [19:11] 所有這些工具輸出都會作為另一項輸入插入 LLM。

代理框架與 LLM 的互動

代理框架的核心功能

  • [19:39] 代理框架實作了在迴圈中呼叫 LLM 的程式碼,接收其輸出,並在使用者的機器上分派與執行任何被請求的工具呼叫。
  • [19:52] 它會將工具產生的文字餵回語言模型,讓模型能產生另一項輸出。
  • [20:06] 大致來說,這個框架會接收使用者輸入,在迴圈中呼叫代理,執行被要求的工具,並把結果回饋回去,直到模型輸出給使用者的文字輸出為止。
  • [20:32] 當模型表示已完成時,控制權便交還給使用者。

工具實作與程式編寫能力

  • [20:32] 工具實作可能包含用於執行腳本的 bash,以及檔案讀取、寫入與編輯工具。
  • [20:59] 這些工具合在一起,讓底層的 AI 模型能在機器上做出變更,並以某種聰明的方式開發程式碼。
  • [21:05] 代理可以執行測試、查看結果,並在決定後續動作時將這些結果納入考量。

語言模型與框架作為主要基礎元件

  • [21:22] 核心是一個語言模型,它對字串或符記建立機率分布模型。
  • [21:28] 代理框架會在迴圈中呼叫 LLM,進行反覆取樣,分派工具請求、執行它們,並把結果回饋回去以推動進展。
  • [21:41] 當使用者輸入查詢時,系統通常會在底層多次從語言模型取樣的同時執行一堆工具。

框架與模型之間的互通性

  • [22:00] 代理框架與底層語言模型之間存在常用的介面,儘管它們並未完全標準化。
  • [22:14] 現在已有相當程度的互通性;例如,像 OpenCode 這樣的開源代理框架可以搭配不同的底層語言模型運作,例如 Anthropic 與 OpenAI 的模型。

確定性的框架與機率性的模型

  • [22:49] 框架程式碼本身大概是確定性的,而語言模型是機率性的,兩者透過取樣產生關聯。
  • [22:59] 當框架呼叫 LLM 時,它會取樣出一個輸出字串並據此行動,例如執行模型輸出的指令,然後繼續進行。
  • [23:13] 模型是以聰明的方式訓練出來的,但並不像人一樣具有智慧,因此有時會輸出不太正確的東西,例如不存在的指令或不正確的程式碼。
  • [23:30] 實務上這些東西往往運作得還算不錯,而隨著你使用它們,你會建立起直覺,了解它們擅長什麼,以及它們還不夠好到能做什麼。

Claude Code 中的代理框架工具呼叫迴圈

設定:Claude Code 搭配 Opus 4.5

  • [24:01] 講者啟動了一個代理框架,具體來說是由 Anthropic 開發的 Claude Code,底層執行 Anthropic 的 Opus 4.5 大型語言模型。
  • [24:15] 最初的初始使用者查詢作為第一個查詢給出,顯示在範例的右上角。

讀取檔案工具呼叫與上下文增長

  • [24:20] 模型輸出中間文字,接著以參數 dl.py 呼叫讀取檔案工具。
  • [24:30] 代理框架將模型輸出解讀為工具請求,在檔案系統上執行檔案讀取,並將檔案內容作為額外輸入回饋給語言模型。
  • [24:49] 這會不斷將更多內容附加到對話歷史 X,因此在此範例中,最後傳送給 LLM 的內容就是該檔案的內容。

透過差異與型別檢查器寫入檔案

  • [25:14] 經過另一次推論後,模型輸出更新並請求檔案寫入工具,提供一份差異,由代理框架套用到檔案系統上的檔案。
  • [25:51] 檔案更新操作傳回成功值,該值會在另一次推論前回饋給模型。
  • [25:59] 接著模型呼叫型別檢查器 mypy;框架執行它,並將輸出(成功,未發現問題)回饋給模型以進行另一次推論。

每個使用者提示的多個推論步驟

  • [26:26] 再經過一次工具呼叫後,模型表示其回合已結束並給出最終文字輸出,將控制權交還給使用者。
  • [26:29] 單一使用者提示會發生一連串的 LLM 推論步驟與工具呼叫。

程式設計代理的隱私與使用案例

雲端推論與資料隱私

  • [26:52] 能更妥善執行複雜任務的高容量語言模型需要大量硬體進行推論,因此大多在雲端執行。
  • [27:05] 除非您與供應商有特別協議,否則使用這些模型意味著將您的資料傳送給他們,而他們可能會用於訓練。
  • [27:11] 若隱私是考量,請詳細檢閱條款與條件,或尋找適合您的設定。

Claude 與代理框架的執行位置

  • [27:30] Claude 不一定同時是 LLM 與代理框架;有幾種不同的設定。
  • [27:36] 在此情況下,代理框架的某個元件可視為與模型一同在雲端執行,而模型必定在雲端執行。
  • [27:42] 開源代理框架可以完全在本機執行,但所有 LLM 推論步驟仍會將資料傳送到雲端。

開放式程式設計代理指示

  • [27:55] 程式設計代理可以依照非常具體的指示實作新功能,或者您可以從開放式開始,再逐步迭代。
  • [28:08] 在課程網站(也就是 missing semester 網站)的 Git 儲存庫中,可以告訴 Claude 類似「讓它看起來有復古感」這樣的指令,大約需要一兩分鐘。

用代理修正程式碼問題

  • [28:22] 程式碼代理另一個很棒的用途,就是請它們修正程式碼中的問題。

用程式碼代理搭配測試來修正錯誤

把編譯器、linter 與測試失敗交給代理處理

  • [28:27] 如果程式碼無法編譯,或是 linter 或其他錯誤檢查工具提出警告,你可以請程式碼代理修正這個問題。
  • [28:42] 遇到錯誤時,很好的第一步是先為它寫一個測試,這個測試同時也能作為回歸測試,防止未來再次損壞。
  • [29:01] 一旦有了失敗的測試,你就可以告訴程式碼代理這個測試失敗了,並請它找出問題並讓測試通過。

示範設定:手寫的失敗測試

  • [29:26] 在一個示範程式碼庫中,講者注意到一個問題,並為它手寫了一個單元測試,執行時會失敗。
  • [29:41] 或者,也可以讓程式碼代理先嘗試寫測試,例如描述程式碼在某種用法下會如何出錯。

代理重現錯誤並閱讀程式碼

  • [29:51] 對測試滿意之後,使用者啟動程式碼代理,告訴它有錯誤、提供重現錯誤的指令,並請它修正這個錯誤。
  • [30:21] 代理會先透過 bash 執行測試來重現錯誤並確認失敗,但單憑輸出資訊不足以修正問題。
  • [30:40] 接著代理會閱讀並理解程式碼,使用檔案讀取與正規表達式搜尋工具,講者指出這些代理相當擅長瀏覽程式碼庫。

速度限制與自主驗證

  • [31:13] 程式碼代理並不是最快的工具,讓它們既有效又更快速是目前積極研究的領域;這場示範已經執行了約一分鐘。
  • [31:35] 模型說明它的診斷與修正方式,接著自行要求重新執行測試,使用者核准後,測試便通過了。
  • [31:50] 代理也會執行其餘測試,確認沒有破壞程式碼庫中的其他部分,然後摘要說明它做了什麼。

依儲存庫風格提交變更

  • [32:19] 使用者可以請代理提交這些變更,它會建立一個帶有實用提交訊息的 git commit。
  • [32:29] 代理經過訓練,不會憑空編造提交訊息,而是遵循儲存庫的風格,檢查 git status 以了解哪些內容已暫存、哪些已變更。

從 git diff 與 log 產生 AI 提交訊息

檢視 diff 與近期提交風格

  • [32:42] 此工具會檢查 git diff,以確認要提交的是它所做的變更,而非無關的過去變更。
  • [32:47] 接著它會檢閱 git log 與近期的提交訊息,以便盡量符合既有風格。

撰寫提交訊息

  • [32:53] 它會寫出一則看起來相當合理的提交訊息,例如註明此變更修正了某個特定的 GitHub 議題。

提示編碼代理與 Claude Code 的使用

提示與模型對程式碼品質的影響

  • [33:22] 提供給編碼代理的提示與底層 LLM 絕對會影響程式碼品質、冗長程度、任務時長及類似結果。
  • [33:33] 目前妥善提示 LLM 與其說是科學,不如說是藝術,而底層模型也持續在進步。
  • [33:40] 較舊的模型或許仍能完成這類任務,但需要非常詳盡的指示,而目前的模型只需告訴它有錯誤要修正即可。

複雜功能的高階提示模式

  • [34:00] 許多連結資源涵蓋如何針對各類任務有效提示這些工具,包括實作精細的功能。
  • [34:10] 像讓某個東西看起來有復古感這類簡單要求只需簡短的提示,而複雜、正式的功能則可能需要長達數段的提示。
  • [34:22] 一種高階模式是以產品設計師或產品經理的身分撰寫完整的 PRD,將該 markdown 文件交給編碼代理,並要求它閱讀、擬定實作計畫並加以實作。

目錄範圍與 Git 儲存庫操作

  • [34:58] 粗略來說,在某个目錄中啟動的編碼代理會看到該目錄及其子目錄中的所有內容,而部分代理也能要求查看其外的內容。
  • [35:12] 這些細節在這個相當新的領域中會因眾多工具而異,但一般而言你會希望在 Git 儲存庫的層級上操作。

課程網站上的 Claude Code

  • [35:43] Claude Code 曾用於多項課程網站變更,部分原因是為了實際試用這項技術,包括在該講座被抽離為獨立講座時進行的重構。
  • [36:03] Claude Code 做得還不錯,但並非完美:它沒有把所有練習都搬到完全正確的位置,有些練習消失了而必須還原,而一個連結的 GitHub 提取請求則顯示了哪些部分是手動修正的。
  • [36:26] Tufte 風格的邊註也是用 Claude Code 實作的,據說 Jon 自己一行那樣的程式碼都沒寫。

以氛圍編碼進行創意前端工作

  • [36:33] 對於創意工作,給代理高層次的描述、檢視結果並提供微調回饋可能非常有效,現在許多人就是這樣進行前端與網頁設計。

編碼代理的使用案例

重構與程式碼審查

  • [37:14] 編碼代理對於重構很有用,而 Claude Code 曾被用來重構講義;同樣的工具也能重構程式碼。
  • [37:25] 這些代理非常擅長程式碼審查:對於 GitHub 拉取請求或個人用途,只要請代理審查程式碼就能得到有用的回饋,而更明確的指引則能產生更貼合需求的回應。
  • [37:43] 講者在進行風格調整或效能最佳化、且意圖讓程式碼語意維持不變時,曾用這些工具捕捉細微的錯誤,因為代理能很好地推理程式碼與差異,並能標示出行為實際不同之處。

理解程式碼庫

  • [38:15] 這些代理非常適合用來理解程式碼庫,這對在較小儲存庫上工作、想貢獻開源專案或深入較大程式碼庫(例如實驗室供 UROP 使用的程式碼庫)的新手很有幫助。
  • [38:36] 你可以進入一個全新的程式碼庫並請代理解釋它,或者當 UROP 指導教授指派功能時,代理能協助找出需要修改哪些檔案,否則這項任務需要大量思考與理解。

編碼代理作為 shell、學校工具與氛圍編碼

將編碼代理當作自然語言 shell 使用

  • [38:59] 因為編碼代理能呼叫工具,它們可以某種程度像 shell 一樣使用,免去記憶 shell 指令及其旗標的需要。
  • [39:14] 你不必回想語法,可以用自然語言描述任務,例如請代理使用 ag 找出所有含有重新命名匯入的 Python 檔案。
  • [39:54] 你可以給出後續指示,例如忽略 lib 目錄,代理就會產生並執行正確的 shell 指令,甚至摘要結果。

學校政策與學術誠信疑慮

  • [40:17] 在學校作業上使用這些工具是否會惹上麻煩因課程而異,因此學生應詢問授課教師是否允許。
  • [40:25] 有些課程禁止任何 AI 編碼工具,包括 AI 自動補全、行內聊天與編碼代理,而其他課程則允許你使用任何工具。
  • [40:41] 講者建議遵守規則,不要在作業上作弊。

AI 工具作為類似助教的幫手

  • [40:50] Anthropic、OpenAI 等公司已推出專為學校作業設計的特殊功能或模式,它們不會直接給出答案,而是更像助教一樣運作。
  • [41:04] 為了優先確保你自己理解,你可以請工具給個提示,例如請它在不揭露答案的情況下,指點你正確的方向。
  • [41:12] 講者表示這些工具在這方面還算堪用,可以當作助教的替代方案。

用氛圍編碼完成整個專案

  • [41:22] 程式編碼代理可用於所謂的氛圍編碼,從頭到尾實作整個專案,而你完全不必親手寫任何一個字元的程式碼。
  • [41:37] 講者以讓網站看起來有復古風格為例,說明氛圍編碼的小範例。

進階代理功能:可重複使用的提示

儲存並重複使用提示

  • [42:35] 一項常見的進階功能,是避免對代理反覆輸入相同的指令,例如針對拉取請求重複使用的程式碼審查提示。
  • [42:47] 範例提示可以指定專案準則、著重程式碼品質與正確性,並說明安全性對該專案較不重要。
  • [43:07] 這些工具通常可讓你將整段提示存成文字,之後更輕鬆地叫用,而不必從文字檔複製貼上到程式編碼代理中。

平行代理與 git 工作樹

為何要平行執行代理

  • [43:40] 即使是相當簡單的任務,模型也可能需要幾分鐘來思考和處理。
  • [43:47] 有了更進階的功能後,模型已被觀察到能在程式碼庫上工作 20 或 30 分鐘,因此你不會想只是坐在那裡盯著看。
  • [43:55] 你可以啟動多個代理副本,平行處理一個錯誤修正以及一項或多項功能。
  • [44:04] 在本堂課中,不同的代理分別在不同的儲存庫上執行:一個 Claude 把課程網站改成復古風格,另一個則修正程式碼庫中的錯誤。

用 git 工作樹隔離代理

  • [44:16] 當你想讓多個代理在同一個程式碼庫上執行時,git 工作樹特別有用。
  • [44:25] 多個 Claude 若啟動在同一個目錄中,編輯檔案時會互相衝突,因此完全隔離的程式碼庫副本會很方便。
  • [44:42] Git 工作樹會建立各自獨立的簽出:它在目前位置建立新目錄與新分支,詳細資訊可透過 git help worktree 取得。
  • [44:59] 你可以在新目錄中啟動一個 Claude,並在原始副本中啟動另一個,給它們不同的任務,讓它們提交到各自的分支,之後再合併所有內容。

用於代理與工具連結的模型情境協定

MCP 作為連接代理框架與工具的標準

  • [45:35] 存在一套名為 Model Context Protocol 的標準化協定,用於連接 AI 代理框架與工具。
  • [45:43] 這些連接有各種開源選項,也有一些託管選項可供使用。

範例:將程式設計代理連接到 Notion

  • [45:50] 舉例來說,程式設計代理可以連接到 Notion,讓使用者把 Notion 文件網址交給 Claude,請它讀取並改進該處的實作計畫,然後在程式碼庫中實作該功能。
  • [46:04] Notion MCP 連接器可實現對 Notion 的讀取與寫入,作為將程式設計代理框架連接到其他工具的一種方式。

程式設計代理中的上下文管理功能

有限的上下文視窗與受污染的上下文

  • [46:29] 底層 AI 模型具有有限的上下文視窗,通常是固定的上限,例如 100,000 個輸入 token。
  • [46:43] 即使是技術上落在上下文視窗內的輸入,模型也較難處理,因而產生受污染的上下文。
  • [46:57] 有效管理提供給底層 AI 模型的輸入內容,通常能提升其效能。

清除上下文視窗

  • [47:04] 代理框架支援清除上下文視窗等基本功能,因為對話通常會持續將訊息、工具呼叫與工具輸出附加到訊息歷史中。
  • [47:25] 當切換到不相關的任務時,你可以終止並重新啟動代理,或使用指令清除上下文視窗並重新開始。

倒轉對話

  • [47:42] 代理框架可以透過從訊息歷史末端彈出訊息並將其從上下文中刪除,來倒轉對話。
  • [47:50] 用後續指令打斷代理,會讓自上一則訊息以來完成的所有內容留在上下文中,這令人困惑且不太有用。
  • [48:15] 倒轉有助於維持較小的上下文視窗,並讓 LLM 更專注。

長歷史的壓縮

  • [48:31] 壓縮功能透過壓縮歷史的前段,在固定上下文視窗下支援無限長度的對話,可自動或按需執行,例如在 ChatGPT 和 Claude Code 等工具中。
  • [49:01] 在 Claude Code 中,斜線指令 compact 會使用 LLM 將訊息歷史摘要成較小的內容,用以取代上下文的前段。
  • [49:24] 這些工具通常會自動壓縮,但當上下文過長而影響效能時,你可以手動呼叫。
  • [49:34] 工具會顯示壓縮後的上下文,講者建議試用程式設計代理,先進行幾次工具呼叫的來回互動,再壓縮並檢視 LLM 摘要。

用於模型知識截止點的 LLMs.txt

知識截止點與擷取函式庫文件

  • [50:03] 程式設計代理所使用的 AI 模型是凍結在特定時間點的,且是以訓練當時已存在的資料訓練而成,因此會有知識截止點。
  • [50:27] 若要求使用在截止點之後才發布的函式庫來實作功能,模型對它一無所知,因此必須在推論時於模型上下文中提供該函式庫的相關資訊。
  • [50:48] 語言模型可以從網際網路擷取程式碼;具備網頁擷取工具的程式設計代理可以取得諸如 missing.csail.mit.edu 的網址,將內容帶入其上下文視窗並據以行動。

LLMs.txt 作為純文字標準

  • [51:27] llms.txt 是一項為 LLM 格式化資訊的提案標準。
  • [51:34] 擷取原始 HTML 會讓模型取得冗長的內容,包括與任務無關的樣式與分析資訊。
  • [51:59] LLMs.txt 是一種純文字檔案的標準,旨在供 LLM 於推論時取用,許多函式庫(尤其是與 AI 相關或具前瞻性的函式庫)都會發布一份。

Pydantic AI 範例與上下文節省

  • [52:16] pydantic ai 的 llms.txt 比擷取 HTML 頁面內容好用得多。
  • [52:28] 它精簡許多,因此佔用的上下文較少,模型也能有更好的表現。

使用 agents.md 為程式設計代理提供儲存庫上下文

agents.md 的用途與設定

  • [52:47] 可以將一個大致標準化的檔案 agents.md 放在 Git 儲存庫或專案中,讓程式設計代理每次啟動時都讀取其完整內容。
  • [53:02] 對 Claude 而言,此檔案稱為 claude.md,講義或課程儲存庫中提供了範例。

避免在空白上下文中重複下達指示

  • [53:18] 當程式設計代理以完全空白的上下文啟動、對儲存庫一無所知時,此檔案便十分有用。
  • [53:31] 它可以記錄諸如執行型別檢查器的指令或執行單元測試的指令等做法,如此一來,代理每次啟動時就不必重新提供這些資訊。

可重複使用的提示與長期儲存庫

  • [53:44] 此做法與可重複使用的提示有關:每次程式設計代理啟動時,檔案內容都會被傾印到上下文的前置部分。
  • [53:52] 這些檔案對於你長期與程式設計代理一起協作的儲存庫來說非常有用。

技能作為隨需的上下文管理

大型 agent.md 檔案的極限

  • [54:09] 技能是作為 agent.md 檔案的後續發展而出現的,後者讓程式設計代理在啟動時就能了解專案的一切。
  • [54:24] 一份非常龐大的 Agents.md 會在對話開始之前,就消耗掉底層 LLM 上下文視窗的一大塊。
  • [54:36] Agent.md 檔案涵蓋了許多主題,但程式設計代理在任何給定時刻可能並不需要所有這些資訊都放在上下文中。

透過隨需技能檔案進行間接引用

  • [54:43] 一個巧妙的技巧是增加一層間接引用,而不是在 Agents.md 中預先提供所有資訊。
  • [54:50] 這份預先載入的檔案只提供概觀,指向分別說明開發實務、程式碼品質標準、執行測試與撰寫文件的獨立文件。
  • [55:06] 代理可以開啟那些獨立檔案,並依需求將它們載入自己的上下文視窗,大致上就像一次工具呼叫。

程式設計代理中用於上下文管理的子代理

子代理作為一種上下文管理機制

  • [55:42] 子代理有一部分與上下文管理有關:程式設計代理可以為某個已識別的子任務啟動一個獨立的代理,只針對該子任務給它一段提示,並在它完成後取回控制權。
  • [56:02] 因為子代理會將控制權交還給父代理,父代理的上下文不會被子代理所做的一切細節所汙染。
  • [56:12] 你可以為這些程式設計代理撰寫自己的子代理,而有時它們也被用來實作代理本身的各個元件。

以子代理實作的 WebFetch

  • [56:21] 有些程式設計代理將 WebFetch 工具實作為子代理,因為將整個網頁的 HTML 載入上下文視窗可能不是最好的做法。
  • [56:38] 一個獨立的代理可以擷取網站並摘要出相關的部分,因此只有經過篩選的相關資訊會進入父代理的上下文,上下文視窗也能保持較小。

以平行子代理提升速度

  • [56:54] 這些代理通常支援平行工具呼叫與平行子代理,讓多個任務能同時執行。
  • [57:02] 舉例來說,可以針對某個功能同時啟動一個具備強力程式碼審查提示的子代理,以及另一個具備強力提示、用來修正型別檢查問題的子代理。
  • [57:15] Claude Code 可以做到這點:兩個子代理同時運作,等兩者都完成後父代理再繼續,這也能加快你的速度。

透過筆記與練習來學習這些概念

  • [57:30] 講義中還有幾個更具體的例子,而高層次的目標是讓你熟悉這些概念。
  • [57:40] 你可以自行瀏覽,或跟著練習走一遍,這些練習會帶你逐步演練其中許多內容,學會在實務中運用它們。

在壓縮與開啟新工作階段之間做決定

壓縮是一門延續同一脈絡的藝術

  • [58:15] 決定要壓縮還是開啟新工作階段,與其說是科學,不如說是一門藝術。
  • [58:19] 沿著同一脈絡繼續時,產生壓縮脈絡的 LLM 摘要品質相當不錯,所以直接繼續下去就好。

為無關的工作清空脈絡

  • [58:29] 如果接下來的工作無關,就清空脈絡,而不是留著一堆無用的脈絡,即使那只是摘要也一樣。

AI 程式設計工具的注意事項與限制

LLM 是機率模型,並非魔法

  • [58:47] AI 工具很強大,但 AI 並非魔法,也可能出錯。
  • [58:55] LLM 是機率模型,給定輸入符元後,會回傳後續內容的條件機率,不過訓練出好的模型仍需運用巧妙的技術。
  • [59:07] LLM 的智慧運作方式與人類不同。

審查 AI 輸出的正確性與安全性

  • [59:11] 使用者應審查 AI 輸出的正確性與安全性錯誤。
  • [59:17] 講者經常看到 AI 產生的程式碼乍看之下合理,實際上卻有細微錯誤,而要找出這個錯誤得花上大量時間。
  • [59:25] 驗證程式碼有時比直接自己寫出正確的程式碼還難,所以對於真正關鍵的事,講者經常寧可親自處理,而不呼叫 AI 工具。

煤氣燈效應與除錯螺旋等失敗模式

  • [59:41] AI 可能會失控、鑽牛角尖,並在被指出錯誤時堅稱自己是對的,試圖對你進行煤氣燈操縱。
  • [59:52] AI 可能陷入除錯螺旋,反覆修正程式碼並執行每次都失敗的測試,讓程式碼愈改愈糟,因此必須緊盯它。

電腦科學與程式設計技能的持續價值

  • [1:00:06] 自己具備電腦科學與程式設計能力仍然很有價值,而且我們離電腦科學家被淘汰的地步還很遠。
  • [1:00:16] 這些是非常強大的工具,值得學習,講者也建議透過代理式程式設計的練習,學會在實務中使用它們。

免裝外掛讀取附時間戳記的現有字幕,匯出 TXT、SRT 或 Markdown。免費 YouTube 字幕擷取工具。

前 3 段雙語對照預覽 · 免消耗額度

以下展示前 3 段已儲存繁體中文譯文與英文原版字幕的逐段對照(零額度消耗):

[0:01]

好的,歡迎來到 Missing Semester 的第七天。今天我們要談代理式程式設計,延續上週「開發環境與工具」講座中涵蓋的一些 AI 驅動開發主題。上次在那場講座中,我們談到一些 AI 輔助程式設計,例如自動補全和行內聊天。今天我們要談的是我認為更為精密且新穎的東西,一種與五年前存在的形式不同的形態,也就是程式設計代理。

All right, welcome to day seven of Missing Semester. Today we're going to talk about agentic coding, so continuing some of the topics on AI-powered development that we were covering in the Development Environment and Tools lecture last week. So last time in that lecture we talked about some AI-assisted coding like autocomplete and inline chat. Today we're going to be talking about something that I think is much more sophisticated and new, a different form factor than what existed, say, five years ago. which is coding agents.

[0:32]

我想在這場講座一開始,先示範這些東西如何運作,然後我們會談它們在底層如何運作,接著談基礎,再談更進階的主題。這就是今天的計畫。至於最初的示範,我實際上會延續我們在「開發環境與工具」講座中做的範例。回想一下,我現在把程式碼叫到螢幕上。回想一下,我們當時正在寫這個小腳本,用來……

And I think right at the start of this lecture, I'm going to show you a demo of how these things work, and then we'll talk about how they work under the hood, and then talk about basics, and then more advanced topics. So that's the plan for today. So for the initial demo, I'm going to actually continue with the example we were doing during the development environment and tools lecture. So recall, I'll actually bring up the code here on the screen. So recall that we were working on this little script to...

[1:05]

喔,這不是我想要的。等一下,我需要做一點設定。好的,回想一下,我們當時正在寫這個腳本,用來從課程網站下載一個頁面並擷取其中所有連結。這就是我們目前的進度。相當簡單的小型 Python 腳本。假設我們想把這個臨時拼湊出來的腳本,變成稍微好一點的命令列程式。

Oh, it's not what I wanted. One second, I need to do a little bit of setup. Okay, so recall that we were working on this script to download a page from the course website and extract all the links from it. And this is where we're at. So pretty simple little Python script. And suppose we wanted to take this script we hacked together and turn it into a slightly nicer command line program.

[0:01] 好的,歡迎來到 Missing Semester 的第七天。今天我們要談代理式程式設計,延續上週「開發環境與工具」講座中涵蓋的一些 AI 驅動開發主題。上次在那場講座中,我們談到一些 AI 輔助程式設計,例如自動補全和行內聊天。今天我們要談的是我認為更為精密且新穎的東西,一種與五年前存在的形式不同的形態,也就是程式設計代理。

[0:32] 我想在這場講座一開始,先示範這些東西如何運作,然後我們會談它們在底層如何運作,接著談基礎,再談更進階的主題。這就是今天的計畫。至於最初的示範,我實際上會延續我們在「開發環境與工具」講座中做的範例。回想一下,我現在把程式碼叫到螢幕上。回想一下,我們當時正在寫這個小腳本,用來……

[1:05] 喔,這不是我想要的。等一下,我需要做一點設定。好的,回想一下,我們當時正在寫這個腳本,用來從課程網站下載一個頁面並擷取其中所有連結。這就是我們目前的進度。相當簡單的小型 Python 腳本。假設我們想把這個臨時拼湊出來的腳本,變成稍微好一點的命令列程式。

[1:40] 也許我們想做些像是加入引數解析,把要擷取內容的頁面變成命令列引數之類的事。所以我們可以請程式設計代理替我們端到端完成整個任務。在今天的講座中,我會使用 Claude Code,這是一種特定的程式設計代理。外面還有很多其他工具。今天的講座筆記底部有推薦軟體區段。它們都是命令列程式設計代理,至少我個人目前比較偏好使用這種。也有內建於 IDE 的程式設計代理。

[2:11] 所以如果你使用的是 VS Code 或 Cursor 這類工具,你可以在 IDE 內存取大致相同類型的功能。我在這裡啟動了 Claude Code。我剛在存放程式碼的目錄中開啟終端機,然後輸入 Claude,這就帶出了 Claude Code 的命令列介面。而程式設計代理基本上就是一個對話式 AI 模型,能存取一些有助於它撰寫程式碼的工具。所以如果你以前用過 ChatGPT 之類的東西,這大致上就像 ChatGPT,差別在於這個 AI 模型可以做一些事,例如讀寫檔案,以及在你的電腦上執行 shell 命令。

[2:50] 然後有時候這些模型會經過特別調校,讓它們在撰寫軟體方面特別有效率。我這個目錄裡有一個 dl.py 腳本,我可以直接給程式設計代理自然語言輸入,說些像這樣的話:把我寫的腳本……在這裡,我故意打錯字,就讓它保持這樣。把我寫的腳本 dl.py 改寫成一個正式的的命令列程式,用 argparse 來解析參數,也許我還想在其他方面讓它更好一些,像是這裡我漏掉的一些東西。喔,我想我這裡有型別註解,但我可以說,確保它有正確的型別註解,並用 mypy 檢查它能正常運作。

[3:36] 所以我想檢查型別註解,確保它通過型別檢查。我可以給它像這樣的命令,然後它就開始思考。接著讓我說明一下它的輸出。它會給出一些文字輸出,就像 ChatGPT 可以回文字給你一樣。但它也能夠呼叫工具。所以這個模型決定讀取 dl.py 這個檔案。這是開始這項任務的合理方式。而這是一項相當簡單的任務。所以它就更新了這個 dl.py,並一次就把所有程式碼寫好。

[4:09] 所以我可以進來這裡看看它做了哪些變更。如我們所見,它寫了更多函式。現在它有了這個 main 函式,裡面有命令列參數解析之類的東西。所以它就完成了我要求它做的整項任務。而且它繼續遵循我的指示。回想一下,我告訴它要確保有型別註解,並讓型別檢查器確認一切都沒問題。所以在這裡它正試著呼叫下一個命令。它使用 shell 呼叫 mypy,並把腳本的路徑作為參數傳給它。

[4:41] 我們在這裡注意到一件事:它在編輯檔案時,並沒有向我要求權限,就直接動手改了。這是因為我把我的程式設計代理設定成這樣。因為我覺得,如果它對檔案的每一個小變動都要請求權限,那實在有點煩人。我希望它能更自主地運作。而且一般來說,我會用 Git 追蹤所有東西,所以我可以讓程式設計代理做一大堆變更,之後再一次性審查。但對於程式設計代理想呼叫的其他工具,例如 bash,我就會確保自己真的逐一檢查它執行的每一個指令,確認它是安全的。

[5:14] 我們今天不會深入探討代理安全性的細節,但如果你讓這些東西完全自主運作,確實會有一些安全上的顧慮。它們可能會做出像是呼叫你不希望它呼叫的指令這類事情,可能會說要刪除你在意的檔案,或做出比那更危險的事。至少我個人建議你以這種模式執行它們,也就是你必須逐一核准那些會改變你電腦狀態的工具呼叫。所以我核准了那個工具呼叫。代理框架執行了工具,把結果交回給 AI 模型。模型說一切看起來都沒問題,接著它決定繼續往下走,想要執行腳本並確認它似乎能正常運作。

[5:50] 所以它在這裡又做了一些事。然後,就是這樣。它完成了我要求它完成的任務。這就是 AI……對,有問題嗎?好,所以問題是,這在背景使用某個託管的 AI 服務,你能限制自己花多少錢嗎?答案是,可以,你能。具體的設定選項會依工具而有所不同。

[6:22] 一般來說,你可以選擇想用哪個 AI 模型,而能力較高的模型可能更擅長解決較難的任務,但費用也更高;較小、較快的模型則比較便宜。所以這是你的一項控制手段。而某些服務實際上會稍微限制花費,例如你可以說我這個月最多只想花 10 美元。對。所以問題是,我會限制花費嗎?至少我個人不會,我會持續留意用量。例如在 Claude Code 裡,就有一個 slash cost 指令。

[6:53] 到目前為止,我們做的這件事大約花了 19 美分。所以我會持續留意,確保費用不會變得太不合理。我認為如果你是學生,一個不錯的入門方式可能是選擇每月 $20 的訂閱方案,方案中會包含一定的使用量。然後,如果你超過了那個使用量,就要思考你是如何使用 AI 的。我知道,在許多公司裡,人們發現這些工具深受工程師喜愛,而且能提升生產力到一個程度,相較於工程師時間的成本,工具的成本相對較低。

[7:26] 所以我知道很多人每月在 AI 使用上花費很多,像是好幾百美元,但對他們的需求來說沒問題。對,有問題。對。程式編寫代理會花錢,而且粗略來說也需要網路,沒錯。你也可以使用在本機執行的程式編寫代理,以及在你自己的筆記型電腦上執行的模型。這其中有取捨。

[7:57] 你必須使用能力低得多的模型,除非你有……基本上,你必須使用比我在这场示範中使用的能力低得多的模型。然後它們對簡單任務可能還行,但對較難的事情,它們就沒那麼好用。而且我認為就現今實務而言,使用其中一種託管模型並付費是合理的。對,問題是關於訂閱制與一次付費。我認為它們全都是訂閱制,因為推論成本就取決於使用量。然後定價模式各不相同,你可以上網搜尋找到。

[8:30] 我認為我們不想在這堂課花太多時間談定價。我們要談的是程式編寫代理概念上是什麼、如何有效使用它們等等。好。所以在這個例子中,我向你們展示了非常高階的概觀。示範如何使用程式編寫代理,展示我們如何與工具對話。這裡它把每件事都做對了。所以我只做了這個一次性的操作。我給它一個指令。它做了一堆事情,包括呼叫工具、查看結果、根據發生的情況呼叫更多工具。最後只是回來告訴我,好,我完成了你要求的事,結束。

[9:01] 你也可以和這些東西進行多輪互動。我其實可以把型別標註移除,它還是會繼續運作。它保有先前對話其餘部分的脈絡,而我可以像這樣送出後續訊息。現在這算是個有點蠢的例子。它某種程度上讓我們的程式變差了。但它確實完成了那件事。更實際的做法是,你會針對正在處理的東西反覆迭代。如果你正在開發某個功能,卻不太確定這個功能的規格應該是什麼,你可能會給它一些粗略的描述。它做出一個東西。然後你評斷結果。就像,喔,其實我想要你用這種或那種方式調整一下。而你可以用這種迭代的方式來使用這些工具。

[9:37] 另一件你可以做的事是,如果程式碼代理出錯了,不管是它做錯事而搞混了,還是你看到它偏離正軌、想得太多,或是以毫無道理的方式呼叫工具,你甚至可以中斷它,例如在我正在使用的這個工具裡按 Control-C,然後給它一則後續訊息。所以如果它呼叫了錯誤的工具,比如也許我把 mypy 裝在某個奇怪的位置,而它找不到,我就可以直接中斷它並說,其實 mypy 在這裡。用這種方式呼叫它。舉例來說。所以關於這點有什麼問題嗎?

[10:10] 是的。所以問題是,你能不能硬性規定像是不准碰這個檔案,或不准碰檔案的這個部分?在我使用的這個特定工具裡,我想你沒辦法硬性規定不能碰檔案的某個特定部分。實務上我會怎麼處理這種情況,我想就是像把所有東西都記錄在 Git 裡,看看它改了什麼。比如如果它改了你不想改的東西,就還原它。你也可以給它自然語言的指示。比如你可以說,移除所有型別標註,除了這個函式裡的以外,或是像不要改動這段程式碼。

[10:42] 而它通常能相當不錯地遵守這些指示。是的。所以問題是,我有沒有使用代理進行測試驅動開發的經驗?答案是有的。我想我們會在講座稍後一點談到這個。簡而言之,這是使用這些工具相當不錯的方式。要嘛你自己寫測試,然後告訴代理去做那件事,像是實作這個功能讓它能通過測試,或者你甚至可以用程式碼代理來幫你一開始就寫好測試,然後再請它實作能通過測試的功能。

[11:15] 這些東西搭配回饋迴路運作得非常好。所以如果你能把它們設定成這樣:比如說,也許你有一些無法編譯的程式碼,或是一些無法通過型別檢查的程式碼,之類的情況,你就可以說,這是錯誤訊息,這是你可以執行來重現錯誤的指令,然後持續處理直到修好為止。這跟測試驅動開發搭配起來也運作得很好。好,所以我們接下來會更深入討論在各種不同使用情境下實際怎麼使用這些工具等等。但今天我想涵蓋的一個主題,是稍微談一下底層的 AI 模型,以及這些代理框架是如何運作的。

[11:51] 現在,這是一個非常深入的研究領域。我顯然不會在我打算花在這上面的 10 分鐘內把這個主題完整涵蓋。我可以花一整個學期深入探討這個主題,但我想我會試著用恰到好處的抽象層次來涵蓋它,讓你學到有效使用這些工具所需的面向。所以我不會教你如何自己重新實作這些東西,但我會試著分享一點它們運作的方式,讓你了解它們擅長什麼、有什麼限制,以及為什麼人們在代理框架中實作了某些功能之類的主題。

[12:31] 所以 AI 程式設計代理大致上可以拆解成兩個主要部分。有一個底層的語言模型,然後包裝這個語言模型的是代理框架。我不知道大家對語言模型有多少背景知識。也許用過 ChatGPT 或任何一種 AI 語言模型的人可以舉個手。好,太好了。好,所以大部分人都舉手了。語言模型,有時也稱為大型語言模型,可以想成是在對完成字串的機率分布進行建模。

[13:08] 我們把那些稱為 y。將完成字串建模為提示字串的函數,我們稱之為 x。所以完成字串,完成字串 y,以及提示 x。所以它就只是這個條件生成模型,會告訴我們,給定特定的輸入、一串字串序列,對應於該輸入序列的輸出字串的機率分布是什麼。

[13:40] 然後當我們使用這些模型時,我們可以把它建模為一個分布,我會寫成 pi theta,這個以提示 x 為條件、對完成字串 y 的參數化分布。然後當我們使用這些東西時,我們從這個分布中取樣,你可以從這個 pi theta 中,在給定輸入的情況下取樣出字串 y hat。就這樣。這就是 AI 語言模型的全部。

[14:11] 而要找到一個能派上用場的良好機率分布,需要投入大量心力。但在實務上,使用它們的方式是把一堆文字、字串或符記當作輸入餵給它們,然後它們回傳一堆文字作為輸出。接著其他所有東西都建立在這項核心基本操作之上。這裡還有一個細節要注意:實務上這些東西的上下文視窗是有限的。基本上這代表這個字串的長度和這個字串的長度都有上限。

[14:51] 好,那麼,建立在這之上……像 ChatGPT 這樣的東西是怎麼運作的?對話式聊天是怎麼運作的?我想你大概可以想像怎麼把它用在單輪對話上。你在這裡給它一個問題,然後在這裡得到一個答案。那你要怎麼做後續追問?有任何想法嗎?如果你想做多輪對話的話。這些東西運作的方式是它們使用輪次標記,而你把整段對話歷史當作輸入餵給模型,它就會給你接下來的內容,也就是模型的輸出。

[15:33] 作為機率分布的輸出。所以你可能會給它一個像這樣的輸入,例如使用者說,我不知道,像在黑板上寫不下長問題,但比如一加一等於多少?然後助理可能會說,所以在第一個查詢中,當你從機率分布取樣時,這就是你的第一個 X,然後這東西就是你的第一個 Y hat。接著如果你想問一個後續問題。我這裡是刻意寫一個有依賴關係的查詢。

[16:10] 比如說如果你把那些一換成二會怎樣?所以這個查詢單獨來看並不是很有意義。你需要上下文才能正確回答這東西。而對話式聊天應用程式處理這種情況的方式,是它會再次從大型語言模型取樣,喔。並把這整串東西當作提示,其中這些東西以某種特殊編碼作為輪次標記。

[16:43] 所以使用者說了這個,語言模型的輸出是這個,現在使用者說了這個,而這會生成接下來的內容。所以這會是你的下一個輸出。好,所以我們已經從大型語言模型這項基本操作——你可以把它想成一個機率分布——談到如何把它們用於多輪對話。接下來我們會談到如何建立像代理框架這樣的東西,建立在那個概念之上。

[17:18] 到目前為止有任何問題嗎?很好。所以你可以再加一個概念,就像這裡我們加了輪次標記。現在我們可以對大型語言模型的輸出加上一些特殊的解讀。我們可以說模型能輸出像 assistant 加上一些文字這樣的東西。但我們也可以有像呼叫工具這樣的輸出。

[17:52] 所以我們會訓練一個語言模型,讓它能輸出在你機器上執行某些操作的請求。所以它可能是……舉例來說,就像我們在這裡放棄的那些東西。工具呼叫的一個例子,可能是要求執行,我不知道,像是 mypy、dl.py。然後我們會有一些包裝語言模型的程式碼,有點像在我們的對話聊天應用程式裡,我們有這個東西會透過串接整段訊息歷史來建構後續的輸入,我們會有一個 AI 代理的執行框架,有時也稱為鷹架,它會解讀這些輸出。

[18:43] 所以這是來自 LLM 的 y-hat。它會解讀這些輸出。它會實際在機器上執行它們,然後把它們回饋給語言模型作為後續的輸入。所以它會去做這件事。然後也許會有 mypy 工具產生的這一大堆文字輸出。像是它說正在檢查 dl.py, blah blah。也許它說第一行有錯誤,預期是 int,blah blah。而你會把這整段輸出拿來,直接塞進去作為 LLM 的另一個輸入。

[19:16] 所以這構成了你餵給 LLM 的 X 的後續組成部分。對,有問題嗎?所以問題是,AI 代理的執行框架究竟是什麼?它會做幾件事。它會實作程式碼中在迴圈裡執行 LLM 呼叫、取得輸出的那個部分。

[19:48] 如果有工具呼叫,它會實際分派該工具呼叫。它會實際在你的機器上執行這個東西。它會取得這個東西產生的文字,回饋給語言模型,讓它產生另一個輸出。所以……對,這就是它做的兩件主要事情。所以大致上,AI 代理的執行框架會接收你的使用者輸入,然後在一個 for 迴圈裡呼叫代理。如果它要求工具呼叫,它會執行該工具,把輸入回饋給語言模型,讓它產生另一個完成結果。

[20:19] 如果又是另一個工具呼叫,它會執行該工具,把結果回饋給語言模型並產生另一個完成結果,如此反覆,直到語言模型輸出給使用者的文字輸出,此時……它會說像是好我完成了,控制權回到使用者手上,然後你就可以繼續進行,所以你會有工具實作,就像你在程式設計代理裡可能有的工具,像是 bash,讓你的代理可以執行 bash 腳本,你可能會有像是檔案編輯、檔案讀寫之類的東西,而這些工具的組合,讓底層的 AI 模型能實際對你機器上的東西做出變更,並以某種聰明的方式開發程式碼。

[21:05] 能夠執行測試、觀察執行測試後發生什麼事,並在決定未來要採取哪些行動時把這些納入考量。喔,後面有人有問題嗎?好。所以這些就是主要的基礎元件。就像核心有一個語言模型,對字串或符元上的機率分布進行建模。然後還有這個代理框架,它會在迴圈中呼叫大型語言模型、進行反覆取樣,也會分派請求去執行工具、實際執行它們,並把結果回饋給語言模型以推動事情向前進展。

[21:41] 通常當你輸入查詢時,它會執行一堆工具,而在底層,它會從這個語言模型取樣非常多次。對,有問題嗎?對,所以問題是,代理框架與底層語言模型之間是否有個廣為理解的介面,讓它們可以互通?有的,有一些常用的介面。它並沒有完全標準化、讓大家都用同一套東西,但現在已經有相當程度的互通性。

[22:17] 舉例來說,有像 OpenCode 這樣的開源代理框架,你可以讓它們搭配不同的底層語言模型運作。它們可以搭配 Anthropic 的模型運作,也可以搭配 OpenAI 的模型等等。對,有問題。對,所以你指出語言模型是這種機率性的東西,而代理框架大概就像你在框架本身寫的程式碼是確定性的,這些東西要怎麼關聯起來?

[22:57] 它們是透過取樣關聯起來的。當代理框架呼叫大型語言模型時,它是在取樣一個輸出字串,然後根據那個字串去做事。所以你會呼叫語言模型,它會輸出像是執行這個指令之類的內容,然後你就會執行該指令並繼續下去。關於非確定性,這個語言模型是以聰明的方式訓練出來的,但它並不是以人類智慧的方式在運作。它有時會輸出不太正確的東西。所以它會呼叫你電腦上實際上不存在的指令,或使用這些檔案讀寫工具去寫出錯誤的程式碼之類的事情。

[23:30] 但實務上,這些東西往往運作得還算不錯。而隨著你使用它們,你會建立起直覺,了解它們擅長什麼、以及它們還不夠好到能做什麼。對。所以我們已經做過了。對。所以現在我們談過這些東西如何運作之後,我們可以回頭再看這個例子,並從底層運作方式的角度來理解它。

[24:01] 所以這裡我已經啟動了我的代理框架。在這個特定情況下,我使用的是由 Anthropic 開發的 Claude Code 代理框架。而在底層,它使用的是 Anthropic 的 Opus 4.5 大型語言模型。我在這裡給了它第一個查詢。所以那就像是右上角最初的使用者查詢。這裡的模型輸出了一些中間文字。然後決定呼叫一個工具。所以這個 LLM 呼叫了讀取檔案工具,並帶有一個參數 dl.py。那被送到了代理框架。

[24:32] 它將那個語言模型的輸出解讀為,喔,語言模型正在要求呼叫一個工具。它實際上在我的檔案系統上執行了檔案讀取操作,產生了對應於該檔案內容的資料,然後將那資料作為額外的輸入送回給語言模型。所以再次想想右上角的例子。這就是在把越來越多的東西加進那個 X 裡。就像我們有這個一開始的 X,然後我們不斷地把更多東西附加到這個對話歷史中,直到我們送進 LM 的最新訊息或資料為止。

[25:06] 在這個例子中。所以這裡最後送給 LLM 的東西會是那個檔案的內容。於是模型,我們執行推論,從模型取樣,現在模型決定輸出更多文字。它在給我關於目前狀況的更新。而且它也輸出一個呼叫另一個工具的請求。這次是檔案寫入工具。而在這個特定的代理框架中,我認為這相當常見。檔案寫入工具,有幾種不同的介面,而模型實際上可以提供一個差異檔,套用到該檔案上。

[25:38] 所以模型再次發出一個差異檔,它就只是一段文字,然後代理框架,在這個情況下是 Claude Code,會解讀那個差異檔並將它套用到我檔案系統上的檔案。所以它完成了那件事,它輸出了一些更多的中間文字。對,有這個檔案更新操作。有一個回傳值,就像是,喔,操作成功了,所以那被送回到這裡的模型,我們執行另一輪推論,模型決定呼叫另一個工具,這次它呼叫 mypy,也就是型別檢查器。於是代理框架解讀那個,執行型別檢查器,產生來自該工具的輸出,所以這次是成功,沒有發現問題,那被送回到模型,我們執行另一輪 LLM 推論,依此類推,而在它終於發出像是「我的回合結束了,這是給你的最終文字輸出」之前,這裡還有大約一個工具呼叫。

[26:27],然後控制權會回到使用者手上。所以像是針對單一使用者提示,這裡會發生一大堆 LLM 推論步驟,這裡也會發生一大堆工具呼叫,對吧?酷,接著在我們談論這些東西底層如何運作的同時,簡短提一下隱私。所以就像我們稍早談到的,這些容量較高的語言模型通常更擅長執行這些複雜任務,需要相當強悍的硬體來執行推論,像是從那個機率分布中取樣。

[27:02] 所以這些東西大部分都在雲端執行。而當你在使用這些東西時,除非你跟供應商有某種特殊協議,否則你就是在把資料傳送給他們。他們可能會用這些資料進行訓練之類的。所以如果隱私是個顧慮,請詳閱條款與細則的細節,或找到適合你的設定方式。好。那我們就把這堂課剩下的時間拿來談談一些使用案例。我想我會試著花……對,有問題。好,所以問題是,Claude 同時是 LLM 和代理框架嗎?答案是,有幾種不同的設定方式。

[27:36] 在這個情況下,代理框架的某個元件可以想成是與模型一起在雲端執行,而模型在這裡肯定是在雲端執行的。你也可以找到開源的代理框架,框架會完全在本機執行,但所有 LLM 推論步驟最後都會把資料送到雲端進行推論。對,酷。好,所以我們已經給了一個如何使用編碼代理來實作新功能的範例。你可以非常具體地指示這些東西該怎麼做。你也可以保持開放式,然後從那裡反覆迭代。

[28:07] 舉例來說,我可以進入課程網站(missing semester 網站)的 Git 儲存庫,然後啟動 Claude,接著我可以說類似「讓它看起來有復古感」。我們可以看看會發生什麼事。這會花上一兩分鐘。所以我們之後再回來看這個。編碼代理另一個很棒的用途,是拿來修正你程式碼裡的問題。所以如果你有編譯器問題,例如你的程式碼無法編譯,或者你有 linter 問題(我們週五會更詳細介紹 linter),但如果你有個工具會檢查程式碼中各種類型的錯誤,而它在抱怨,你就可以請編碼代理幫你修正這個問題。

[28:42] 如果你有單元測試,或者你的程式碼裡有 bug,至少初步嘗試解決那個 bug 的一個好方法,就是為那個 bug 建構一個測試。這整體來說是很棒的軟體工程實務。如果你的程式碼有問題,就寫個測試。這樣你之後就能把它當作回歸測試,確保未來不會出錯。然後一旦你有了測試,你就可以告訴程式設計代理,嘿,這個測試失敗了,找出問題所在並讓測試通過。為了快速示範這一點,其實有點難示範,因為我想在真實的程式碼庫裡操作,而你對那個程式碼庫沒有背景脈絡。

[29:15] 但我會的,所以別擔心這裡的所有細節,不過我們會試著在高層次上向你展示事情是如何運作的。好。所以我這裡有一個程式碼庫,別擔心細節,而我注意到程式碼有個問題。在這個特定情況下,我為那個問題手寫了一個單元測試。所以如果我執行這個測試,我會看到測試失敗。你也可以用程式設計代理來嘗試初步撰寫測試。

[29:46] 比如說你可以啟動代理並說,嘿,當我這樣使用程式碼時,事情就出錯了,你能為它寫個測試嗎?但一旦你有了滿意的測試,你就可以啟動你的程式設計代理。而我要複製貼上這個指令。你可以啟動你的程式設計代理並說些像這樣的話:有個 bug。我為它寫了一個測試。你可以用這個指令執行它來重現問題,修復這個 bug。

[30:17] 然後就像人類開發者在這種情況下會做的那樣,你會先執行測試來重現 bug。而這就是代理正在做的事。所以它用我給它的東西呼叫 bash,來執行我剛寫的那個特定測試。然後它查看……輸出並看到,是的,這確實失敗了。那個輸出本身並不是很有資訊量,或者說單靠它本身,不足以修復 bug。你需要實際閱讀並理解程式碼。所以這就是代理接下來在做的事。這些東西其實相當擅長瀏覽程式碼庫,以及理解程式碼,無論對 AI 模型來說理解意味著什麼。

[30:50] 所以我們可以看到,好,它在讀取檔案。這裡是它有的另一個工具。它有一個搜尋工具,可以讓它用正規表達式模式搜尋符合這個模式的各種東西。它在讀取更多檔案、搜尋、讀取、搜尋、讀取、思考。而我認為在這個特定案例中,這個錯誤夠簡單,它應該一次就能修好這個錯誤。另一件讓這些程式編寫代理有點難以示範的事,是它們並不是最快的東西。而這是很多人正在努力的事。像是,你要怎麼做出在它們所做的事情上真的很好又有效,但同時執行得更快的代理?

[31:25] 就像這裡我們看到的,喔,這東西已經跑了整整一分鐘。但沒錯,這讓示範變得有點棘手。我沒辦法快轉這個行為,因為我想做現場示範。但好,現在模型說,喔,這是我認為的錯誤所在。這裡,我要用這種方式修好它。然後它自己嘗試再跑一次測試,看看我是不是真的修好了這個錯誤。所以我們可以核准那個執行工具的請求。然後我們發現,喔,測試現在通過了。然後這裡,模型在做一些其他聰明的事。一般來說,當你修好程式碼中的錯誤時,你會想確保你剛剛寫來捕捉那個錯誤的測試確實通過。

[32:00] 但接著,你應該跑其餘的測試,確保你沒有弄壞程式碼庫中的其他東西。所以接著代理會去做其他像這樣有用的事。然後,好,現在代理思考和工作完了,它做了變更來跑測試,而且它給我一份它做了什麼的摘要。我甚至可以說像是提交這些變更,它就會幫我建立一個 git 提交,附上一些有用的提交訊息等等。沒錯,我們可以看到這些東西其實訓練得相當好。

[32:31] 它學到不應該只是憑空編造提交訊息。你要遵循儲存庫的風格。所以它其實在查看,好,它在看 git status 來看看哪些已暫存待提交、哪些有變更。它在看 git diff 來確保變更的是它做的事,而不是我過去做的其他變更。然後它在看 git log,像是最近的提交訊息長什麼樣子,這樣它就能試著符合那個風格。然後它寫出這個提交訊息,看起來其實相當合理,還說像是,喔,這修好了這個 GitHub 問題等等。後面有問題。

[33:22] 對,所以問題是,你給程式設計代理的提示詞,以及底層的大型語言模型,是否會影響程式碼的品質、程式碼有多冗長、完成任務需要多久時間等等。答案是絕對會。現階段要把大型語言模型的提示詞寫好,與其說是科學,不如說更像一門藝術。而且底層模型也在持續進步。所以情況可能是這樣:如果你用兩年前的模型,現在已經沒什麼人在用了,但如果你用非常舊的模型,也許還是能讓它完成這樣的任務,只是你必須非常詳細地描述該怎麼做。而現在你只要說「有個 bug,修掉它」,它就會自己搞定。

[33:55] 不過對,我想有很多資源。我想我們在筆記裡有連結一些,我們也可以再連結更多。有很多資源在講如何有效地給這些工具下提示詞,來完成各種類別的任務。你要怎麼讓它實作一個複雜的功能?人們會遵循各種模式。像這種好玩的小事,讓我的東西看起來有復古感,我只說了讓它看起來有復古感,它就開始動手做了。但對於任何複雜、認真的功能,你可能會有一個很長的提示詞,長達好幾段。或者你也許想遵循更接近業界人士所做的軟體開發實務。

[34:26] 也許你想戴上產品設計師的帽子、戴上產品經理的帽子,寫一整份 PRD,也就是產品需求文件,然後你可以把一份巨大的 markdown 文件交給程式設計代理,說:「嘿,這是 PRD。讀它、理解它、做一份實作計畫,然後把它實作出來。」所以你可以遵循這類不同的進階模式,來做更複雜的事情。對,有問題。[聽不清楚的問題] 對,所以問題是,如果你在某個目錄裡啟動程式設計代理,它會看到那個目錄和子目錄裡的所有東西嗎?

[35:03] 粗略來說,是的。有些代理也能要求查看目錄以外的東西。這個領域還很新,有很多不同的工具。而且像這類細節會因工具而異。不過對,基本上是的,你通常會想在 Git 儲存庫的層級上操作。太好了。所以我們看到了這個端到端的範例……在給定一個失敗測試的情況下修好一個 bug。我們也可以純粹為了好玩來看看,喔,它還在思考,讓我們的課程網站看起來有復古感。

[35:36] 好。對,我們先不要提交並推送那個變更。不過,這些東西對創意工作其實也相當有用。如果你讀了今天的講義,其中一部分就是為了實際試用這項技術,我們用 Claude Code 來處理一些與課程網站相關的變更。當我們把這堂課從先前的開發環境課程中抽出來、變成獨立的一堂課時,需要做一些重構。我們就直接請 Claude Code 幫我們做。它做得還不錯,但沒有做到完美。你可以看連結的 GitHub pull request,了解代理做了什麼,以及我後來必須手動修正哪些地方。

[36:11] 它沒有把所有練習都搬過來,也沒有放到完全正確的位置。有些練習就消失了,我必須把它們還原。我們也實作了像……等等,我先把這些變更撤掉。我們也用 Claude Code 實作了像 Tufte 風格的邊註這類東西。所以,我想 Jon 自己一行程式碼都沒寫。那全都像是用 Claude Code 做 vibe coding。對,所以對這類創意工作來說,只要給代理一些高層次的描述、叫它去做某件事,然後看看結果、再給它回饋做微調,其實可以非常有效。

[36:44] 我知道現在有很多做前端設計、網頁設計這類工作的人,都在用這些代理。很好。所以我們正在討論使用案例。我想我們大約還剩 10 分鐘。所以我不會把講義裡的每一個使用案例都示範一遍。而且我相信你們還能想出比我們講義裡寫的更多使用案例。但我會簡短地講過一遍。然後我想把這堂課最後一點時間花在與代理相關的進階主題上。所以,用程式設計代理來做很有用的事情之一,包括重構。

[37:19] 所以如我所說,我們用 Claude Code 實際重構了我們的講義。但你也可以用這些工具來重構程式碼。這些代理非常擅長程式碼審查。所以如果有人送你一個 GitHub pull request,或者即使你只是想自己用,你都可以直接請代理「審查這段程式碼」。光是這種程度的指示,就已經能拿到相當有用的回饋。你也可以給它更具體的指示,以獲得更有針對性的回應。所以,我經常把這些工具用在一個地方,而且實際上還靠它們抓到一些非常隱微的錯誤,那就是在我做效能最佳化,或其他類型的重構,而希望變更前後的程式碼在語意上等價的時候。

[37:56] 不過沒錯,我當時在做的是風格上的調整、效能改進之類的事情。而這個編碼代理其實相當擅長推理程式碼,以及推理程式碼之間的差異。它能夠指出,喔,你做了這個變更。然後,喔,在這個特定情況下,行為其實不一樣了。接著我就可以去修正那個錯誤。所以這對程式碼審查真的很有用。這些代理也很擅長理解程式碼庫。所以我知道……我知道至少這裡有一大部分聽眾才剛開始接觸資訊科學。你們現在大概在處理比較小的儲存庫。但如果你想在 GitHub 上貢獻開源專案,或深入一個更大的程式碼庫,比如說你在做 UROP,正在處理你實驗室的程式碼庫,這些代理非常擅長瀏覽程式碼庫、理解它們,並向你解釋。

[38:36] 所以你甚至可以進入一個全新的程式碼庫,直接說,向我解釋這個程式碼庫。或者如果你在找特定的東西,也許你開始了一個 UROP,而你的 UROP 指導教授說,喔,去做這個功能。光是知道要修改哪些檔案,就可能需要很多思考和理解,對吧?但這些編碼代理相當擅長幫你處理這件事。另一個我會示範的快速例子,我要把它連結回我們先前的一些課程。因為這些編碼代理可以呼叫工具,你可以把它們有點像 shell 一樣使用。所以我現在其實會這樣用,因為記住 shell 指令和它們所有的旗標之類的東西可能很有挑戰性。

[39:11] 而你可以改用自然語言或編碼代理來解釋。怎麼做?你想要它做什麼,比如說,假設我想用一個指令找出這個目錄中所有有重新命名匯入的 Python 檔案,我可以說,用 ag,這是一個命令列工具,找出所有做重新命名匯入的 Python 檔案。所以這就像是 from something import something as something,或只是 import something as something,在 Python 裡,這只是我在找的某個特定模式,但像這樣自己打出來要難得多,像是要記得 ag 有這個 --python 旗標,或者確切的規則表達式要怎麼格式化之類的,所以你只要跟你的編碼代理說話,讓它像你的 shell 一樣運作。

[39:54] 而且你可以給它後續指令,例如忽略 lib 目錄。所以這是在同時搜尋我寫的程式碼和這個函式庫目錄裡的程式碼。所以也許我只想搜尋我寫的程式碼。你就可以給它這樣的後續指令。它會產生正確的 shell 指令、替你執行,甚至幫你摘要結果。所以我覺得這是個很棒的應用案例。對,有問題嗎?是的,問題是,在學校作業上使用這些工具會不會惹上麻煩?請詢問你的授課教師,看看使用這些工具是否沒問題。我認為這因課程而異。

[40:25] 我看過有些課程禁止使用任何 AI 程式設計工具。所以像是不能用 AI 自動補全、不能用行內聊天。甚至連我們在上一堂課講過的較簡單功能也不行。然後顯然也不能用 AI 代理。我也看過其他課程是,對,你想用什麼工具就用什麼工具。是的。所以請遵守規定。不要在你的作業上作弊。我也認為有些公司,像是 Anthropic 和 OpenAI 等等,已經為學校作業推出了他們軟體的特殊功能或模式。

[40:57] 所以你可以用一種模式,它們不會直接給你答案,而是表現得更像助教。或者如果你只想優先增進自己的理解,你可以向工具要提示。即使它沒有為此設計特殊模式,你也可以說,程式碼裡有個錯誤,不要告訴我錯誤是什麼,但指點我正確的方向。它們在這方面還算堪用。所以是的,你可以把它們當作助教的替代品。太好了。最後,我想我們不會深入討論這個,但你可以用 AI 代理來做所謂的 vibe coding。這些東西是相當強大的工具,你實際上可以從頭到尾實作整個專案,完全不用自己寫任何一個字元的程式碼,只要用英文和你的語言模型聊天就行。

[41:37] 我認為「讓網站看起來有復古感」就是個小例子。好,那麼關於 AI 代理的這些應用案例、vibe coding,有什麼問題嗎?太好了。現在讓我們稍微談談這些代理更進階的功能。我也會把其中一些連結回我們先前談過的、這些東西在底層如何運作。所以也許我會把這些寫在黑板上。

[42:13] 這些軟體大多都有幾個功能。從進階功能中最簡單的開始。你可能會經常發現自己對這些代理輸入相同的指令。比如說你在做某個特定專案,而你對代理該如何為別人送給你專案的 pull request 做程式碼審查,有特定的做法。

[42:47] 因為像是,喔這裡有專案準則,然後喔你的審查應該聚焦在程式碼品質,也應該聚焦在正確性,但不用擔心安全性,因為安全性在這個專案裡比較不相關。你可能會……發現自己經常把這個提示當作輸入,餵給你的語言模型或你的編碼代理。所以通常這些東西都有某種功能,讓你可以直接儲存一整個提示,就像一大段文字,之後更輕鬆地叫用它,這樣你就不用在某處放一個文字檔,再從裡面複製貼上到編碼代理裡。

[43:26] 我認為另一個非常有用的功能是平行代理。在某些編碼代理的執行框架中,這其實是內建功能。在其他情況下,這只是你可以遵循的工作流程。但正如我們所見,即使在我們今天走過的範例中,即使是相當簡單的事情,這些模型也可能要花幾分鐘思考和作業。如果你給它們更進階的功能去處理,我見過模型跑去做某個程式碼庫的工作,一弄就是 20 分鐘、30 分鐘的情況。你不會想就坐在那裡盯著它做這些事。你其實可以做的就是直接啟動多個代理的副本。

[43:58] 就像你可能會有。一個想修的錯誤,還有一個想寫的功能,還有另一個想寫的功能。而你可以平行完成所有這些事。所以即使在這一講裡,我其實也讓不同的代理同時在跑。在這個情況下,它們是在不同的儲存庫上。我讓一個 Claude 去把課程網站做得復古一點,同時讓另一個 Claude 去修我程式碼庫裡的一個錯誤。但當你想讓多個代理在同一個程式碼庫上執行時,有一個特別有用的工具叫做 git worktree。所以如果你直接在同一个目錄上啟動多個 Claude,比如說,而它們在編輯檔案,它們就會互相衝突,對吧?

[44:33] 所以真正方便的是,你擁有完全隔離的程式碼庫副本。而如果你是在 Git 儲存庫裡工作,Git Worktree 就是做這件事非常有用的工具。所以,Git Worktree 讓你建立各自獨立的簽出。所以我就要這樣,Git Worktree。我不會詳細解釋這整個指令,你可以執行 Git help Worktree 來看細節。這個指令做的事是幫我建立這個新目錄,並且在我目前所在的同一個位置建立一個新分支。所以現在我可以在這裡啟動一個 Claude,然後到這裡來。

[45:05] 而這裡還有我儲存庫的另一份完整副本。我可以在那裡啟動另一個 Claude,並給它們不同的任務去做。我可以把這些變更提交到各自的分支,之後完成時再全部合併在一起。所以平行代理和 git 工作樹搭配得非常好。還有另一件事,我想稍早有人問過,就是有沒有標準化的協定可以把代理框架和 LLM 連接起來?跟這個有點相關的是,有一個標準化協定叫做 Model Context Protocol,是用來把代理框架和工具連接起來的。

[45:43] 所以有各種開源的東西,也有一些像是託管的東西,讓你可以例如把你的程式設計代理連接到 Notion。這樣你就可以直接跟 Claude 說,喔,這是某個 Notion 文件的 URL。像是把它填好、讀取裡面的內容、改進我在裡面寫的實作計畫,然後在我的程式碼庫裡實作這個功能。所以你可以例如透過 Notion MCP 連接器讀寫 Notion。所以這只是一種把你的程式設計代理框架連接到其他工具的方式。然後還有一堆功能。

[46:25] 在程式設計代理中,這些功能與上下文管理有關。回想一下我們提過,底層的 AI 模型有有限的上下文視窗。一般來說,模型有某個固定的視窗。可能是像,喔,這個模型只支援最多 100,000 個 token 的輸入之類的。而實際上,如果你給模型一大堆輸入,即使技術上在上下文視窗之內,要模型處理所有這些輸入本身就是更困難的任務。所以你會遇到這些情況,像是上下文被污染,或者說……被污染的上下文。

[46:57] 所以如果你能有效管理你餵給底層 AI 模型的輸入,它通常會表現得更好。而這些代理框架支援的一些功能,包括像是清除上下文視窗這類基本功能。通常我們聊天時,只是不斷把更多訊息附加到我們的訊息歷史中。而使用程式設計代理時,我們所有的工具呼叫以及那些工具呼叫產生的輸出,也會被附加到我們的訊息歷史,也就是所謂的上下文。所以如果我們在程式設計代理裡處理一個任務,然後想做一些跟之前無關的其他事情,你其實可以終止代理並重新啟動它。

[47:34] 或者它們通常有一個指令可以清空上下文視窗,讓你從頭開始。這些工具通常還能做的另一件方便的事,就是倒轉對話。假設你正在和代理聊天,接著你要求它做某件事,然後你改變了主意。處理這種情況的一種方式是打斷代理,直接給它一個後續指令。但這麼一來,自從你上一則訊息之後它所做的所有事情現在都留在上下文視窗裡,把這些東西留在那裡既令人困惑又沒什麼用。所以,這些代理框架通常有一個指令,你可以用它基本上把訊息從訊息歷史的尾端彈出,直接回到過去。

[48:12] 所以就像是假裝那些事情從未發生過,把它們從上下文中刪除。再說一次,這能幫你維持較小的上下文視窗,讓你的 LLM 更專注。關於這些有什麼問題嗎?另一個與上下文管理相關、這些工具支援的功能是壓縮。那麼,如果你的底層 AI 模型有固定的上下文視窗,你要怎麼支援無限長度的對話這類事情?一般來說,無論是在像 ChatGPT 這樣的對話式聊天工具,還是在像 Claude Code 這樣的編碼代理框架中,運作方式都是:一旦你的對話歷史變得非常長,代理就會自動或按需壓縮歷史前段的部分,藉此縮小上下文視窗。

[49:01] 所以我甚至可以,如果其中一些工作階段有開著,比如在 Claude Code 裡,我可以呼叫斜線 compact 指令。那實際上就是用 LLM 把我的訊息歷史拿來摘要成較短的訊息歷史。然後它會用這段摘要取代我上下文的前段。這樣我就能繼續和我的代理對話。一般來說,這些工具會自動執行這件事,但如果你發現自己已經和代理聊了一陣子,上下文變得過長而讓它無法有效運作,你可以手動呼叫這個指令。

[49:34] 所以我們可以在這裡看到,比如說,這會實際顯示給我們看這段壓縮後的上下文長什麼樣子。我們現在不會詳細看這個,但我建議你試著跑一個編碼代理,來回互動一下,讓它呼叫一些工具,然後壓縮歷史,看看 LLM 的摘要長什麼樣子。接下來與上下文管理有點相關的東西叫做 LLMs.txt。編碼代理所使用的底層 AI 模型是固定或凍結在某個時間點的,它們會不時更新。

[50:10] 但在任何特定時刻,模型都是凍結的,它是用模型訓練當時存在的某些資料訓練出來的,但不一定跟得上之後發生的所有事情。所以一般來說,語言模型有個東西叫做知識截止點。然後,舉例來說,如果你要求你的程式設計代理使用昨天才推出的某個函式庫來實作某項功能,模型對那個函式庫會一無所知。那麼你要怎麼處理這種情況?處理的方式是,在推論時,你把關於該函式庫的資訊放進模型的上下文裡給模型。

[50:44] 所以有一個……這些語言模型一般都能從網際網路抓取程式碼。所以我現在一時想不到具體的函式庫,但我可以說,抓取 missing.csail.mit.edu,然後給我一份課程時程的摘要。所以你可以說,這是我想要使用的某個函式庫的網址,讀取該函式庫的文件,然後寫一些能完成某件事的程式碼。

[51:15] 它就會抓取那個網站。在這個程式設計代理所擁有的一組工具當中,有一個網頁抓取工具,它會把抓到的內容放進自己的上下文視窗,現在它就能根據那些資訊做事了。而 llms.txt 的用途在於,llms.txt 是一個為 LLM 格式化資訊的提案標準。通常當這些程式設計代理從網站抓取東西時,這可以用好幾種方式設定,但如果你直接抓取原始 HTML,並把全部內容交給代理和底層的 LLM,HTML 相當冗長,裡面有一堆對模型沒那麼有用的資訊。

[51:50] 在這個例子裡,所有樣式資訊或我們的 analytics 之類的東西,都跟這個模型想做的事無關。所以 llms.txt 是一個標準,用來提供純文字檔案,專門供 LLM 在推論時取用。很多函式庫,尤其是比較與 AI 相關的、或比較有前瞻性的那些,都會發布 llms.txt。所以我們可以在這裡看一個例子。比如這是 pydantic ai 的 llms.txt,我們可以看到,這比模型去抓取這個頁面的 HTML 內容要好用得多。

[52:28] 特別是它緊湊得多,所以你用到的上下文更少,模型也能表現得更好。還有另一個算是標準的東西叫做 agents.md。這是一個你放在 Git 儲存庫或專案裡的檔案,其設定方式是,每當你啟動程式設計代理時,它就會讀取這個檔案的全部內容。

[53:02] 對 Claude 來說,這是一個 claude.md 檔案。如果你想看範例,我們其實在課堂講義或這門課的儲存庫裡就有一個。這是我們的 agent.md 檔案。所以如果你發現自己需要給你的程式設計代理一些指示,告訴它如何在你的儲存庫裡做基本的事情,比如當你把這些東西啟動起來、它們的上下文裡什麼都沒有時,如果上下文一開始完全是空的,它對你的儲存庫一無所知,對吧?所以如果你有某些遵循的做法,或者像是「喔,執行這個指令來跑型別檢查器、執行這個指令來跑單元測試」等等。

[53:38] 你不必每次啟動程式設計代理時都提供那些資訊,而是可以——這也跟可重複使用的提示有點關係,但這就像是每次你啟動程式設計代理時,直接把這個東西倒進上下文的前綴裡。所以這些東西對於你長期用程式設計代理處理的儲存庫來說真的很有用。這些代理框架支援的另一個與上下文管理相關的概念叫做技能。

[54:09] 所以像 agent.md 這類東西先出現,而技能幾乎可以說是它的後續發展。人們發現的一件事是,把一大堆資訊倒進這裡真的很有幫助,這樣每當你啟動程式設計代理時,它就知道關於你專案所需的一切。但這裡也有一個缺點。如果你有一個非常大的 Agents.md,你甚至還沒開始跟它對話,就已經把底層大型語言模型的上下文視窗佔掉一大塊了。所以人們發現,在這些 Agent.md 檔案裡,有關於一大堆不同主題的資訊,但在任何給定時刻,程式設計代理可能並不需要所有那些資訊都在它的上下文裡。

[54:43] 所以有個巧妙的小技巧可以增加一層間接性。與其在 Agents.md 裡預先提供所有資訊,你可以說,這裡是你可查看的不同事物的概覽。比如說,關於我們的開發實務和程式碼品質標準,請看這份文件。關於如何執行測試,請看這份文件。關於你應該如何撰寫文件等等,請看這份文件。所以你預先提供給代理的資訊量很少。然後在基本上是一個獨立的檔案裡——代理可以打開、查看,並按需載入它的上下文視窗,大致上是作為一次工具呼叫——你就有每個主題的所有後續資訊。

[55:28] 而我認為在程式編寫代理的脈絡管理方面,最後一個我想談的主題是子代理的概念。所以說,如果你有一個代理,為什麼不擁有多個代理呢?而你又為什麼會想要這樣的東西?嗯,這部分與脈絡管理有關。如果你有一個程式編寫代理,它正試圖完成某項任務,而它能辨識出一個要做的子任務,與其全部在同一行程式編寫代理中依序完成,如果它能啟動一個不同的代理,並給它一個提示,就像你希望它做的子任務那樣,那麼它就可以去做它的事、完成,然後把控制權交還給父代理。

[56:02] 如此一來,父代理的脈絡就不會被該子代理所做的一切細節所汙染。這聽起來可能有點抽象,但這些東西是……你可以為這些程式編寫代理撰寫自己的子代理,而有時它們也會被用在實作這些代理的元件上。舉例來說,有些程式編寫代理會把 WebFetch 工具實際上實作成一個子代理。所以當父代理想要擷取某個網頁的內容時,正如我們先前提到的,把一個網頁的整份 HTML 內容載入語言模型的脈絡視窗裡可能不是最好的主意,因為那裡有非常多東西。

[56:38] 但反過來說,如果你能啟動另一個代理,告訴它:擷取這個網站,並為我摘要它這方面的內容,那麼它就只會篩選出相關的資訊。然後那些相關資訊就會被放進父代理的脈絡中。這樣一來,你就能讓你的脈絡視窗保持較小。這也促成了一些不錯的功能,例如這些代理通常支援平行工具呼叫與平行子代理。所以也許你有好幾件事想平行進行。比如也許你寫了一個子代理,它有一個很棒的提示來做程式碼審查。而也許你寫了另一個子代理,它有一個很棒的提示來修正與型別檢查相關的問題。

[57:11] 而也許你正在開發某項功能,然後你平行啟動這兩個子代理。舉例來說,Claude Code 就能做到這件事。接著它們兩個就會同時做各自的事。而當它們都完成後,父代理就可以繼續進行。所以這也能加快你的速度。所以是的,我剛剛只是把這些東西講過一遍,而不是為每一個都做示範。我認為在講義裡我們有幾個關於這些東西更具體的例子,但就高層次而言,我們希望能讓你熟悉這些概念,然後你可以自行研讀這些內容,或是跟著我們的練習走,這些練習會帶你走過一連串這類東西,讓你真正學會如何在實務中使用它們。

[57:50] 所以對於編碼代理這些更進階的功能還有任何問題嗎?對,所以問題是當語言模型開始進行壓縮時,一般建議是讓它進行壓縮,還是開啟新的工作階段?我認為這是許多比較偏向藝術而非科學的事情之一。我認為如果你是在同一條思路上繼續,產生壓縮上下文的 LLM 摘要還算不錯。

[58:26] 所以如果你還在處理同一件事,就繼續做下去。如果你在做不相關的事,就清除上下文,而不是留著一堆沒用的上下文,即使那只是摘要。太好了。然後在我們結束之前,最後一點,我只想說這些工具真的很強大,但 AI 不是魔法,AI 工具也可能出錯。我認為它們在某種程度上希望能稍微揭開 LLM 的神祕面紗。它們就只是這些機率模型,給定一堆輸入 token,就給你接下來內容的條件機率。

[59:01] 它們就只是這樣。而要訓練出好的 pi theta,背後有許多巧妙的技術。但沒錯,LLM 並不像人類那樣具有智慧。所以沒錯,請務必審查你 AI 的任何輸出,檢查正確性和安全性錯誤。至少就我個人的使用經驗,我遇過很多情況是 AI 產生的程式碼乍看之下很合理,但就是有微妙的錯誤,而且要花很多時間才能找到錯誤。有時候驗證程式碼實際上可能比你自己寫出正確的程式碼還難。所以對於真正關鍵的事情,我常常寧可自己動手做,而不是甚至嘗試呼叫 AI 工具來完成任務。

[59:37] 還有許多其他可能出錯的地方,我相信你開始使用這些工具後就會體驗到。例如 AI 可能會失控、鑽牛角尖。它可能會試圖對你進行煤氣燈效應。有時候它會出錯。但如果你試著告訴它錯了,它就會一直堅持自己是對的。你可能會感到非常困惑。它可能會陷入除錯的迴圈。所以它會試著修正程式碼、執行測試。它會修正程式碼、執行測試,然後每次都失敗。它會讓你的程式碼越來越糟。所以你確實必須留意這些事情。所以沒錯,有很多潛在的缺點和需要注意的地方。而在高層次上,我認為自己具備電腦科學和程式設計技能仍然很有價值。

[1:00:10] 我們還遠遠沒到電腦科學家被淘汰的地步。但同時,這些工具真的非常強大,值得學習。所以今天關於代理式程式設計的內容就到這裡。我強烈建議你去做一些這個主題的練習,學習如何在實務中使用這些強大的工具。明天見。

查看英文原始字幕

[0:01] All right, welcome to day seven of Missing Semester. Today we're going to talk about agentic coding, so continuing some of the topics on AI-powered development that we were covering in the Development Environment and Tools lecture last week. So last time in that lecture we talked about some AI-assisted coding like autocomplete and inline chat. Today we're going to be talking about something that I think is much more sophisticated and new, a different form factor than what existed, say, five years ago. which is coding agents.

[0:32] And I think right at the start of this lecture, I'm going to show you a demo of how these things work, and then we'll talk about how they work under the hood, and then talk about basics, and then more advanced topics. So that's the plan for today. So for the initial demo, I'm going to actually continue with the example we were doing during the development environment and tools lecture. So recall, I'll actually bring up the code here on the screen. So recall that we were working on this little script to...

[1:05] Oh, it's not what I wanted. One second, I need to do a little bit of setup. Okay, so recall that we were working on this script to download a page from the course website and extract all the links from it. And this is where we're at. So pretty simple little Python script. And suppose we wanted to take this script we hacked together and turn it into a slightly nicer command line program.

[1:40] Maybe we want to do things like add some argument parsing, make this a page we want to fetch the contents from, a command line argument, things like that. So we can ask a coding agent to just do this entire task for us end-to-end. And so in today's lecture, I'm going to use Claude Code, which is one particular coding agent. There are a bunch of other tools out there. We have a recommended software section at the bottom of today's lecture notes. And they're both command line coding agents, which I at least personally prefer to use at this time. There's also coding agents built into IDEs.

[2:11] So if you're using something like VS Code or Cursor, you can access roughly the same type of functionality within the IDE. And so here I've started up Claude Code. I just opened up a terminal in my directory where I have my code, and I type. Claude, and this brings up this command line interface to Claude Code. And a coding agent is basically just a conversational AI model with access to some tools that will help it write code. So if you've used something like ChatGPT before, this is like roughly ChatGPT, except the AI model can do things like read and write files and execute shell commands on your computer.

[2:50] And then sometimes these models are specially tuned to be able to be particularly effective at writing software. And so I have this dl.py script in this directory, and I can go ahead and just give natural language input to the coding agent and say something like, turn the script I wrote... here, I typoed this on purpose, I'll just leave it that way. Turn the script I wrote, dl.py, into a proper command line program, use argparse for argument parsing, and maybe I wanted to make it nicer in some other ways like some things I'm missing here Oh, I guess I have type annotations here, but I can say, like, make sure it has proper type annotations, check that it works using mypy.

[3:36] So I want to check the type annotations, make sure it passes type checking. So I can give it a command like this, and then it starts thinking. And then let me just explain its output. So it gives some textual output, like ChatGPT can say text back to you. But then it can also invoke tools. So this model decided to read the file dl.py. That's a reasonable way to get started with this task. And this is a pretty easy task. So it just updates this dl.py and in one shot writes all the code.

[4:09] So I can go in here and see what changes it's made. As we can see, it's written some more functions. And now it has this main function that has the command line argument parsing and stuff. So it's just done the entire task I asked it to do. And it's continuing to follow my instructions. So recall that I told it to make sure it has type annotations and that the type checker says that everything is good. And so here it's trying to invoke the next command. So it's using the shell and invoking mypy and giving it as an argument the path to the script.

[4:41] And one thing we noticed here, when it was editing files, it didn't ask me for permissions. It just went ahead and edited stuff. That's because I have my coding agent configured in this way. Because I find that if you need to, if it's asking for permissions for every single little change it makes to files, it's kind of annoying. I want it to be able to operate more autonomously. And generally, I track everything in Git. So I can let the coding agent make a whole bunch of changes and then review them at once later. But for other tools that the coding agent wants to invoke, like bash, I want to make sure I'm actually checking every single command that it runs and making sure that it's safe.

[5:14] We won't get into the details of agent security today, but there are some security concerns if you let these things run fully autonomously. And they might do things like invoke a command that you don't want it to invoke. It might say delete a file that you care about or much more dangerous things than that. At least I personally recommend that you run them in this mode where you have to approve individual tool calls that can do things like mutate your computer state. So I've approved that tool call. The agent harness ran the tool. It gave the results back to the AI model. The model said that everything looks good. And then it's deciding to continue along and wants to run the script and make sure it seems to be working.

[5:50] So it's doing some more things here. And there we go. It has completed the task I asked it to complete. So that's what an AI... Yeah, question? Yeah, so the question is, like, this is using some hosted AI service in the background. Can you limit how much money you're spending? The answer is, yeah, yes, you can. It depends on the tool exactly what the configuration knobs are.

[6:22] Generally, you can select which AI model you want to use and then like higher capacity models, which might be better at solving harder tasks, will be more expensive. And then smaller and faster models will be cheaper. So that's one control you have. And certain services actually slightly limit spend. Like you can say, I want to spend at most, whatever, $10 this month. Yeah. So the question is, do I limit spending? At least personally, I don't. I keep an eye on the usage. So in Claude Code, for example, there's a slash cost command.

[6:53] And this thing we've done so far has cost us about 19 cents. So I'll keep an eye on this and make sure things are not getting too unreasonable. I think if you're a student, one decent way to get started might be go for one of the subscription plans that's $20 a month, and then they have some amount of usage included with that. And then if you're going above that amount of usage and think about how you're using AI. I know that In a lot of companies, people are finding these tools are loved by engineers and increase productivity enough that compared to the cost of the engineer's time, the tool cost is relatively low.

[7:26] And so I know a lot of people who are spending a lot, like several hundred dollars a month in AI usage, but it's fine for their purposes. Yeah, question. Yeah. Coding agents cost money and they also require internet to first approximation, yes. You can also use coding agents that run locally and use models that run on your own laptop. There are trade-offs there.

[7:57] You have to use much lower capacity models unless you have... You basically have to use much lower capacity models than the one I'm using in this demo, for example. And then they might work okay for simple tasks, but for harder things, they don't really work that well. And I think for practically today, it makes sense to use one of the hosted models and pay for it. Yeah, the question is about subscription versus pay once. I think they're all subscription because inference costs just depend on usage. And then the pricing models vary and you can Google them and find it.

[8:30] I think we don't want to spend too much time in this lecture talking about pricing. We're going to talk about what are coding agents conceptually, how to use them effectively, and so on. Cool. So in this example, I've shown you a very high level overview of it. demonstration of using a coding agent, show it how we can talk to the tool. Here it did everything correctly. So I just did this one shot thing. I gave it an instruction. It did a bunch of stuff, including calling tools, looking at the results, calling more tools, depending on what happened. And eventually just coming back to me and saying, okay, I did the thing you asked for, and the end.

[9:01] You can also do multi-turn interactions with these things. I can actually remove the type annotations, and it will keep going. It has the context from the rest of the previous conversation, and I can send a follow-up messages like this. And now this is a kind of silly thing. It's kind of making our program worse. But it did the thing. More practically, you would iterate on the thing you're working on. If you were working on a feature where you didn't exactly know what the spec of the feature should be, you might give it some rough description. It does a thing. Then you judge the results. It's like, oh, actually, I want you to tweak it in this way or that way. And you can use these tools in this iterative manner.

[9:37] Another thing you can do is if the coding agent goes wrong, either if it does the wrong thing and gets confused, or if you see it going off the rails and it's kind of thinking a lot or calling tools in a nonsensical way, you can even interrupt it, like with Control-C, for example, in this tool I'm using, and then give it a follow-up message. So if it was calling, say, the wrong tool, like maybe I had mypy installed in some funny location and it couldn't find it, I could just interrupt it and be like, actually, mypy is here. Call it this way. for example. So any questions about that?

[10:10] Yeah. So the question is, can you hard code things like don't touch this file or don't touch this part of the file? In the particular tool I'm using, I think you can't hard code not being able to touch a particular part of a file. The way I would approach something like that practically, I think it's just like keep track of everything in Git, see what it changes. like if it changes a thing you didn't want to change, revert it. You can also give it natural language instructions. Like you can say, remove all the type annotations except for in this function or like don't change this chunk of the code.

[10:42] And it generally does a decent job of respecting those instructions. Yeah. So the question is, do I have experience with test-driven development using the agent? And the answer is yes. I think we will get to that a little bit bit later in the lecture. TLDR is a pretty good way to use these tools. Either write the test yourself and then tell the agent to do the thing, like implement the feature such that it passes the tests, or you can even use the coding agent to help you write the test to begin with, and then you can ask it to implement the feature that passes the tests.

[11:15] These things work really well with feedback loops. So if you can set them up so that it's like, okay, maybe you have some code that doesn't compile, or some code that doesn't type check, or something like that, you can say, here's the error, here's what you can run to reproduce the error, and keep working at it until you fix it. And that works well with test-driven development as well. Okay, so we're going to talk a lot more about practically how to use these tools in different use cases and stuff. But one topic I wanted to cover today was talking a little bit about how the underlying AI models and also how these agent harnesses work.

[11:51] Now, this is like a very deep area of research. I'm obviously not going to cover this topic in its entirety in whatever. the 10 minutes I'm planning on spending on it. I could spend an entire semester going into in-depth on the topic, but I think I'm going to try to cover it at just the right level of abstraction such that you learn the aspects of these tools you need in order to be able to use them effectively. So I'm not going to teach you how to re-implement one of these things on your own, but I will try to share a little bit about how they work so that you understand what they're good at and their limitations and why people have implemented certain features and agent harnesses and topics like that.

[12:31] So AI coding agents consist of roughly, I'd break them down into two main parts. There's an underlying language model, and then wrapping that language model is an agent harness. And so I don't know how much background everybody has on language models. Maybe raise your hand if you've used ChatGPT or any kind of AI language model before. Okay, great. Okay, so most of the hands went up. Language models, or sometimes called large language models, can be thought of as modeling a probability distribution of completion strings.

[13:08] Let's call those y. Modeling completion strings as a function of prompt strings, which we'll call x. So completions, completions y, and prompts x. So it's just this. conditional generative model that will tell us, given a particular input, a sequence of strings, what is the probability distribution over output strings that correspond to that sequence of inputs.

[13:40] And then when we use these models, and then so we can say like model this as a distribution, I'll write it as pi theta, this parameterized distribution of completions y given prompts x. And then when we use these things, we sample from this distribution, You can sample strings y hat from this pi theta given inputs. And that is it. That's all an AI language model is.

[14:11] And there's a lot of work that goes into finding a good probability distribution that does something useful. But then in practice, how you use them is you give them a bunch of text or strings or tokens as input. And then they give you back a bunch of text as output. And then everything else is built on top of that core primitive. One other detail to note here is that in practice these things have a limited context window. And basically that means that the length of this string and the length of the string have bounds.

[14:51] All right, so then building on top of this... Like how does something like ChatGPT work? Like how does conversational chat work? I think you could probably imagine how you could use this for single turn chat. Like you give it a question here and you get an answer here. Like how would you do follow-up questions? Any ideas? If you wanted multi-turn chat. So the way these things work is they use turn markers and you give your entire conversation history as the input to the model and it gives you the next thing, the model's output.

[15:33] as the output from the probability distribution. So you might give it as an input something like the user says, I don't know, like can't write out long questions on the blackboard, but like what is one plus one? And then the assistant might say, so in the first query, when you sample from the probability distribution, like this is your first X, and then this thing is your first Y hat. And then if you want to ask a follow-up question, So I'm writing a dependent query here on purpose.

[16:10] Like what if you replaced the ones with twos? So this query on its own is not super meaningful. You need the context to be able to answer this thing correctly. And so the way a conversational chat app handles something like this is that it will sample Oh. from the large language model again with this whole thing as being the prompt with some special encoding of these things as turn markers.

[16:43] So the user said this, the language model output was this, now the user said this, and that will generate the next thing. So this will be your next output. Okay, so we've gone from this primitive of large language models, which you can think of as a probability distribution to how you use them for multi-turn chat. And next we will talk about how you build something like an agent harness, building on top of that idea.

[17:18] So any questions about that so far? Cool. So one more idea you can add is, like here we added turn markers. Now we can add some special interpretation of the outputs from the LLM. And we can say the model can output things like assistant and then some text. But we can also have outputs that are like call a tool.

[17:52] So we will train a language model such that it can output requests to do things on your machine. So it might be... So an example of this is something like the things we gave up here. An example of a tool call might be a request to run, I don't know, like mypy, dl.py. And then we will have some code that wraps the language model, kind of like in our conversational chat app, we had this thing which would build our follow-up inputs by concatenating together the entire message history, we will have an agent harness, also sometimes called scaffolding, which will interpret these outputs.

[18:43] So this is a y-hat from the LLM. It'll interpret these outputs. It will actually execute them on the machine and then feed them back into the language model as a subsequent input. So it will do the thing. And then maybe there's all this textual output from the mypy tool. Like it says like checking dl.py, blah, blah. Maybe it says like error line one, expected int, blah, blah. And you will take all this output and actually just stick it in as another input to the LLM.

[19:16] So this forms a subsequent component of the X that you'll feed into the LLM. Yeah, question? So the question is, what exactly is the agent harness? It does a number of things. It will implement the aspect of the code, which runs calls to the LLM in a loop, takes the outputs.

[19:48] If there are tool calls, it will actually dispatch the tool call. It will actually execute this thing on your machine. It will get the text that this thing produces and feed it back to the language model and have it produce another output. And so... Yeah, so those are the two main things it does. So roughly, agent harnesses will take in your user input, then in a for loop, call the agent. If it requests a tool call, it will execute the tool, feed the input back into the language model, have it generate another completion.

[20:19] If it's another tool call, it will execute the tool, feed back the results to the language model and produce another completion and so on until the language model outputs textual output for the user, at which point... it will say like okay I'm done, control's back to the user and then you can proceed and so you'll have tool implementations like you might have in your coding agent tools like bash so you can have your agent execute bash scripts you might have things like file edit like file read write and so on and together those combination of tools lets the underlying AI model actually make changes to things on your machine and like develop code in some intelligent way.

[21:05] Doing things like being able to run tests, see what happens when you run the test, and take that into account when deciding what actions to take in the future. Oh, is there a question in the back? Okay. So these are the main primitives. Like there's a language model that's modeling a probability distribution over strings or tokens at the heart. And then there's this. agent harness that will call the LLM in a loop, do repeated samplings, and also dispatch requests to execute tools, actually run them and feed the results back to the language model to drive things forward.

[21:41] Usually when you type in a query, it'll run a bunch of tools and kind of under the hood, it's sampling from this language model many, many times. Yeah, question? Yeah, so the question is, is there a well-understood interface between the agent harness and the underlying language model so that you can have them interop? And yes, there are some commonly used interfaces. It's not perfectly standardized and everybody's using the same thing, but there's a good amount of interop these days.

[22:17] So for example, there are open source agent harnesses like OpenCode, and then you can have them work with different underlying language models. They can work with the Anthropic models and they can work with the OpenAI models and so on. Yeah, question. Yeah, so you're pointing out that the language model is this probabilistic thing and the agent harness is probably like the code you write in the harness itself is deterministic, like how do these things relate?

[22:57] So they relate through sampling. When the agent harness calls to the LLM, it's sampling an output string and then doing things based on that. So you will call into the language model, it'll output something like run this command, and then you will run the command and continue. Related to the non-determinism, this language model is trained in a smart way, but it's not intelligent in the same way that people are intelligent. It will sometimes output things that are not quite right. So it'll call commands that don't actually exist on your computer or use these file read-write tools. to write incorrect code or things like that.

[23:30] But in practice, these things tend to work reasonably well. And as you use them, you'll build intuition for what they're good at and what they're not yet good enough to do. Yeah. So we already did. Yeah. So now that we've talked about how these things work, We can go back and look at this example again and understand it from the lens of how things work under the hood.

[24:01] So here I've booted up my agent harness. In this particular case, I'm using the Claude Code agent harness, which is developed by Anthropic. And under the hood, it's using Anthropic's Opus 4.5 large language model. I gave it the first query here. So that's like the initial user query up in the top right. The model here outputted some intermediate text. and then decided to call a tool. So this LLM called the read file tool with an argument dl.py. That went to the agent harness.

[24:32] It interpreted that language model output as like, oh, the language model is asking to call a tool. It actually executed the file read operation on my file system, produced that data which corresponds to the contents of the file, and then fed that back in as additional input to the language model. And so again, think about the example in the top right. This is adding more and more stuff into that X. Like we kind of have this X that we start off with, and we're appending more and more things to this conversation history up until the most recent message or data we're passing into the LM.

[25:06] in this example. So like the last thing sent to the LLM right here will be the contents of that file. So then the model, we run inference, we sample from the model, and now the model decides to output some more text. It's giving me an update on what's going on. And it also outputs a request to call another tool. This time it's the file write tool. And in this particular agent harness, and I think is fairly commonly done. the file write tool, like there are a couple different interfaces, and the model can actually supply a diff which is applied to the file.

[25:38] So again the model emits a diff, so it's just a chunk of text, and then the agent harness, in this case Claude Code, will interpret that diff and apply it to the file on my file system. So then it's done that, it's outputted some more intermediate text. Yeah there's this file update operation. has a return value which is like it's like oh the operation was successful so that was given back to the model here we run another pass of inference the model decides to call another tool this time it's calling mypy so the type checker so then the agent harness interprets that runs the type checker produces the outputs uh from the tool so this time it's like success no issues found that gets passed back in to the model we run another pass of LLM inference and so on and there's like one more tool call here before it finally emits the like I am done with my my turn, here's the final text output.

[26:27] and control goes back to the user. So like a whole bunch of LLM inference steps happening in here and a whole bunch of tool calls happening in here for a single user prompt, right? Cool, and then just a brief note on privacy while we're talking about how these things work under the hood. So as we talked about a little bit earlier, these higher capacity language models, which tend to be better at doing these complex tasks, need pretty beefy. hardware to run inference, like to sample from that probability distribution.

[27:02] And so these things run in the cloud for the most part. And when you're using these things, like unless you have some kind of special agreement with the provider, you are sending them your data. They may train on the data and things like that. So if privacy is a concern, look into the details of the terms and conditions or find some setup that works for you. All right. So let's spend the rest of this lecture talking a little bit about use cases. And I think I'll try to spend... Yeah, question. Yeah, so the question is, is Claude the LLM and the agent harness both? The answer is there are a couple different setups.

[27:36] In this case, a component to the agent harness can be thought of as running in the Cloud along with the model, which is definitely running in the Cloud here. You can also find open source agent harnesses where the harness will run entirely locally, but all the LLM inference steps will end up sending data to the Cloud for inference. Yeah, cool. Okay, so we've already given one example of how to use a coding agent. to implement a new feature, you can be pretty specific with how you give instructions to these things. You can also be open-ended and iterate from there.

[28:07] So for example, I can go into the Git repo for the class website, the missing semester website, and I'll boot up Claude and I can say something like, make it look retro. We can see what happens. This will take a minute or two. So we'll come back to this one. Another great use case for coding agents is using them them to fix issues in your code. So if you have a compiler issue, like your code doesn't compile, if you have a linter issue, we'll cover linters in more detail on Friday, but if you have a tool that checks for various types of errors in your code and it's complaining, you can ask a coding agent to fix the issue for you.

[28:42] And if you have unit tests, or if you have a bug in your code, one great way to at least take a first shot at solving that bug is to construct a test for the bug. This is a great software engineering practice in general. If there's something wrong with your code, write a test. So you can later use that as a regression test to make sure things don't go wrong in the future. And then once you have a test, you can tell the coding agent, like, hey, this test is failing, like figure out what the issue is and make the test pass. And so as a quick demo of this, it's a little bit hard to demo because like I want to work in a real code base and then you don't have context on the code base.

[29:15] But I will, so don't worry about all the details here, but but we'll try to show you at a high level how things are working. Okay. So I have a code base here, don't worry about the details, and I noticed an issue with the code. And in this particular case, I hand wrote a unit test for that issue. And so if I run this test, I'll see that the test fails. You can also use the coding agent to try to take the initial crack at writing the test.

[29:46] Like you could boot up the agent and say, hey, like when I use the code in this way, like things go wrong, can you write a test for it? But then once you have a test that you're happy with, you can boot up your coding agent. and I'm going to copy paste this command. You can boot up your coding agent and say something like, there's a bug. I wrote a test for it. You can run it with this command to reproduce the issue, fix the bug.

[30:17] And then just like a human developer would do in this case, like you'd first run the test to reproduce the bug. And that's what the agent's doing. So it's calling bash with this thing I gave it to run that particular test I just wrote. And then it looks at... the output and sees like, yes, this is indeed failing. That output on its own is not super informative or it's like on its own, not enough to fix the bug. You need to actually read and understand the code. So that's what the agent's doing next. These things are actually pretty good at traversing code bases and like understanding code, whatever understanding means for AI models.

[30:50] So we can see like, okay, it's reading files. It's here's another tool that it has. It has a search tool, which lets it search with a regex pattern for different things that match this pattern. It's reading more files, searching, reading, searching, reading, thinking. And I think in this particular case, the bug is simple enough that it should just, in one shot, fix the bug. Another thing that makes these coding agents a little bit hard to demo is that they're not the fastest things. And this is something that a lot of people are working on. Like, how do you make agents that are really good and effective at what they do, but also run faster?

[31:25] Like here we're seeing, oh, it's been like one whole minute this thing is running. But yeah, it makes demos a little bit trickier. can't fast forward this behavior because I wanted to do a live demo. But okay, now the model's saying, oh, here's what I think the bug is. Here, I'm going to fix it in this way. And then it tries to, on its own, run the test again to see, did I indeed fix the bug? So we can approve that request to execute a tool. And then we find, oh, the test passes now. And then here, the model's doing some other intelligent stuff. Generally, when you fix a bug in your code, you want to make sure that the test you just wrote to catch that bug indeed passes.

[32:00] But then, you should run the rest of the test, make sure you didn't break something else in the code base. So then the agent's going and doing other useful things like that. And then, okay, now the agent's done thinking and working, it's made the changes to run the test, and it's giving me a summary of what it did. And I can even say something like commit the changes, and it will create a git commit for me with some useful commit message and so on. Yeah, we can see these things are actually trained pretty well.

[32:31] It's learned that it should not just make up a commit message. You want to follow the style of the repository. So it's actually looking at, okay, it's looking at git status to see what stage for commit and what's changed. It's looking at a git diff to make sure what changes is the thing it did, not some other changes that I'd made in the past. And then it's looking at the git log, like what do the recent commit messages look like, so it can try to match the style. And then it writes this commit message, which actually looks pretty reasonable and says, like, oh, this fixes this GitHub issue and so on. question in the back.

[33:22] Yeah, so the question is, does the prompt you gave the coding agent and the underlying LLM affect the quality of the code, how verbose the code is, how long it takes to do the task, and so on. And the answer is absolutely, yes. It's a little bit more of an art than a science at this point to prompt LLMs well. Also, the underlying models are continually improving. So it might have been the case that if you use a model from two years ago, which nobody really uses now, but if you use a really old model, maybe you can still get it to complete a task like this, but you have to be really descriptive in how to do it. And now you can just be like, there's a bug, fix it, and it'll figure things out.

[33:55] But yeah, I think there's a lot of resources. I think we've linked some in the notes and we can link more. A lot of resources on how to effectively prompt these tools to do various classes of tasks. How do you get it to implement a sophisticated feature? And there are various patterns people follow. For silly things like this, make my thing look retro, all I said was make it look retro and it's going and doing the thing. But for any of the complex, serious feature, you might have a really long prompt that's paragraphs and paragraphs long. Or maybe you want to follow a software development practice closer to what people do in industry.

[34:26] Maybe you want to put on your product designer hat and put on your product manager hat and write a whole PRD, a product requirements doc, and then you can give a huge markdown doc to a coding agent and be like, hey, here's a PRD. Read it, understand it, make an implementation plan, and then implement it. So there are different advanced patterns you can follow like that to do more sophisticated things. Yeah, question. [inaudible question] Yeah, so the question is, if you start the coding agent in a directory, does it see everything in that directory and subdirectories?

[35:03] To first approximation, yes. Some of these agents also can request to see things outside of the directory. There's a lot of, the space is pretty new. There are a lot of different tools out there. And like some of these details vary between the tools. But yeah, basically, yes, you will want to operate at the level of a Git repository generally. Cool. So we saw this end-to-end example of... fixing a bug given a failing test. We can also just for fun see, oh this is still thinking, making our course website look retro.

[35:36] All right. Yeah, let's not commit and push that change. But yeah, these things can actually be pretty useful for creative work too. If you read the lecture notes for today, in part just to like kind of dog food this technology, we used Claude Code to work on a number of changes related to our course website. We needed to do some refactoring when we pulled this lecture out from the previous development environment lecture into its own lecture. And we just asked Claude Code to do that for us. It did a decent job, didn't get it perfect. You can see the linked GitHub pull request to see what the agent did and what I had to do manually to fix things up afterwards.

[36:11] It didn't carry over all the exercises and put them in quite the right place. Some of them just disappeared and I had to restore them. We also implemented things like... Here, let me get rid of these changes. We also implemented things like our Tufte-style sidenotes using Claude Code. So yeah, I think Jon didn't write a single line of that code himself. It was all just like vibe coding with Claude Code. And yeah, so for this kind of creative work, just telling the agent to do something with like some high level description and then just looking at the results and then giving it feedback to make tweaks can actually be super effective.

[36:44] Like I know a lot of people who do front-end design and web design and things like that using these agents these days. Cool. So we're talking through use cases. I think we have about 10 minutes left. So I'm not going to demo every single use case that we have in our notes. And also, I'm sure you can come up with many more use cases beyond what we've written in our notes. But I'll briefly talk through them. And then I want to spend the last bit of the lecture on more advanced topics related to agents. So some of the things that are very useful to do with coding agents include things like refactoring.

[37:19] So as I mentioned, we used Claude Code to actually refactor our lecture notes. but you can also use these tools to refactor code. These agents are really good at code review. So if someone sends you a GitHub pull request, or even if you just want to use it for yourself, you can just ask the agent, review this code. Just that level of guidance is already pretty good to get useful feedback. You can also give it more specific guidance to get a more tailored response. So one thing I've used these tools for a lot, and actually used them to catch some very subtle bugs, is in situations where I've done performance optimization, or other kind of refactoring where I wanted the code to be semantically equivalent before and after the change.

[37:56] But yeah, I was making like stylistic changes or performance improvements or things like that. And the coding agent's actually pretty decent at reasoning about code and reasoning about deltas between code. And it was able to point out like, oh, you made this change. And like, oh, in this particular case, the behavior is actually different. And then I could go and fix that bug. So really useful for code review. These agents are great for codebase understanding too. So I know like... I know at least a decent chunk of the audience here is just getting into computer science. You're probably working on smaller repositories right now. But if you want to contribute to an open source project on GitHub or dive into a larger code base, like say you're doing a UROP and you're working on your lab's code base, these agents are really good at navigating code bases and understanding them and explaining them to you.

[38:36] So you can even go into a fresh code base and just be like, explain this code base to me. Or if you're looking for particular things, maybe you start a UROP and your UROP mentor says, oh, go work on this feature. even to know what files to modify can take a lot of thought and understanding, right? But these coding agents are pretty good at helping you with that. Another quick one I'll demo, I'm tying this back to some of our earlier lectures. So because these coding agents can call tools, you can use them kind of sort of like a shell. And so I actually use this these days because memorizing shell commands and all their flags and stuff like that can be challenging.

[39:11] And instead, you can just explain in natural language or coding agent. how what you want it to do so like suppose I want to use a command to find all the Python files in this directory that have uh renaming imports I can say like use ag so this is a command line tool to find all Python files that do renaming imports so this is like from something import something as something or just import something as something in Python it's just a particular pattern I'm looking for but like typing this out myself is way harder like remembering that like ag has this --python flag or like how exactly to format regexes or things like that and so you just talk to your coding agent and have it behave like your shell.

[39:54] And you can give it follow-ups like ignore the lib directory. So this is searching for both code I wrote and code in this library directory. So maybe I just want to search the code I wrote. So you can give it a follow-up like that. It'll generate the right shell command, run it for you, and even summarize the results for you. So I think it's a pretty great use case. Yeah, question? Yeah, so the question is, will you get in trouble for using these tools on school stuff? please ask your instructor and see if it's okay to use these tools. I think it just varies by class.

[40:25] I've seen some classes that prohibit use of any AI coding tools. So like no AI autocomplete, no inline chat. So not even the simpler features we talked about in that previous lecture. And then obviously no coding agents. And then I've seen other classes that are like, yeah, use whatever tools you want to use. Yeah. So please follow the rules. Don't cheat on your your homework. I also think some of these companies, I think like Anthropic and OpenAI and others, have released special features or modes of their software for schoolwork.

[40:57] So you can use them in a mode where they won't give you an answer, but will act a little bit more like TAs. Or if you just want to prioritize your own understanding, you can ask the tool for a hint. Even if it doesn't have a special mode for this, you can be like, there's a bug in the code, don't tell me what the bug is, but point me in the right direction. And they kind of sort of work okay for that. So yeah, you can use them as a replacement TA. Cool. And then finally, I think we're not going to talk about this in depth, but you can use coding agents for what's called vibe coding. So these things are pretty powerful tools and you can actually implement entire projects like start to finish without writing a single character of code yourself just by like chatting with your language model in English.

[41:37] I think that make the site look retro is a small example of that. Okay, so any questions about those use cases of coding agents? vibe coding. Cool. So now let's talk a little bit about more advanced functionality of these agents. And I'm also going to tie back some of this to what we talked about earlier with how these things work under the hood. So maybe I'll write these down on the blackboard.

[42:13] A couple of features that most of these pieces of software have. Starting from the simplest of the advanced features. So one thing you might find yourself doing often is typing out the same command to these agents. Like say you're working on a particular project and you have a particular way you want the agent to do code review for pull requests people send to your project.

[42:47] Because like, oh here are project guidelines and oh your review should focus on code quality and it should also focus on correctness, but don't worry about security because security is less relevant in this project. You might... find yourself frequently giving this prompt as an input to your language model or to your coding agent. And so usually these things have some functionality where you can just save a whole prompt, just like a bunch of text, and invoke it more easily later so you don't have to have a text file somewhere where you copy-paste from it into the coding agent.

[43:26] Another feature that I think is really useful is parallel agents. In some coding agent harnesses, this is actually a built-in feature. In other cases, this is just a workflow that you can follow. But as we've seen, even in the examples we've gone through today, even fairly simple things, these models can take a couple of minutes to think and work. If you give them much more advanced features to work on, I've seen situations where the model goes off and works on a code base for 20 minutes, 30 minutes. You don't want to just sit there and stare at it while it's doing that. What you can do is actually just boot up multiple copies of agents.

[43:58] Like you might have. a bug you want to fix, and also a feature you want to write, and also another feature you want to write. And you can do all of these in parallel. So even in this lecture, I actually had different agents running. In this case, it was on different repositories. I had one Claude working on making the course website retro, while I had another Claude working on fixing a bug in my code base. But one particular tool that's really useful when you want to have multiple agents running on the same code base is something called git worktrees. So if you just boot up multiple Claudes, for example, on the same directory, and they're editing files, they're going to conflict with each other, right?

[44:33] So what's really handy is you have completely isolated copies of your code base. And if you're working in a Git repo, Git Worktree is a really useful tool for doing that. So Git Worktree lets you create separate checkouts. So I'm just gonna like Git Worktree. I'm not gonna explain this whole command in detail to you can do Git help Worktree to see the details. What this command does is it creates this new directory for me and also a new branch at the same place where I currently am here. And so now I can boot up one Claude in here and go here.

[45:05] And here's an entire other copy of my repository. And I can boot up another Claude there and I can give them different tasks to do. I can commit these things to their separate branches and I can merge them all together later when I'm done. So parallel agents and git worktrees go really well together. There's this other thing I think someone earlier was asking about, like is there standardized protocol to connect together agent harnesses and LLMs? Kind of related to that, there's a standardized protocol called Model Context Protocol, which is for connecting agent harnesses with tools.

[45:43] So there are various open source things and also some like hosted things, where you can, for example, connect your coding agent to Notion. So then you can just talk to Claude and be like, oh, here's a URL for a Notion doc. Like, fill it in, like, read the stuff in there, like, improve the implementation plan I've written in there, and then, like, implement the feature in my code base. So you can read and write to Notion, for example, with the Notion MCP connector. So this is just a way to connect your coding agent harness to other tools. And then there are a bunch of features.

[46:25] in coding agents that are related to context management. So recall that we mentioned that the underlying AI models have a limited context window. Generally, the models have some fixed window. It might be like, oh, this model only supports inputs up to 100,000 tokens or something like that. And then in practice, if you give a ton of input to a model, even if it's technically within the context window, it's just a harder task for the model to deal with all of that input. So you get these things like having a polluted or like... a polluted context.

[46:57] And so if you can effectively manage what you're giving as input to the underlying AI model, it'll generally perform better. And so some things that these agent harnesses support is basics like clearing the context window. Normally when we chat, we're just appending more and more messages to our message history. And with coding agents, we also have all our tool calls and the resulting outputs from those tool calls as getting appended to our message history, or also known as context. And so if we're working on one task in our coding agent, and then we want to do something else that's unrelated to what we were doing before, you can actually kill the agent and start it again.

[47:34] Or they generally have a command just clear the context window. So you're starting fresh. Another handy thing that these things can generally do is rewind the conversation. So say you were chatting with the agent, and then you asked it to do something, and you changed your mind. One way you could handle that is by interrupting the agent and just giving it a follow-up instruction. But then all the stuff it's done so far since your last message is in the context window now, and it's confusing and not very useful to have it there. And so instead, these agent harnesses generally have a command you can use to basically pop messages off the end of the message history and just go back in time.

[48:12] So it's like, pretend the stuff never happened, delete it from the context. And again, that'll help you keep a smaller context window to keep your LLM more focused. Any questions about these? Another feature related to context management that these things support is compaction. So how do you do things like support unlimited length conversations if your underlying AI model has a fixed context window? Generally, the way that works both in conversational chat tools like ChatGPT and in coding agent harnesses like Claude Code is that once your conversation history grows really long, the agent will either automatically or on demand compress the stuff in the prefix of the history, and that'll shrink the context window.

[49:01] And so I could even, if some of these sessions open, in Claude Code, for example, I can call the slash compact command. And that actually just uses an LLM to take my message history and summarize it into a smaller message history. And that replaces the prefix of my context with the summary. And that lets me keep conversing with my agent. Generally, these tools will do this automatically, but if you found that you've been talking with the agent for a while and the context is getting too long for it to perform effectively, you can call this manually.

[49:34] And so we can see here, for example, this will actually show us what this compacted context is. And we're not going to look at this in detail right now, but I recommend you try running a coding agent, have a little bit of back and forth, have it call some tools, and then compact the history and just look at what the LLM summary looks like. The next thing kind of related to context management is something called LLMs.txt. So the underlying AI models used in coding agents are fixed or frozen in time, and they get updated from time to time.

[50:10] But at any given moment, the model is frozen and it's trained on some data that existed at the time the model was trained, but it's not necessarily up to date on everything that's happened since. So generally, language models have something called a knowledge cutoff. And then if you ask your coding agent, for example, to implement a feature using a library that came out yesterday, the model will know nothing about that library. And so how do you handle that? And so the way you handle that is that at inference time, you give to the model in the model context information about the library.

[50:44] And so there's a... These language models can generally fetch code from the internet. And so I don't have a library in mind at the moment, but I can be like, fetch missing.csail.mit.edu and give me a summary of the class schedule. So you could be like, here's a URL for a library I want to use, read the library documentation, and then write some code that does xyz.

[51:15] And it'll fetch the website. among the set of tools that this coding agent has as a web fetch tool, and it will get that into its context window, and now it can do things based on that information. And so where llms.txt comes in is that llms.txt is a proposed standard for formatting information for LLMs. Normally when these coding agents fetch things from a website, and this can be set up in a number of ways, but if you were to say fetch the raw HTML and give all that to the agent and to the underlying LLM, HTML is pretty verbose and there's a bunch of information there that's not that useful to the model.

[51:50] In this case, all the styling information or our analytics or things like that are not things that are relevant to what this model is trying to do. And so LLMs.txt is a standard to have plain text files that are meant to be consumed by LLMs at inference time. And a lot of libraries, especially the more AI-related ones or the more forward-thinking ones, will publish an LLMs.txt. And so we can look at an example here. Like here's the pydantic ai llms.txt, and we see that this is way more usable than if the model were to fetch the contents of this page in HTML.

[52:28] And in particular, it's way more compact, so you're using less of your context, and the model's able to perform better. There's another kind of standard-ish thing called agents.md. And this is a file that you put in your Git repository or in your project where the setup is that whenever you boot up the coding agent, it will read the entire contents of this file.

[53:02] For Claude, this is a claude.md file. If you want to see an example, we actually have one in the lecture notes or in the repository for this class. Here's our agent.md file. And so if you find yourself needing to give your coding agent instructions on how to do basic things in your repository, like when you boot these things up without anything else in their context, if the context starts out entirely empty, it knows nothing about your repository, right? So if you have certain practices you follow, or it's like, oh, run this command to run the type checker, run this command to run the unit test, and so on.

[53:38] Instead of having to supply that information to the coding agent every time you boot it up, This is, you can. This is kind of related to reusable prompts too, but this is like every time you boot up your coding agent, just dump this into the prefix of the context. So these things are really useful for repositories you're working on for a long time with coding agents. Another concept that these agent harnesses support related to context management is something called skills.

[54:09] So things like agent.md came first and skills was almost a follow up to this. So one thing people found was this really helpful to dump a whole bunch of information in here so that whenever you boot up your coding agent, it knows everything it needs to about your project. But there's also a downside here. If you have a really big Agents.md, you're filling up a large chunk of your context window for your underlying LLM before you're even getting started talking to it. And so people found that in these Agent.md files, there was information about a whole bunch of different topics, but at any given time, a coding agent might not need all that information in its context.

[54:43] And so there's this neat trick to add one level of indirection. So instead of supplying all the information upfront in Agents.md, you can say like, Here's an overview of the different things you can look at. Like see this document for our development practices and like code quality standards. See this document for like how to run the tests. See this document for how you should write documentation and so on. So you have that small amount of information supplied to the agent up front. And then in basically a separate file that the agent can open up and look at and load into its context window on demand, roughly as a tool call, you have all the follow-up information on each of the topics.

[55:28] And I think the final topic I want to talk about in terms of context management in coding agents is the concept of sub-agents. And so it's like, if you have one agent, why don't you have more than one agent? And why might you want something like that? Well, it's in part related to context management. If you have a coding agent that's trying to do some task and it can identify a sub-task to do, instead of doing that all kind of in line in the same coding agent, if it can spawn up a different agent. and give it as a prompt just like the subtask you want it to do, then it can go do its thing, finish, and then return control back to the parent agent.

[56:02] And then the parent agent's context is not polluted with all the details of what the subagent did. That might sound a little bit abstract, but these things are... You can write your own subagents for these coding agents, and they're also used in implementing components of these agents sometimes. So, for example, some coding agents implement the WebFetch tool. actually as a sub-agent. So when the parent agent wants to fetch contents of a web page, and as we mentioned earlier, loading the entire HTML contents of a web page into the context window of a language model might not be the best idea because there's a lot of stuff there.

[56:38] But instead, if you can spin up another agent, tell it, fetch this website, and summarize this aspect of it for me, then it'll filter out just the relevant information. And then that relevant information will be put into the context of the parent agent. And that way you keep your context window smaller. This also enables some nice things like these agents generally have support for parallel tool calls and parallel subagents. So maybe there's several things you want to do in parallel. Like maybe you've written a subagent that has a really great prompt for doing code review. And maybe you've written another subagent that has a really great prompt for fixing issues related to type checking.

[57:11] And maybe you work on a feature and then you spin off both of these subagents in parallel. This is something that Claude Code, for example, can do. And then they'll both do their thing at the same time. And then when they're both done, the parent agent can continue. And so this can also speed you up. So yeah, I just talked through these things instead of giving demos of each of them. I think in the lecture notes we have a couple more concrete examples of these things, but at a high level we wanted to familiarize you with the concepts, and then you can either go through this on your own or follow our exercises, which will walk you through a bunch of these, to really learn how to use these in practice.

[57:50] So any questions about these more advanced features of coding agents? Yeah, so the question is when a language model starts in compaction, is the general advice to let it do compaction or open up a new session? I think this is one of the many things that's like, it's a little bit more of an art than a science. I think if you're continuing along the same thread, like the LLM summarization that produces the compacted context is pretty decent.

[58:26] So if you're still working on the same thing, like just keep going. If you're doing something unrelated, like clear the context instead of just keeping a bunch of useless context around, even if it's a summary. Cool. And then just as a final note before we wrap up, just wanted to say these tools are really powerful, but AI is not magic and AI tools can make mistakes. They're, I think, hopefully to some degree demystified LLMs a little bit. They're just these probabilistic models that, given a bunch of input tokens, give you the conditional probability on the things that come next.

[59:01] That's all they are. And there's a bunch of clever techniques that go into training a good pi theta there. But yeah, LLMs are not intelligent in the same way that humans are. And so, yeah, make sure you review any output from your AI in terms of looking for correctness and security bugs. Like, at least in my personal usage, I've had many situations where the AI generates some code that looks reasonable at first glance. And it's just subtly wrong. And it takes a lot of time to find the bug. And sometimes verifying code can actually be harder than just writing the correct code yourself. So for, like, really critical things, oftentimes I will just do the thing myself instead of even trying to call an AI tool to do the task.

[59:37] There are also many other things that can go wrong, as I'm sure you'll experience if you start using these tools. Like the AI can just like go off the rails, go down rabbit holes. It can like try to gaslight you. Sometimes it'll like be wrong. But if you try to tell it it's wrong, it'll just like keep insisting that it's right. And you can get really confused. It can get stuck in debugging spirals. So it'll be trying to fix code, run the test. It'll fix the code, run the test, and it's like fail every time. It'll make your code worse and worse. So you kind of do have to keep an eye on these things. And so, yeah, a lot of potential downsides and things to be aware of. And at a high level, I think it's still valuable to have computer science and programming skills yourself.

[1:00:10] We're far from the point where computer scientists are obsolete. But at the same time, these are really powerful tools and worth learning. So that is all I have for you today on agentic coding. And I highly recommend you go through some of the exercises on this topic to learn how to use these powerful tools in practice. See you all tomorrow.

在 Vidleaf 工作台開啟

搜尋逐字稿、選取行、複製含時間戳的引文、翻譯。

姓名標示

「講座 7:代理式程式設計」由 Missing Semester(https://www.youtube.com/@missingsemester)製作,依 CC BY 4.0 授權(https://creativecommons.org/licenses/by/4.0/)。來源影片:https://www.youtube.com/watch?v=sTdz6PZoAnw。本頁為該影片的文字逐字稿,並加入了段落分隔與時間戳;創作者與 Vidleaf 無隸屬關係,亦不為其背書。Vidleaf 加入了 AI 標點與 AI 生成的摘要;原始措辭仍可於本頁取得。

你是創作者或權利持有人嗎?申請更正或移除:copyright@vidleaf.app(請見關於這些頁面)。

最後更新於