MIT Missing Semester 2026、講義7:エージェント型コーディング — 文字起こし
このページのタイトル、字幕、要約、読書メモは保存済みの AI 翻訳です。誤りが含まれる場合があります。タイムスタンプは原文と同じです。原文の字幕を開き、元の動画と照合できます。
MIT Missing Semesterはエージェント型コーディングツールを実演し、LLMのツール呼び出しループ、コンテキストとプライバシーのトレードオフ、テストワークフローと落とし穴について説明します。
文字起こしのソース:YouTubeでクリエイターがアップロードした手動字幕。AIによる句読点の追加はVidleafによるもので、元の文言とタイムスタンプのアンカーは保持されています。元の文字起こしは以下でご覧いただけます。
AI生成の要約
この講義は「開発環境とツール」の講義で扱ったAIを活用した開発トピックの続きで、オートコンプリートやインラインチャットから、より洗練された新しい形態とされるコーディングエージェントへと進みます。コーディングエージェントとは、基本的にはファイルの読み書きやシェルコマンドの実行などのツールにアクセスできる会話型AIモデルです。デモではClaude Codeを使い、シンプルなPythonスクリプトdl.pyを、mypyで型注釈をチェックするargparseを用いたコマンドラインプログラムに変換します。講演者は安全性とコストについて論じ、コンピュータの状態を変更する可能性のある個々のツール呼び出しを承認すること、すべてをGitで追跡すること、デモのコストが約19セントだったことを推奨しています。エージェントには大きく二つの部分があります。一つは基盤となる言語モデルで、これはプロンプト文字列を条件とした補完文字列の確率分布をモデル化していると考えることができます。もう一つはそれを包み込むエージェントハーネスで、コンテキストウィンドウ、ターンマーカー、ツールを呼び出す出力を処理します。ハーネスはおそらく決定論的である一方、モデルは確率的であり、モデルは人間のように知的ではなく、時には完全に正しくないものを出力しますが、実際にはこれらのものはかなりうまく機能する傾向があります。講義ではまた、プロンプティング、クリア・巻き戻し・圧縮によるコンテキスト管理、llms.txtとagents.md、サブエージェント、そして注意点も扱います。LLMは確率的であり、出力は正確性とセキュリティバグについてレビューすべきであり、AIは脱線したりデバッグのスパイラルにはまったりすることがあるため、監視する必要があります。
文字起こしから生成されました。リンクされた箇所は元の動画と照らし合わせて確認してください。
タイムスタンプ付きの章の要約
Claude Codeによるエージェント型コーディング
AI支援コーディングからコーディングエージェントへ
- [0:04] この講義は「開発環境とツール」の講義で扱ったAIを活用した開発の話題を引き継ぎ、オートコンプリートやインラインでのチャットからコーディングエージェントへと進みます。
- [0:21] コーディングエージェントは、およそ五年前に存在したものよりも洗練された新しい形態として紹介されています。
- [0:36] まずエージェントがどのように動作するかをデモし、次にその仕組みを内部でどのように実現しているかを説明し、その後で基礎と発展的な話題を扱う予定です。
デモの課題:dl.pyをCLIプログラムに変える
- [1:21] 出発点は、コースのウェブサイトからページをダウンロードし、その中のすべてのリンクを抽出するシンプルなPythonスクリプトです。
- [1:33] 目標は、その場しのぎで作ったスクリプトを、より洗練されたコマンドラインプログラムに変えることです。たとえば、取得するページをコマンドライン引数として指定できるように引数解析を導入します。
- [1:48] コーディングエージェントにこの作業全体を最初から最後まで任せることができます。
ツールの選択とコーディングエージェントの定義
- [1:54] このデモでは、特定のコーディングエージェントであるClaude Codeを使用します。他のコマンドラインエージェントは講義ノートの推奨ソフトウェアのセクションに記載されています。
- [2:08] コーディングエージェントはVS CodeやCursorなどのIDEにも組み込まれており、ほぼ同様の機能を提供します。
- [2:32] コーディングエージェントとは基本的に、コードを書くのを助けるツールにアクセスできる対話型のAIモデルです。たとえばファイルの読み書きやシェルコマンドの実行などができます。
- [2:50] 中には、ソフトウェアの作成に特に効果を発揮するよう特別に調整されたモデルもあります。
自然言語によるプロンプトとツールの使用
- [3:05] プロンプトでは、argparseを使ってdl.pyを適切なコマンドラインプログラムに変え、適切な型注釈を付け、mypyでチェックするようエージェントに指示します。
- [3:49] エージェントはChatGPTのようにテキスト出力を生成しますが、ツールを呼び出すこともできます。ここではまずdl.pyを読むことにしました。
- [4:04] この簡単な課題では、エージェントは一度にdl.pyを更新し、関数とコマンドライン引数解析を備えたmain関数を追加しました。
- [4:32] 指示に従い、次にスクリプトのパスを引数としてシェル経由でmypyを呼び出し、型注釈をチェックしました。
権限と自律的な動作
コーディングエージェント:安全性、コスト、インタラクション
安全性のためのツール呼び出しの承認
- [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のスラッシュコマンドcostで確認でき、デモでは約19セントかかりました。
- [7:02] 学生にとっては、利用枠が含まれた月額20ドルのサブスクリプションプランが、始めるのに手頃な方法かもしれません。
コストのトレードオフとローカルモデル
- [7:17] 多くの企業ではエンジニアがこうしたツールを好み、生産性の向上によってツールのコストはエンジニアの時間に比べて比較的低く抑えられるため、月に数百ドルを使う人もいます。
- [7:45] コーディングエージェントには費用がかかり、概算ではインターネット接続が必要です。
- [7:50] 自分のノートPC上でモデルを動かすローカルのコーディングエージェントも可能ですが、はるかに低性能なモデルを使う必要があり、簡単なタスクには使えても難しいタスクにはあまり向きません。
- [8:11] 実際のところ今日では、ホスト型モデルを利用してその対価を支払うのが理にかなっていると講演者は考えています。
- [8:21] 推論コストは利用量に依存するため、料金は一般にサブスクリプション制であり、料金モデルはプロバイダーによって異なります。
反復的なコーディングエージェントのワークフローと安全策
反復的な改善とエージェントへの割り込み
- [9:27] 結果を判断し、あれこれ調整するようエージェントに指示しながら、ツールを反復的に使っていきます。
- [9:37] コーディングエージェントが誤作動したり、混乱したり、無意味にツールを呼び出したりした場合は、使用中のツールで Control-C を押すなどして割り込み、その後フォローアップのメッセージを送ることができます。
- [9:53] 例えば、mypy が通常と異なる場所にインストールされていたために間違ったツールを呼び出した場合、割り込んで mypy の場所と呼び出し方を伝えることができます。
ファイルを意図しない変更から守る
- [10:17] 使用中の特定のツールでは、どうやらファイルの特定の部分に触れないよう制限をハードコードすることはできないようです。
- [10:27] 実用的な方法は、すべてを Git で追跡し、望まない変更を元に戻すことです。
- [10:34] また、コードの一部を変更しないでくださいといった自然言語の指示を与えることもでき、エージェントは一般にそれをかなりうまく守ります。
エージェントを用いたテスト駆動開発
AI コーディングエージェントの仕組み:言語モデルとエージェントハーネス
説明の範囲と目的
- [11:38] 講演者は、基盤となる AI モデルとエージェントハーネスがどのように機能するかを、再実装するのではなくこれらのツールを効果的に使うのに十分な抽象度で説明する予定です。
- [12:18] 目的は、これらのツールが何を得意とし、どのような限界があり、なぜ特定の機能がエージェントハーネスに実装されたのかを伝えることです。
条件付き確率分布としての言語モデル
- [12:33] AI コーディングエージェントは、大きく分けて二つの主要な部分に分解できます。基盤となる言語モデルと、そのモデルを包み込むエージェントハーネスです。
- [13:04] 大規模言語モデルは、プロンプト文字列 x を条件とした補完文字列 y の確率分布をモデル化していると考えることができ、pi theta of y given x と表記されます。
- [13:55] これらのモデルを使うということは、入力が与えられたときにこのパラメータ化された分布から出力文字列 y ハットをサンプリングするということであり、それ以外のすべてはこの中核的なプリミティブの上に構築されています。
- [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 ハット) を解釈し、マシン上で実行し、その結果を後続の入力として言語モデルにフィードバックします。
ツールの出力をモデルにフィードバックする
エージェントハーネスと 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におけるエージェントハーネスのツール呼び出しループ
セットアップ: Opus 4.5 を使ったClaude Code
- [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:52] 複雑なタスクをよりうまくこなす高性能な言語モデルは、推論に多大なハードウェアを必要とするため、主にクラウドで実行されます。
- [27:05] プロバイダーとの特別な契約がない限り、これらのモデルを使うことは、モデルにデータを送ることを意味し、そのデータで学習される可能性があります。
- [27:11] プライバシーが懸念される場合は、利用規約を詳しく確認するか、自分に合った設定を見つけてください。
Claude とエージェントハーネスが動作する場所
- [27:30] Claude が必ずしも LLM とエージェントハーネスの両方であるとは限らず、いくつかの異なる構成があります。
- [27:36] この場合、エージェントハーネスの一部のコンポーネントはモデルとともにクラウドで実行されていると考えることができ、モデルは間違いなくクラウドで実行されます。
- [27:42] オープンソースのエージェントハーネスは完全にローカルで実行できますが、すべての LLM 推論ステップは依然としてクラウドにデータを送信します。
自由度の高いコーディングエージェントへの指示
- [27:55] コーディングエージェントは、非常に具体的な指示で新機能を実装することもできますし、自由度の高いところから始めてそこから反復することもできます。
- [28:08] クラス用ウェブサイトのGitリポジトリ、missing semesterのウェブサイトで、Claudeに「レトロな見た目にして」のような指示を出すことができ、一分か二分ほどかかります。
エージェントによるコードの問題の修正
- [28:22] コーディングエージェントのもう一つの優れた用途は、コード内の問題を修正させることです。
コーディングエージェントを使ってテストでバグを修正する
コンパイラ、リンター、テストの失敗を委任する
- [28:27] コードがコンパイルに失敗したり、リンターやその他のエラーチェックツールが警告を出したりした場合、コーディングエージェントにその問題を修正するよう依頼できます。
- [28:42] バグに対しては、まずそのバグのテストを書くのが良い第一歩であり、将来の破損を防ぐ回帰テストとしても機能します。
- [29:01] 失敗するテストができたら、コーディングエージェントにテストが失敗していることを伝え、問題を見つけてテストを通過させるよう依頼できます。
デモの準備:手書きの失敗するテスト
- [29:26] デモ用のコードベースで、話者はある問題に気づき、そのための単体テストを手書きしましたが、実行すると失敗します。
- [29:41] あるいは、コーディングエージェントに最初にテストを書かせることもできます。たとえば、ある使い方をしたときにコードがどう誤動作するかを説明する方法です。
エージェントがバグを再現しコードを読む
- [29:51] テストに満足したら、ユーザーはコーディングエージェントを起動し、バグがあることを伝え、再現するコマンドを渡し、バグを修正するよう依頼します。
- [30:21] エージェントはまずbash経由でテストを実行してバグを再現し、失敗を確認しますが、出力だけでは修正するのに十分な情報がありません。
- [30:40] 次にエージェントはファイル読み取りと正規表現検索ツールを使ってコードを読み理解します。話者は、これらのエージェントはコードベースの探索がかなり得意だと述べています。
速度の限界と自律的な検証
- [31:13] コーディングエージェントは最速のツールではなく、効果的かつ高速にする取り組みが活発に行われています。このデモは約一分間実行されていました。
- [31:35] モデルは診断と修正を述べ、その後自らテストの再実行を要求し、ユーザーが承認するとテストが通過します。
- [31:50] エージェントはまた、コードベースの他の部分を壊していないか確認するために残りのテストも実行し、その後行ったことを要約します。
リポジトリのスタイルで変更をコミットする
git diffとログからAIが生成したコミットメッセージ
diffと最近のコミットスタイルを確認中
- [32:42] このツールはgit diffを調べて、コミットされるのが今回の変更であり、無関係な過去の変更ではないことを確認します。
- [32:47] 次にgit logと最近のコミットメッセージを確認し、既存のスタイルに合わせようとします。
コミットメッセージを作成中
- [32:53] かなり妥当に見えるコミットメッセージを作成し、たとえばこの変更が特定のGitHub issueを修正するものであることなどを記載します。
コーディングエージェントへのプロンプトと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:59] コーディングエージェントはツールを呼び出せるため、ある程度シェルのように使え、シェルコマンドやそのフラグを覚える必要がありません。
- [39:14] 構文を思い出す代わりに、自然言語でタスクを説明できます。たとえば、agを使ってインポートの名前を変更するすべてのPythonファイルを見つけるようエージェントに頼むことができます。
- [39:54] libディレクトリを無視するといった追加の指示を出すと、エージェントが適切なシェルコマンドを生成して実行し、結果を要約してくれます。
学校の方針と学術的誠実性に関する懸念
- [40:17] 学校の課題でこれらのツールを使うと問題になるかどうかは授業によって異なるため、学生は担当教員に許可されているか尋ねるべきです。
- [40:25] AIオートコンプリート、インラインチャット、コーディングエージェントを含むあらゆるAIコーディングツールを禁止する授業もあれば、好きなツールを何でも使ってよい授業もあります。
- [40:41] 発表者はルールに従い、宿題でカンニングをしないよう助言しています。
TAのような支援者としてのAIツール
- [40:50] AnthropicやOpenAIなどは、答えを教えるのではなく、もう少しTAのように振る舞う、学校の課題向けの特別な機能やモードを公開しています。
- [41:04] 自分自身の理解を優先するために、ツールにヒントを求めることができます。たとえば、バグを明かさずに正しい方向を示してもらうよう頼むなどです。
- [41:12] 話者は、これらのツールはその用途で問題なく機能し、TAの代替として使えると述べています。
バイブコーディングでプロジェクト全体を作る
エージェントの高度な機能:再利用可能なプロンプト
プロンプトの保存と再利用
並列エージェントとgit worktree
エージェントを並列で実行する理由
- [43:40] かなり単純なタスクでも、モデルが考えて作業を進めるのに数分かかることがあります。
- [43:47] より高度な機能では、モデルがコードベース上で20分や30分も作業している例が確認されており、ただ座って見ているわけにはいきません。
- [43:55] 複数のエージェントのコピーを起動して、バグ修正と一つ以上の機能を並行して処理できます。
- [44:04] この講義では、異なるエージェントが異なるリポジトリで実行されました。一方のClaudeがコースのウェブサイトをレトロにし、もう一方がコードベースのバグを修正しました。
git worktreeでエージェントを分離する
- [44:16] git worktreeは、同じコードベース上で複数のエージェントを実行したい場合に特に便利です。
- [44:25] 同じディレクトリで複数のClaudeを起動すると、ファイルを編集する際に互いに競合するため、完全に分離されたコードベースのコピーが便利です。
- [44:42] Git Worktreeは別々のチェックアウトを作成します。現在の場所に新しいディレクトリと新しいブランチを作り、詳細はgit help worktreeで確認できます。
- [44:59] 新しいディレクトリで一つのClaudeを、元のコピーでもう一つを起動し、それぞれに異なるタスクを与え、別々のブランチにコミットし、後で全てをマージできます。
エージェントとツールの接続のためのModel Context Protocol
ハーネスとツールを接続する標準としてのMCP
- [45:35] エージェントハーネスとツールを接続するための、Model Context Protocolと呼ばれる標準化されたプロトコルが存在します。
- [45:43] これらの接続には、さまざまなオープンソースの選択肢や、いくつかのホスト型の選択肢が利用できます。
例:コーディングエージェントをNotionに接続する
コーディングエージェントにおけるコンテキスト管理機能
限られたコンテキストウィンドウと汚染されたコンテキスト
- [46:29] 基盤となるAIモデルには限られたコンテキストウィンドウがあり、多くの場合、入力100,000トークンのような固定の上限があります。
- [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 のようなURLを取得し、その内容をコンテキストウィンドウに取り込んで活用できます。
プレーンテキスト標準としてのLLMs.txt
- [51:27] llms.txt は、LLM向けに情報を整形するための提案された標準です。
- [51:34] 生のHTMLを取得すると、モデルにはスタイルや分析情報などタスクに関係のない冗長な内容が渡されます。
- [51:59] LLMs.txt は推論時にLLMが読むことを想定したプレーンテキストファイルの標準であり、多くのライブラリ、特にAI関連や先進的なものが公開しています。
Pydantic AIの例とコンテキストの節約
agents.md を使ってコーディングエージェントにリポジトリのコンテキストを与える
agents.md の目的と設定
- [52:47] agents.md という標準的なファイルをGitリポジトリやプロジェクトに置くと、コーディングエージェントは起動するたびにその内容全体を読み込みます。
- [53:02] Claudeの場合、このファイルは claude.md と呼ばれ、例は講義ノートやクラスのリポジトリで入手できます。
空のコンテキストで同じ指示を繰り返さないこと
- [53:18] このファイルは、コーディングエージェントが完全に空のコンテキストで始まり、リポジトリについて何も知らない場合に役立ちます。
- [53:31] 型チェッカーを実行するコマンドやユニットテストを実行するコマンドなどの慣行を記録できるため、エージェントが起動するたびにこの情報を提供する必要がありません。
再利用可能なプロンプトと長期的なリポジトリ
オンデマンドのコンテキスト管理としてのスキル
大きなagent.mdファイルの限界
- [54:09] スキルはagent.mdファイルの後継として登場しました。agent.mdファイルは、コーディングエージェントが起動時にプロジェクトのあらゆる情報を把握できるようにするものです。
- [54:24] 非常に大きなAgents.mdは、会話が始まる前に基盤となるLLMのコンテキストウィンドウの大部分を消費してしまいます。
- [54:36] Agent.mdファイルは多くのトピックを網羅していましたが、コーディングエージェントがそのすべての情報を常にコンテキスト内に必要とするわけではありません。
オンデマンドのスキルファイルを介した間接参照
コーディングエージェントにおけるコンテキスト管理のためのサブエージェント
コンテキスト管理の仕組みとしてのサブエージェント
- [55:42] サブエージェントは一部コンテキスト管理と関係しています。コーディングエージェントは特定されたサブタスクのために別のエージェントを起動し、そのサブタスクだけのプロンプトを与え、完了したら制御を取り戻すことができます。
- [56:02] サブエージェントが親エージェントに制御を返すため、親のコンテキストはサブエージェントが行ったことの詳細すべてで汚染されません。
- [56:12] これらのコーディングエージェント用に独自のサブエージェントを作成できますし、エージェント自体のコンポーネントを実装するために使われることもあります。
サブエージェントとして実装されたWebFetch
- [56:21] 一部のコーディングエージェントはWebFetchツールをサブエージェントとして実装しています。ウェブページのHTML全体をコンテキストウィンドウに読み込むのは最善のアイデアとは限らないからです。
- [56:38] 別のエージェントがウェブサイトを取得して関連する側面を要約できるため、フィルタリングされた関連情報だけが親エージェントのコンテキストに入り、コンテキストウィンドウをより小さく保てます。
速度のための並列サブエージェント
- [56:54] これらのエージェントは一般に並列ツール呼び出しと並列サブエージェントをサポートしており、複数のタスクを同時に実行できます。
- [57:02] たとえば、強力なコードレビューのプロンプトを持つサブエージェントと、型チェックの問題を修正するための強力なプロンプトを持つ別のサブエージェントを、ある機能に対して並列に起動できます。
- [57:15] Claude Codeはこれを実行できます。両方のサブエージェントが同時に作業し、両方が完了すると親エージェントが続行するため、作業を加速することもできます。
ノートと演習を通じて概念を学ぶ
圧縮と新しいセッションのどちらを選ぶか
同じ流れを続けるための圧縮という技術
- [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 はデバッグの悪循環にはまり、コードを修正してテストを実行するたびに失敗し、コードをどんどん悪化させることがあるため、注意深く見守る必要があります。
コンピュータサイエンスとプログラミングスキルの継続的な価値
冒頭 3 段落の二言語対訳プレビュー · 利用枠の消費なし
保存済みの日本語訳と英語原文の冒頭 3 段落を並べて確認できます(利用枠は消費しません):
それでは、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.
この講義の冒頭で、これらの仕組みのデモをお見せし、その後で内部でどのように動作するか、基本、そしてより高度な話題について話します。これが今日の計画です。最初のデモでは、開発環境とツールの講義で行っていた例を実際に続けます。思い出してください、画面上でコードを出します。この小さなスクリプトに取り組んでいたのを思い出してください…
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...
ああ、思った通りになっていません。ちょっと待ってください、少し準備が必要です。では、コースのウェブサイトからページをダウンロードし、そこからすべてのリンクを抽出するこのスクリプトに取り組んでいたのを思い出してください。今ここにいます。とてもシンプルな小さな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モデルがファイルの読み書きやコンピュータ上でのシェルコマンドの実行などもできる点が異なります。
[2:50] そして時には、これらのモデルはソフトウェアを書くのが特に得意になるように特別に調整されています。このディレクトリにdl.pyというスクリプトがあり、コーディングエージェントに自然言語で入力して、例えば「私が書いたスクリプトを…」ここでわざとタイプミスをしておきます、そのままにしておきます。「私が書いたスクリプトdl.pyを、きちんとしたコマンドラインプログラムにして、引数の解析にはargparseを使ってください。それから、ここでいくつか欠けているものなど、他の点でももっと良くしたいかもしれません。ああ、ここには型アノテーションがあると思いますが、例えば『適切な型アノテーションがあることを確認し、mypyを使って動作するかチェックしてください』と言えます。
[3:36] ですから、型アノテーションをチェックし、型チェックに通ることを確認したいのです。このようなコマンドを与えると、考え始めます。そしてその出力を説明しましょう。ChatGPTがテキストを返せるように、テキストの出力を返します。しかし、ツールを呼び出すこともできます。このモデルはdl.pyファイルを読むことにしました。それはこのタスクを始めるのに合理的な方法です。そしてこれはかなり簡単なタスクです。ですから、このdl.pyを更新し、一発で全てのコードを書いてしまいます。
[4:09] ですから、ここに入ってどのような変更が加えられたか見ることができます。ご覧のとおり、いくつかの関数が追加されています。そして今、コマンドライン引数の解析などを行うこのmain関数があります。つまり、私が依頼したタスク全体をやり遂げたのです。そして私の指示に従い続けています。型アノテーションがあること、型チェッカーがすべて問題ないと言うことを確認するように伝えたのを思い出してください。そしてここで次のコマンドを呼び出そうとしています。シェルを使ってmypyを呼び出し、スクリプトへのパスを引数として渡しています。
[4:41] ここで気づいたことの一つは、ファイルを編集する際に許可を求められなかったことです。そのまま編集を進めました。これは、私のコーディングエージェントがこのように設定されているからです。というのも、ファイルへの細かな変更のたびに許可を求められると、少々煩わしいと感じるからです。もっと自律的に動作してほしいのです。そして一般的に、私はすべてをGitで管理しています。なので、コーディングエージェントにまとめて多くの変更をさせておき、後からまとめてレビューできます。しかし、bashなどコーディングエージェントが呼び出したい他のツールについては、実行される一つ一つのコマンドを実際に確認し、安全であることを確かめたいのです。
[5:14] 今日はエージェントのセキュリティの詳細には立ち入りませんが、これらを完全に自律的に実行させると、いくつかのセキュリティ上の懸念があります。そして、呼び出してほしくないコマンドを呼び出したりするかもしれません。大切なファイルを削除すると言い出すかもしれませんし、それよりもはるかに危険なことをするかもしれません。少なくとも個人的には、コンピュータの状態を変更しうる個々のツール呼び出しを承認しなければならないこのモードで実行することをお勧めします。さて、私はそのツール呼び出しを承認しました。エージェントのハーネスがツールを実行しました。その結果がAIモデルに返されました。モデルはすべて問題ないと述べました。そして、そのまま続行しようと決め、スクリプトを実行して問題なく動作しているか確認しようとしています。
[5:50] それで、ここでさらにいくつかの処理を行っています。そして、はい、できました。私が依頼したタスクを完了しました。これがAIの…ええ、質問ですか?ええ、質問は、これはバックグラウンドで何らかのホスト型AIサービスを使っているということですね。使うお金を制限できますか?というものです。答えは、ええ、はい、できます。正確な設定項目はツールによって異なります。
[6:22] 一般的に、使用するAIモデルを選択でき、より難しいタスクの解決に優れているかもしれない高性能なモデルほど高価になります。そして、より小さく高速なモデルほど安価になります。これが一つの制御手段です。また、一部のサービスでは実際に支出を多少制限できます。たとえば、今月は最大で10ドルまで使いたい、と言えます。ええ。つまり、質問は、支出を制限するのか、ということですね。少なくとも個人的には、制限していません。使用状況には目を配っています。たとえばClaude Codeでは、スラッシュコストのコマンドがあります。
[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] そしてこれらのモデルを使うとき、これを分布としてモデル化できるので、プロンプトxが与えられたときの補完yのパラメータ化された分布として、πθと書くことにします。そしてこれらを使うときは、この分布からサンプリングします。入力が与えられたときに、このπθから文字列yハットをサンプリングできます。それだけです。AI言語モデルとはそれだけのことなのです。
[14:11] また、何か役に立つことをする良い確率分布を見つけるには、多くの作業が必要です。しかし実際には、その使い方は、テキストや文字列、トークンをまとめて入力として与えるというものです。すると、テキストをまとめて出力として返してきます。そして、それ以外のすべては、この中核的なプリミティブの上に構築されます。ここで注意すべきもう一つの詳細は、実際にはこれらのものには限られたコンテキストウィンドウがあるということです。つまり基本的に、この文字列の長さと、この文字列の長さには上限があるということです。
[14:51] では、その上に構築していくと... ChatGPTのようなものはどう動くのでしょうか。会話形式のチャットはどう動くのでしょうか。単一ターンのチャットにこれをどう使えるかは、おそらく想像できると思います。ここで質問を与えると、ここで答えが返ってきます。では、フォローアップの質問はどうするのでしょうか。何かアイデアはありますか。マルチターンのチャットをしたい場合です。これらのものの仕組みは、ターンマーカーを使い、会話履歴全体をモデルへの入力として与えると、モデルが次のもの、つまりモデルの出力を返すというものです。
[15:33] 確率分布からの出力として。たとえば、入力として、ユーザーがこう言ったとします。何だろう、黒板に長い質問は書けませんが、一足す一は何ですか、といった具合です。するとアシスタントはこう言うかもしれません。最初のクエリでは、確率分布からサンプリングするとき、これが最初のXで、そしてこれが最初のYハットです。そして、フォローアップの質問をしたい場合、ここでは意図的に依存するクエリを書いています。
[16:10] たとえば、一を二に置き換えたらどうなるでしょうか。つまり、このクエリ単体ではあまり意味がありません。これを正しく答えるにはコンテキストが必要です。そこで、会話形式のチャットアプリがこのようなものを扱う方法は、再び大規模言語モデルからサンプリングすることです。おっと。この全体をプロンプトとして、これらのものをターンマーカーとして特別にエンコードして与えるのです。
[16:43] つまり、ユーザーがこう言い、言語モデルの出力がこうで、今度はユーザーがこう言ったとすると、それが次のものを生成します。これが次の出力になります。さて、これで大規模言語モデルという、確率分布と考えられるプリミティブから、マルチターンのチャットにどう使うかまで来ました。次は、そのアイデアの上に構築して、エージェントハーネスのようなものをどう作るかについて話します。
[17:18] ここまでで何か質問はありますか。いいですね。では、さらに追加できるアイデアがもう一つあります。ここではターンマーカーを追加しました。今度は、LLMからの出力に特別な解釈を加えることができます。そして、モデルはアシスタントとそれに続くテキストのようなものを出力できると言えます。しかし、ツールを呼び出す、といった出力も可能です。
[17:52] そこで、マシン上で何かを行うためのリクエストを出力できるように言語モデルを訓練します。例えば…その一例は、ここで挙げたようなものです。ツール呼び出しの例としては、例えばmypy、dl.pyを実行するリクエストなどがあります。そして、会話型チャットアプリでメッセージ履歴全体を連結してフォローアップ入力を構築していたのと同様に、言語モデルをラップするコードを用意します。エージェントハーネス(スキャフォールディングとも呼ばれます)がこれらの出力を解釈します。
[18:43] これはLLMからのyハットです。これがこれらの出力を解釈します。実際にマシン上でそれらを実行し、その結果を次の入力として言語モデルにフィードバックします。こうして処理が行われます。そして、mypyツールからのテキスト出力があるかもしれません。例えば「dl.pyをチェック中」などと表示されます。あるいは「エラー 行一、int型を期待」などと表示されるかもしれません。この出力全体を取得し、それをそのままLLMへの別の入力として挿入します。
[19:16] これが、LLMにフィードするXの後続の構成要素となります。はい、質問は? 質問は、エージェントハーネスとは正確には何かということです。それはいくつかのことを行います。LLMへの呼び出しをループで実行し、出力を受け取るコードの部分を実装します。
[19:48] ツール呼び出しがあれば、実際にそのツール呼び出しをディスパッチします。実際にマシン上でこれを実行します。このツールが生成するテキストを取得し、言語モデルにフィードバックして、別の出力を生成させます。そして…はい、これらが主な二つの機能です。大まかに言うと、エージェントハーネスはユーザー入力を受け取り、forループ内でエージェントを呼び出します。ツール呼び出しを要求された場合は、ツールを実行し、入力を言語モデルにフィードバックして、別の補完を生成させます。
[20:19] さらに別のツール呼び出しであれば、ツールを実行し、結果を言語モデルにフィードバックして別の補完を生成させ、これを言語モデルがユーザー向けのテキスト出力を出すまで続けます。その時点で…「はい、完了しました」と言って制御がユーザーに戻り、その後処理を進められます。そして、コーディングエージェントのツールにあるようなツール実装、例えばbashがあり、エージェントにbashスクリプトを実行させられます。ファイル編集、ファイル読み書きなどのツールもあり、これらのツールの組み合わせによって、基盤となるAIモデルが実際にマシン上の物事を変更し、ある程度知的な方法でコードを開発できるようになります。
[21:05] テストを実行したり、テストを実行したときに何が起こるかを確認したり、それを踏まえて将来どのような行動を取るかを決めたりできることです。ああ、後ろの方で質問がありますか?はい。これらが主なプリミティブです。中心には文字列やトークン上の確率分布をモデル化する言語モデルがあります。そして、このエージェントハーネスがループでLLMを呼び出し、繰り返しサンプリングを行い、さらにツール実行のリクエストをディスパッチして実際に実行し、その結果を言語モデルにフィードバックして物事を進めます。
[21:41] 通常、クエリを入力すると、たくさんのツールが実行され、内部的にはこの言語モデルから何度も何度もサンプリングしています。はい、質問ですか?はい、質問は、エージェントハーネスと基盤となる言語モデルの間に相互運用できるようによく理解されたインターフェースはあるのか、ということです。はい、よく使われるインターフェースはいくつかあります。完全に標準化されて全員が同じものを使っているわけではありませんが、最近はかなりの相互運用性があります。
[22:17] 例えば、OpenCodeのようなオープンソースのエージェントハーネスがあり、それらをさまざまな基盤となる言語モデルと組み合わせて使えます。Anthropicのモデルとも、OpenAIのモデルとも、その他とも連携できます。はい、質問。はい、つまり言語モデルはこの確率的なもので、エージェントハーネスはおそらくハーネス自体に書くコードが決定論的である、これらはどう関係するのか、という指摘ですね?
[22:57] それらはサンプリングを通じて関係します。エージェントハーネスがLLMを呼び出すとき、出力文字列をサンプリングし、それに基づいて行動します。言語モデルを呼び出すと、「このコマンドを実行せよ」のようなものを出力し、そのコマンドを実行して続行します。非決定性に関して言えば、この言語モデルは賢い方法で訓練されていますが、人間と同じようには知的ではありません。時には完全には正しくないものを出力することがあります。すると、コンピュータに実際には存在しないコマンドを呼び出したり、ファイル読み書きツールを使って誤ったコードを書いたりすることがあります。
[23:30] しかし実際には、こうしたものはかなりうまく機能する傾向があります。使っていくうちに、何が得意で、何がまだ十分にできないのかについて直感が身につきます。はい。もうやりましたね。はい。これでこれらの仕組みについて話したので、この例に戻って、内部でどのように動いているかという観点からもう一度理解することができます。
[24:01] ここでエージェントハーネスを起動しました。この場合は、Anthropic が開発した Claude Code エージェントハーネスを使っています。内部的には、Anthropic の Opus 4.5 大規模言語モデルを使用しています。ここで最初のクエリを渡しました。これが右上の最初のユーザークエリです。モデルはここでいくつかの中間テキストを出力し、その後ツールを呼び出すことにしました。この LLM は read file ツールを引数 dl.py で呼び出しました。それがエージェントハーネスに渡されました。
[24:32] ハーネスはその言語モデルの出力を、ああ、言語モデルがツールを呼び出そうとしているのだと解釈しました。実際に私のファイルシステム上でファイル読み取り操作を実行し、そのファイルの内容に対応するデータを生成し、それを追加の入力として言語モデルに戻しました。ですので、右上の例を思い浮かべてください。これはその X にどんどんものを追加しているのです。最初に始めたこの X があり、最新のメッセージや LM に渡すデータに至るまで、この会話履歴にどんどん追加していきます。
[25:06] この例では。つまり、ここで LLM に最後に送られるのはそのファイルの内容です。そしてモデルは、推論を実行し、モデルからサンプリングし、今度はモデルがさらにテキストを出力することにしました。何が起きているかの更新を私に伝えています。そして別のツールを呼び出すリクエストも出力しています。今回は file write ツールです。この特定のエージェントハーネスでは、そしてこれはかなり一般的に行われていると思いますが、file write ツールにはいくつか異なるインターフェースがあり、モデルは実際にファイルに適用される diff を提供できます。
[25:38] 繰り返しますが、モデルは diff を出力します。これは単なるテキストの塊で、その後エージェントハーネス、この場合は Claude Code がその diff を解釈し、私のファイルシステム上のファイルに適用します。そしてそれが完了し、さらに中間テキストを出力しました。そう、このファイル更新操作には戻り値があり、それは操作が成功したというようなもので、それがここでモデルに返され、別の推論パスを実行し、モデルは別のツールを呼び出すことにしました。今回は mypy、つまり型チェッカーを呼び出しています。そしてエージェントハーネスがそれを解釈し、型チェッカーを実行し、ツールからの出力を生成します。今回は成功、問題なしという結果で、それがモデルに戻され、別の LLM 推論パスを実行し、といった具合です。そして最終的に「自分のターンは終わりだ、これが最終テキスト出力だ」と出力する前に、あと一つツール呼び出しがあります。
[26:27]、そして制御はユーザーに戻ります。つまり、一つのユーザープロンプトに対して、ここで大量のLLM推論ステップが行われ、ここで大量のツール呼び出しが行われているわけです。いいですね。では、これらの仕組みの内部動作について話しているついでに、プライバシーについて簡単に触れておきます。先ほど少し話したように、こうした複雑なタスクをより得意とする高性能な言語モデルは、推論を実行する、つまりその確率分布からサンプリングするために、かなり強力なハードウェアが必要です。
[27:02] そのため、これらのものはほとんどがクラウドで動作します。そして、これらを使うときは、プロバイダーと特別な契約でも結んでいない限り、あなたのデータを彼らに送信していることになります。彼らはそのデータでトレーニングするかもしれませんし、そういったこともあります。ですから、プライバシーが気になる場合は、利用規約の詳細を確認するか、自分に合った設定を探してください。では、この講義の残りの時間を使って、ユースケースについて少し話しましょう。そして、私は…そうですね、質問がありますね。はい、質問は、ClaudeはLLMとエージェントハーネスの両方なのか、ということですね。答えは、いくつかの異なる構成があります。
[27:36] この場合、エージェントハーネスの一構成要素は、モデルとともにクラウドで動作していると考えることができます。モデルは間違いなくここではクラウドで動作しています。完全にローカルでハーネスが動作するオープンソースのエージェントハーネスも見つかりますが、すべてのLLM推論ステップは推論のためにクラウドにデータを送信することになります。はい、いいですね。では、コーディングエージェントを使って新機能を実装する方法の例を一つすでに挙げました。これらの指示の出し方はかなり具体的にすることもできますし、オープンエンドにしてそこから反復していくこともできます。
[28:07] 例えば、クラスウェブサイト、Missing SemesterのウェブサイトのGitリポジトリに入って、Claudeを起動し、「レトロな見た目にして」のように言うことができます。どうなるか見てみましょう。これには一、二分かかるでしょう。なので、これには後で戻ってきます。コーディングエージェントのもう一つの優れたユースケースは、コードの問題を修正するために使うことです。コンパイラの問題、つまりコードがコンパイルされない場合や、リンターの問題がある場合、リンターについては金曜日にもっと詳しく扱いますが、コード内のさまざまな種類のエラーをチェックするツールがあってそれが文句を言っているなら、コーディングエージェントにその問題を修正するよう頼むことができます。
[28:42] また、単体テストがある場合や、コードにバグがある場合に、そのバグを少なくとも最初に解決しようとする素晴らしい方法の一つは、そのバグに対するテストを構築することです。これは一般的に優れたソフトウェアエンジニアリングの実践です。コードに何か問題があれば、テストを書いてください。そうすれば後でそれを回帰テストとして使い、将来問題が起きないことを確認できます。そしてテストができたら、コーディングエージェントに「このテストが失敗しているので、問題が何かを突き止めてテストを通るようにして」と伝えられます。これを簡単にデモするのは少し難しいです。なぜなら、実際のコードベースで作業したいのですが、そのコードベースについてのコンテキストが皆さんにはないからです。
[29:15] でもやりますので、ここでの細かい詳細は心配しないでください。ただし、物事がどのように機能しているかを高いレベルでお見せしようと思います。では、ここにコードベースがあります。細かい詳細は気にしないでください。そして、このコードに問題があることに気づきました。この特定のケースでは、その問題に対して単体テストを手書きしました。このテストを実行すると、テストが失敗することがわかります。コーディングエージェントを使って、テストを書く最初の試みをさせることもできます。
[29:46] 例えば、エージェントを起動して「このようにコードを使うと物事がうまくいかないのですが、それに対するテストを書いてもらえますか?」と言えます。しかし、満足のいくテストができたら、コーディングエージェントを起動できます。そして、このコマンドをコピー&ペーストします。コーディングエージェントを起動して、「バグがあります。それに対するテストを書きました。このコマンドで実行すると問題を再現できます。バグを修正してください」のように言えます。
[30:17] そして、この場合に人間の開発者がするのと同じように、まずテストを実行してバグを再現します。エージェントがやっているのはまさにそれです。つまり、私が渡したこのものでbashを呼び出し、私が書いたその特定のテストを実行しています。そして出力を見て、「はい、確かに失敗しています」と確認します。その出力自体はあまり情報量が多くありません。それ自体だけではバグを修正するのに十分ではありません。実際にコードを読んで理解する必要があります。そこでエージェントが次にやるのがそれです。これらのものは、コードベースを巡回してコードを理解するのが実際かなり得意です。AIモデルにとって理解するということが何を意味するにせよ、ですが。
[30:50] ですので、ファイルを読み込んでいるのが分かります。ここに、それが持つ別のツールがあります。正規表現パターンで、そのパターンに一致するさまざまなものを検索できる検索ツールがあります。さらにファイルを読み、検索し、読み、検索し、読み、考えています。そして、この特定のケースでは、バグは十分に単純なので、一発でバグを修正できるはずです。これらのコーディングエージェントのデモを少し難しくしているもう一つの点は、それらがあまり速くないことです。そしてこれは多くの人が取り組んでいることです。つまり、本当に優れていて効果的なエージェントをどう作るか、しかもより速く動くようにするか、ということです。
[31:25] ここで見ているように、ああ、これが動き始めてまるまる一分が経ちました。でもまあ、デモを少し難しくしますね。ライブデモをしたかったので、この動作を早送りすることはできません。でも、今モデルが言っています、ああ、バグはこれだと思う、と。ここで、こういうふうに修正しよう、と。そしてその後、自分でテストを再び実行して、本当にバグを修正できたかを確かめようとします。ですので、そのツール実行のリクエストを承認できます。そして、ああ、テストが今通ったと分かります。そしてここで、モデルは他の賢いことをしています。一般に、コードのバグを修正するときは、そのバグを捉えるために書いたテストが本当に通ることを確認したいものです。
[32:00] しかしその後、残りのテストを実行して、コードベースの他の何かを壊していないことを確認すべきです。ですので、エージェントはそういった他の有用なことをしに行きます。そして、ああ、今エージェントは考えて作業するのを終え、テストを実行するための変更を行い、何をしたかの要約を私に与えています。さらに、変更をコミットして、のように言うこともでき、そうすると有用なコミットメッセージなどとともに git コミットを作成してくれます。ええ、これらのものは実際かなりよく訓練されているのが分かります。
[32:31] コミットメッセージをでっち上げるだけではいけないことを学んでいます。リポジトリのスタイルに従うべきです。ですので、実際に見ています、ああ、git status を見て、何がコミットのステージにあり、何が変更されたかを確認しています。git diff を見て、変更が自分がしたことであって、過去に私がした他の変更ではないことを確認しています。そして git log を見て、最近のコミットメッセージがどんな感じか、スタイルに合わせられるように確認しています。そしてこのコミットメッセージを書きますが、実際かなり妥当に見え、ああ、これはこの GitHub イシューを修正する、などと言っています。後ろからの質問。
[33:22] ええ、では質問は、コーディングエージェントに与えたプロンプトと基盤となるLLMが、コードの品質、コードの冗長さ、タスクの完了にかかる時間などに影響するのか、ということですね。答えは間違いなく、はいです。現時点では、LLMにうまくプロンプトを与えるのは科学というよりは少し芸術に近い面があります。また、基盤となるモデルは絶えず改善されています。ですから、今は誰もほとんど使っていない二年前のモデルを使った場合、とても古いモデルを使えば、こうしたタスクを完了させることはまだできるかもしれませんが、その方法をとても詳細に説明する必要があるでしょう。そして今では、バグがある、直して、と言うだけで、うまくやってのけます。
[33:55] でもええ、たくさんのリソースがあると思います。ノートにいくつかリンクを載せていますし、さらに追加することもできます。さまざまな種類のタスクをこなすためにこれらのツールに効果的にプロンプトを与える方法についてのリソースがたくさんあります。高度な機能を実装させるにはどうすればよいか。人々が従うさまざまなパターンがあります。今回のようなちょっとしたこと、つまり自分のものをレトロな見た目にしたい、といった場合は、レトロな見た目にしてとだけ言えば、やってのけてくれます。しかし複雑で本格的な機能の場合は、何段落にもわたる非常に長いプロンプトになるかもしれません。あるいは、業界で人々が行っているようなソフトウェア開発の慣行に近いやり方に従いたいかもしれません。
[34:26] プロダクトデザイナーの帽子をかぶり、プロダクトマネージャーの帽子をかぶって、PRD、つまりプロダクト要求仕様書を丸ごと書きたくなるかもしれません。そして巨大なマークダウン文書をコーディングエージェントに渡して、はい、これがPRDです。読んで理解して、実装計画を立ててから実装してください、と言うことができます。このように、より高度なことを行うために従えるさまざまな上級パターンがあります。ええ、質問ですね。[聞き取れない質問] ええ、では質問は、コーディングエージェントをあるディレクトリで起動した場合、そのディレクトリとサブディレクトリ内のすべてが見えるのか、ということですね。
[35:03] おおよそ、はい。これらのエージェントの中には、ディレクトリの外にあるものを見せてほしいと要求できるものもあります。この分野はかなり新しいです。世の中にはたくさんの異なるツールがあります。そして、こうした詳細の一部はツールによって異なります。でもええ、基本的には、はい、通常はGitリポジトリのレベルで操作することになるでしょう。いいですね。では、失敗するテストが与えられた状態でバグを修正する、というエンドツーエンドの例を見てきました。おまけに、まだ考え中ですが、講座のウェブサイトをレトロな見た目にする様子も見てみましょう。
[35:36] わかりました。ええ、その変更をコミットしてプッシュするのはやめておきましょう。でも、こういったものは実は創作作業にもかなり役立つんですよ。今日の講義ノートを読んだなら、その一部はこの技術をドッグフーディングするためでもあって、私たちはコースのウェブサイトに関連するいくつかの変更にClaude Codeを使いました。この講義を以前の開発環境の講義から切り出して独立した講義にする際に、リファクタリングが必要でした。それをClaude Codeに頼んだんです。なかなか良い仕事をしてくれましたが、完璧ではありませんでした。リンクされているGitHubのプルリクエストを見れば、エージェントが何をしたのか、その後私が手動で何を修正しなければならなかったのかがわかります。
[36:11] すべての演習問題を引き継げたわけではなく、適切な場所に配置できたわけでもありませんでした。いくつかは消えてしまい、復元しなければなりませんでした。また、こんなものも実装しました…ちょっと待って、これらの変更を消しましょう。TufteスタイルのサイドノートなどもClaude Codeを使って実装しました。そう、Jonはそのコードを一行も自分で書いていないと思います。すべてClaude Codeとのバイブコーディングのようなものでした。ええ、こういった創作作業では、高レベルな説明とともにエージェントに何かをやらせて、結果を見て、微調整のためのフィードバックを与えるというのが、実はとても効果的なんです。
[36:44] 最近こういったエージェントを使ってフロントエンドのデザインやウェブデザインなどをしている人をたくさん知っていますからね。いいですね。ではユースケースについて話していますが、残り時間は10分ほどだと思います。なので、ノートにあるすべてのユースケースをデモするつもりはありません。また、ノートに書いた以上のユースケースを皆さんならもっとたくさん思いつくことでしょう。でも簡単に説明していきます。そして講義の最後の部分は、エージェントに関連するより高度なトピックに使いたいと思います。コーディングエージェントで非常に役立つことの一つに、リファクタリングなどがあります。
[37:19] 先ほども言ったように、私たちは講義ノートのリファクタリングに実際にClaude Codeを使いましたが、こういったツールはコードのリファクタリングにも使えます。これらのエージェントはコードレビューが本当に得意です。誰かがGitHubのプルリクエストを送ってきた場合でも、自分自身のために使いたい場合でも、エージェントに「このコードをレビューして」と頼むだけでいいんです。その程度の指示だけでも、有用なフィードバックを得るには十分です。より具体的な指示を与えれば、よりカスタマイズされた回答が得られます。私がこれらのツールをよく使ってきたことの一つで、実際に非常に微妙なバグをいくつか見つけるのにも使ったのが、パフォーマンス最適化やその他のリファクタリングを行った際に、変更の前後でコードが意味的に等価であることを確認したいような状況です。
[37:56] でもそう、私はスタイル上の変更やパフォーマンスの改善などをしていました。コーディングエージェントはコードについての推論やコード間の差分についての推論がかなり得意です。そして、こう指摘してくれました。「ああ、あなたはこの変更をしましたね。そして、この特定のケースでは、動作が実際に異なります」と。それでそのバグを直しに行けました。コードレビューにとても役立ちます。これらのエージェントはコードベースの理解にも優れています。ですから、ええと…ここにいる聴衆のかなりの部分がコンピュータサイエンスを始めたばかりだと思います。今はおそらく小さめのリポジトリで作業しているでしょう。でも、GitHubのオープンソースプロジェクトに貢献したいとか、より大きなコードベースに飛び込みたい、例えばUROPをやっていて研究室のコードベースで作業しているなら、これらのエージェントはコードベースをナビゲートし、理解し、あなたに説明するのが本当に得意です。
[38:36] ですから、まっさらなコードベースに入って「このコードベースを説明して」と言うこともできます。あるいは特定のものを探しているなら、例えばUROPを始めて、UROPのメンターに「この機能に取り組んでみて」と言われたとします。どのファイルを変更すべきか知るだけでも、多くの思考と理解が必要ですよね。でもこれらのコーディングエージェントは、その手助けをするのがかなり得意です。もう一つ簡単にデモします。これは以前の講義のいくつかにつなげる話です。これらのコーディングエージェントはツールを呼び出せるので、ある意味シェルのように使えます。それで私は最近これを使っています。シェルコマンドやそのフラグなどを全部覚えるのは大変ですからね。
[39:11] その代わりに、自然言語で説明するか、コーディングエージェントにどうしたいかを伝えるだけでいいのです。例えば、このディレクトリ内で名前変更インポートを含むPythonファイルをすべて見つけるコマンドを使いたいとします。そうしたら「agを使って」と言えます。これはコマンドラインツールで、名前変更インポートを行うPythonファイルをすべて見つけます。これはPythonでいう`from something import something as something`、あるいは単に`import something as something`のような、私が探している特定のパターンなのですが、これを自分で打ち込むのはずっと大変です。agにこの`--python`フラグがあることを覚えていたり、正規表現を正確にどう書くかなどを覚えていたりするのは大変です。ですから、コーディングエージェントに話しかけて、シェルのように振る舞わせるだけでいいのです。
[39:54] そして「libディレクトリを無視して」のような追加指示を出すこともできます。これは自分が書いたコードとこのライブラリディレクトリ内のコードの両方を検索している状態です。そこで、自分が書いたコードだけを検索したいかもしれません。そのような追加指示を出せば、適切なシェルコマンドを生成し、実行し、さらに結果を要約してくれます。これはかなり素晴らしいユースケースだと思います。はい、質問は? ええ、質問は、学校の課題でこれらのツールを使うと問題になるのかということですね。担当の講師に確認して、これらのツールを使ってもよいか聞いてみてください。クラスによって異なると思います。
[40:25] 中には、AIコーディングツールの使用を一切禁止しているクラスもあります。つまり、AIオートコンプリートもインラインチャットもなし。前回の講義で話したようなより単純な機能さえも使えません。そしてもちろんコーディングエージェントもなしです。一方で、好きなツールを使ってよいというクラスもあります。ええ。ですからルールに従ってください。宿題でカンニングをしないように。また、AnthropicやOpenAIなどの企業の中には、学校の課題向けの特別な機能やモードをソフトウェアに搭載しているところもあると思います。
[40:57] そうしたモードでは、答えを教えるのではなく、ティーチングアシスタントのように振る舞ってくれます。あるいは、自分の理解を優先したいなら、ツールにヒントを求めることもできます。たとえそのための特別なモードがなくても、「コードにバグがあるけれど、バグが何かは教えずに、正しい方向を示して」と頼むことができます。そういう使い方にはまあまあ使えます。つまり、ティーチングアシスタントの代わりとして使えるわけです。いいですね。そして最後に、これは詳しくは話しませんが、コーディングエージェントはいわゆるバイブコーディングに使えます。これらのツールは非常に強力で、英語で言語モデルとチャットするだけで、コードを一文字も自分で書かずにプロジェクト全体を最初から最後まで実装することも実際にできます。
[41:37] サイトをレトロな見た目にするというのは、その小さな例だと思います。では、コーディングエージェントのこうしたユースケースやバイブコーディングについて質問はありますか? いいですね。では次に、これらのエージェントのより高度な機能について少し話しましょう。そして、これらの仕組みの内部でどう動いているかについて以前話したことにも一部つなげていきます。では、黒板に書いてみましょう。
[42:13] これらのソフトウェアのほとんどが備えているいくつかの機能です。高度な機能の中で最も単純なものから始めます。よくあることとして、同じコマンドをこれらのエージェントに何度も入力してしまうことがあります。たとえば、特定のプロジェクトに取り組んでいて、プロジェクトに送られてくるプルリクエストのコードレビューをエージェントに特定の方法でやってほしいとします。
[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 worktreeはとても相性が良いのです。以前に誰かが質問していた別のこともあると思いますが、エージェントハーネスとLLMを接続する標準化されたプロトコルはあるのでしょうか?それに関連して、Model Context Protocolという標準化されたプロトコルがあり、これはエージェントハーネスをツールと接続するためのものです。
[45:43] さまざまなオープンソースのものや、ホスト型のものもあり、たとえばコーディングエージェントをNotionに接続できます。そうすれば、Claudeに話しかけて、NotionドキュメントのURLを渡し、そこに記入したり、中の内容を読んだり、そこに書いた実装計画を改善したり、そしてコードベースで機能を実装したりできます。たとえばNotion MCPコネクタを使えば、Notionの読み書きができます。これはコーディングエージェントハーネスを他のツールに接続する一つの方法にすぎません。そして、たくさんの機能があります。
[46:25] コーディングエージェントにおけるコンテキスト管理に関連する機能です。基盤となるAIモデルには限られたコンテキストウィンドウがあると述べたことを思い出してください。一般に、モデルには固定のウィンドウがあります。たとえば、このモデルは最大100,000トークンの入力しかサポートしない、といった具合です。そして実際には、たとえ技術的にはコンテキストウィンドウ内であっても、大量の入力をモデルに与えると、そのすべての入力を処理するのはモデルにとって難しいタスクになります。その結果、汚染されたコンテキスト、つまり…汚染されたコンテキストのようなものが生じます。
[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 を取得して授業スケジュールの要約を出して、と頼むことができます。つまり、使いたいライブラリの URL はこれです、ライブラリのドキュメントを読んで、xyz を行うコードを書いてください、と頼めるのです。
[51:15] そしてウェブサイトを取得します。このコーディングエージェントが持つツールの中にウェブ取得ツールがあり、それによってその内容をコンテキストウィンドウに取り込み、その情報に基づいて処理できるようになります。そこで llms.txt が登場するのですが、llms.txt は LLM 向けに情報を整形するための提案された標準です。通常、これらのコーディングエージェントがウェブサイトから何かを取得するとき、これはいくつもの方法で設定できますが、生の HTML を取得してそのすべてをエージェントと基盤となる LLM に渡すと、HTML はかなり冗長で、モデルにとってあまり有用でない情報がたくさん含まれています。
[51:50] この場合、すべてのスタイリング情報やアナリティクスなどは、このモデルがやろうとしていることには関係ありません。そこで 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があると、基盤となるLLMと会話を始める前に、コンテキストウィンドウの大部分を埋めてしまいます。そして人々は、これらの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について少しは神秘性が解けたのではないかと思います。LLMは、たくさんの入力トークンが与えられると、次に来るものの条件付き確率を返す、ただの確率的モデルです。
[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: エージェント型コーディング」by 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を推奨するものでもありません。VidleafはAIによる句読点の付加とAI生成の要約を追加しましたが、元の文言はこのページで引き続き確認できます。
あなたは制作者または権利者ですか?訂正や削除をご希望の場合はこちら:copyright@vidleaf.app(このページについてをご覧ください)。
最終更新