サブエージェント
Simon Willison氏は、LLMのコンテキスト制限を克服するための「サブエージェント」パターンを紹介し、Claude Codeの実装例を通じて、大規模タスクを効率的に処理するエージェント工学の手法を解説している。
キーポイント
LLMのコンテキスト制限問題
LLMの能力は向上しているが、コンテキスト制限(トークン数)は約100万で頭打ちとなっており、高品質な結果を得るには約20万以下が推奨される状況にある。
サブエージェントの基本概念
サブエージェントは、親エージェントが特定の目標を達成するために新たなコンテキストウィンドウで自身のコピーを起動する手法であり、大規模タスクを効率的に処理する。
Claude Codeの実装例
Claude Codeは「Explore」サブエージェントを使用して、既存リポジトリの探索を新規コンテキストで実行し、必要な情報を収集して親エージェントに返す実用的なワークフローを実現している。
エージェント工学の実践的パターン
サブエージェントはツール呼び出しと同様に扱われ、モデルが自身にプロンプトを送るという興味深いインタラクションを可能にするエージェント設計の重要なパターンである。
影響分析・編集コメントを表示
影響分析
この記事は、LLMのコンテキスト制限という根本的な課題に対する実践的な解決策としてサブエージェントパターンを提示しており、エージェント工学の実装者にとって具体的な設計指針を提供する。特にClaude Codeの実例を通じて、理論だけでなく実際の開発ワークフローへの適用可能性を示している点が価値がある。
編集コメント
技術ブログの実践的な知見として、LLMエージェント開発者にとって即座に参考になる具体的なパターンを提供している。Claude Codeの実装例が理解を深めるのに効果的。
*Agentic Engineering Patterns >*
LLM はコンテキスト制限によって制約されています。これは、ある時点で作業メモリに収容できるトークンの数を指します。これらの数値は過去 2 年間で劇的な能力向上を遂げた LLM そのものとは対照的に、ほとんど増加していません。一般的には約 1,000,000 トークンで頭打ちとなり、ベンチマークでは 200,000 トークン未満の範囲でより高品質な結果が報告されることが頻繁にあります。
モデルから優れた結果を引き出すためには、これらの制限内に収まるようにコンテキストを慎重に管理することが極めて重要です。
サブエージェントは、コーディングエージェントの貴重なトップレベルのコンテキストを過度に消費することなく、大規模なタスクを処理するためのシンプルかつ効果的な方法を提供します。
コーディングエージェントがサブエージェントを使用すると、それは事実上、指定された目標を達成するために自分自身の新しいコピーを起動し、新しいプロンプトから始まる新たなコンテキストウィンドウを確保します。
Claude Code の Explore サブエージェント
Claude Code は、その標準的な作業プロセスの一部として、サブエージェントを頻繁に使用しています。これを例示的な事例として見てみましょう。
既存のリポジトリに対して新しいタスクを開始する際、Claude Code はまずそのリポジトリを検索し、一般的な構造を把握するとともに、そのタスクを達成するために必要な関連情報を特定する必要があります。
これを行うために、Claude Code はプロンプトを構築し、その探索を実行して発見した内容の記述を返すようサブエージェントを起動します。
私は、ガイドの あるページ のスクリーンショットと以下のプロンプトを添えて、Claude Code セッションを開始しました。
この章の差分表示において、個別に変更されたテキストセグメントに対して赤または緑のより濃い色で、どの文字が変更されたかを示すようにしてください。
このセッションの transcript(逐字書き起こし) です。
Claude Code はまず、以下の初期プロンプトを持つ「探索」サブエージェントを実行しました:
この Django ブログで「章」の差分表示を実装するコードを探してください。以下を見つける必要があります:
- 差分をレンダリングするテンプレート(赤や緑の背景を持つ diff 関連の HTML/CSS を探す)
- 差分を生成する Python コード(difflib の使用または類似のものを探す)
- 差分レンダリングに関連する JavaScript
- 差分表示用の CSS スタイル(赤や緑の行背景)
徹底的に検索してください。templates/, static/, blog/ ディレクトリを確認してください。「diff」「chapter」「revision」「history」「compare」といったキーワードを探します。
サブエージェントは、他のツール呼び出しと同様に機能します:親エージェントが他のツールを呼び出すようにサブエージェントをディスパッチし、応答を待ちます。このようにモデル自身がプロンプトする様子を見るのは興味深いです—彼らは通常、プロンプト戦略において優れたセンスを持っています。
探索サブエージェントは数秒間処理を続け、以下のような内容で応答を返しました:
Perfect! Now let me create a comprehensive summary with all the findings:
Summary: Chapter Diff View Implementation
I found the complete implementation of the diff view for chapters in this Django blog. Here are the key components:
1. Python Code - Diff Generation (/home/user/simonwillisonblog/guides/views.py, lines 139-202)
The chapter_changes() view function uses Python's built-in difflib.unified_diff() to generate diffs: […]
The full subagent response included all of the details the parent agent needed in order to start editing the code to address my original request.
Parallel subagents
This Explore subagent is the simplest example of how subagents can work, with the parent agent pausing while the subagent runs. The principle advantage of this kind of subagent is that it can work with a fresh content in a way that avoids spending tokens from the parent's available limit.
Subagents can also provide a significant performance boost by having the parent agent run multiple subagents at the same time, potentially also using faster and cheaper models such as Claude Haiku to accelerate those tasks.
Coding agents that support subagents can use them based on your instructions. Try prompts like this:
Use subagents to find and update all of the templates that are affected by this change.
For tasks that involve editing several files - and where those files are not dependent on each other - this can offer a significant speed boost.
Specialist subagents
一部のコーディングエージェントでは、サブエージェントをさらにカスタマイズして実行することができ、その多くはカスタムシステムプロンプトやカスタムツール、あるいはその両方の形式で提供されます。これにより、サブエージェントが異なる役割を担うことが可能になります。
これらの役割には、さまざまな有用な専門分野が含まれます:
- コードレビューアエージェントはコードを検査し、バグ、機能の欠落、または設計上の弱点を特定します。
- テストランナージェントはテストを実行します。これは、特にテストスイートが膨大で冗長である場合に非常に価値があります。サブエージェントがメインのコーディングエージェントに対して完全なテスト出力を隠し、失敗の詳細のみを報告して戻ってくるためです。
- デバッガエージェントはデバッグ問題に特化し、トークンの割り当てを使ってコードベースを推論したり、コードのスニペットを実行したりすることで、再現手順の特定やバグの原因究明を支援します。
数十もの異なる専門サブエージェントにタスクを細分化したくなるのは当然ですが、サブエージェントの本質的な価値は、貴重なルートの文脈(root context)を維持し、トークンを大量に消費する操作を管理することにあることを忘れないでください。十分なトークンが余っている限り、メインのコーディングエージェント自身でもデバッグやレビューを行うことは十分に可能です。
公式ドキュメント
いくつかの人気のあるコーディングエージェントはサブエージェントをサポートしており、それぞれ独自の使用方法に関するドキュメントを提供しています:
- OpenAI Codex サブエージェント
- Claude サブエージェント
- Gemini CLI サブエージェント
- Mistral Vibe サブエージェント
- OpenCode エージェント
- Visual Studio Code 内のサブエージェント
- Cursor サブエージェント
Tags: parallel-agents, coding-agents, generative-ai, agentic-engineering, ai, llms
Subagents (続き 5/5)
前回の投稿では、サブエージェント(subagent)の概念を紹介し、複雑なタスクを複数の小さなエージェントに分割して処理するアプローチについて解説しました。この手法は、大規模言語モデル(LLM: Large Language Model)のコンテキストウィンドウ制限や、単一のエージェントが抱える論理の複雑さに対する有効な解決策となります。
サブエージェントを活用することで、各タスクを独立したコンテキストで処理できるため、エラーが発生しても影響範囲を局所化できます。また、並列実行が可能になるため、全体の実行時間を短縮できるケースも少なくありません。例えば、コード生成タスクにおいて、1 つのエージェントがすべてのロジックを一度に処理するのではなく、ファイルの読み込み、構文解析、エラー修正、テスト作成といった役割をそれぞれ別々のサブエージェントに割り当てることで、精度と速度の両方を向上させることができます。
ただし、サブエージェントの設計には注意が必要です。各サブエージェントの入出力形式を明確に定義し、親エージェントが結果を統合するロジックも慎重に実装する必要があります。また、過度に細分化するとオーバーヘッドが増大するため、タスクの性質に応じて適切な粒度で分割することが重要です。
次回の投稿では、実際にサブエージェントを実装したコード例と、その運用におけるベストプラクティスについて詳しく解説します。
原文を表示
*Agentic Engineering Patterns >*
LLMs are restricted by their context limit - how many tokens they can fit in their working memory at any given time. These values have not increased much over the past two years even as the LLMs themselves have seen dramatic improvements in their abilities - they generally top out at around 1,000,000, and benchmarks frequently report better quality results below 200,000.
Carefully managing the context such that it fits within those limits is critical to getting great results out of a model.
Subagents provide a simple but effective way to handle larger tasks without burning through too much of the coding agent’s valuable top-level context.
When a coding agent uses a subagent it effectively dispatches a fresh copy of itself to achieve a specified goal, with a new context window that starts with a fresh prompt.
Claude Code’s Explore subagent
Claude Code uses subagents extensively as part of its standard way of working. Let's use that as an illustrative example.
Any time you start a new task against an existing repo Claude Code first needs to explore that repo to figure out its general shape and find relevant information needed to achieve that task.
It does this by constructing a prompt and dispatching a subagent to perform that exploration and return a description of what it finds.
I kicked off a Claude Code session with a screenshot of a page from my guide and this prompt:
Make the chapter diffs also show which characters have changed in this diff view with a darker color of red or green for the individually changed segments of text within the line
Here's the transcript of that session.
Claude Code started by running an “Explore” subagent with the following starter prompt:
Find the code that implements the diff view for "chapters" in this Django blog. I need to find:
Templates that render diffs (look for diff-related HTML/CSS with red/green backgrounds)
Python code that generates diffs (look for difflib usage or similar)
Any JavaScript related to diff rendering
CSS styles for the diff view (red/green line backgrounds)
Search thoroughly - check templates/, static/, blog/ directories. Look for keywords like "diff", "chapter", "revision", "history", "compare".
Subagents work similar to any other tool call: the parent agent dispatches them just as they would any other tool and waits for the response. It's interesting to see models prompt themselves in this way - they generally have good taste in prompting strategies.
The Explore subagent churned away for a few seconds and returned a response that started like this:
Perfect! Now let me create a comprehensive summary with all the findings:
### Summary: Chapter Diff View Implementation
I found the complete implementation of the diff view for chapters in this Django blog. Here are the key components:
### 1. Python Code - Diff Generation (/home/user/simonwillisonblog/guides/views.py, lines 139-202)
The chapter_changes() view function uses Python's built-in difflib.unified_diff() to generate diffs: […]
The full subagent response included all of the details the parent agent needed in order to start editing the code to address my original request.
Parallel subagents
This Explore subagent is the simplest example of how subagents can work, with the parent agent pausing while the subagent runs. The principle advantage of this kind of subagent is that it can work with a fresh content in a way that avoids spending tokens from the parent’s available limit.
Subagents can also provide a significant performance boost by having the parent agent run multiple subagents at the same time, potentially also using faster and cheaper models such as Claude Haiku to accelerate those tasks.
Coding agents that support subagents can use them based on your instructions. Try prompts like this:
Use subagents to find and update all of the templates that are affected by this change.
For tasks that involve editing several files - and where those files are not dependent on each other - this can offer a significant speed boost.
Specialist subagents
Some coding agents allow subagents to run with further customizations, often in the form of a custom system prompt or custom tools or both, which allow those subagents to take on a different role.
These roles can cover a variety of useful specialties:
- A code reviewer agent can review code and identify bugs, feature gaps or weaknesses in the design.
- A test runner agent can run the test. This is particularly worthwhile if your test suite is large and verbose, as the subagent can hide the full test output from the main coding agent and report back with just details of any failures.
- A debugger agent can specialize in debugging problems, spending its token allowance reasoning though the codebase and running snippets of code to help isolate steps to reproduce and determine the root cause of a bug.
While it can be tempting to go overboard breaking up tasks across dozens of different specialist subagents, it's important to remember that the main value of subagents is in preserving that valuable root context and managing token-heavy operations. Your root coding agent is perfectly capable of debugging or reviewing its own output provided it has the tokens to spare.
Official documentation
Several popular coding agents support subagents, each with their own documentation on how to use them:
- OpenAI Codex subagents
- Claude subagents
- Gemini CLI subagents
- Mistral Vibe subagents
- OpenCode agents
- Subagents in Visual Studio Code
- Cursor Subagents
Tags: parallel-agents, coding-agents, generative-ai, agentic-engineering, ai, llms
関連記事
今日のまとめ
AI日報で今日の重要ニュースをまとめ読み