Documentation Index
Fetch the complete documentation index at: https://docs.qoder.com/llms.txt
Use this file to discover all available pages before exploring further.
サブ Agent は、メインセッションが一時的に委任できる専門ロールです。Qoder Agent SDK は次の2種類のサブ Agent をサポートします。
- 組み込みサブ Agent:qodercli が提供します。汎用検索、コード探索、計画などがあります。
- カスタムサブ Agent:SDK 利用者が
options.agents で定義します。業務レビュー、テスト実行、セキュリティ分析などの専門ロールに適しています。
このガイドでは、SDK から組み込みサブ Agent を使う方法と、必要に応じてカスタムサブ Agent を定義する方法を説明します。順番に読んでも、特定の設定項目だけを参照してもかまいません。完全な型定義は Agents Reference を参照してください。
特に断りがない限り、このガイドの「Agent」はメインセッションから委任できるサブ Agent を指します。options.agent は、あるサブ Agent 定義をメインセッションのロールとして実行する特別な使い方です。
組み込みサブ Agent
組み込みサブ Agent を使う場合、自分でサブ Agent 定義を書く必要はありません。名前を知っていれば SDK から参照できます。
現在 qodercli でよく使われる組み込みサブ Agent:
| 名前 | 用途 |
|---|
general-purpose | 汎用サブ Agent。コード検索、複雑な問題の調査、複数ステップのタスク実行に適しています |
Explore | 読み取り専用のコード探索サブ Agent。ファイル検索、キーワード検索、コード構造の把握に適しています |
Plan | 読み取り専用の計画サブ Agent。実装方針の設計、重要ファイルの特定、アーキテクチャ上のトレードオフ分析に適しています |
組み込みサブ Agent の一覧は、qodercli のバージョンや現在の設定によって変わります。対話型 CLI では /agents を入力すると、現在検出されているサブ Agent を確認できます。コマンドラインでも次を実行できます。
SDK セッション初期化後は、q.supportedAgents() で現在のセッションから実際に利用できるサブ Agent を取得できます。
import { query } from '@qoder-ai/qoder-agent-sdk';
const q = query({ prompt: 'List available agents.' });
const agents = await q.supportedAgents();
q.supportedAgents() の戻り値には、options.agents で登録したサブ Agent と、現在の CLI が検出した SDK セッション向けの組み込み、ユーザー、プロジェクト、プラグインのサブ Agent が含まれます。general-purpose、Explore、Plan は SDK セッションでデフォルト表示される場合があります。対話型 CLI 補助 Agent の qoder-guide と statusline-setup は SDK セッションではサポートされず、q.supportedAgents() にも含まれません。戻り値の構造は AgentInfo を参照してください。
組み込みサブ Agent の使用
メインセッションロールとして実行する
セッション全体を組み込みサブ Agent のロールで実行したい場合は、組み込みサブ Agent 名を options.agent に直接渡します。options.agents で再定義する必要はありません。
import { query } from '@qoder-ai/qoder-agent-sdk';
const q = query({
prompt: 'Summarize this project architecture and identify the most important modules.',
options: {
agent: 'general-purpose',
},
});
options.agent は、SDK が登録したサブ Agent だけでなく、現在の CLI が検出した SDK セッション向けの組み込み、ユーザー、プロジェクト、プラグインのサブ Agent も参照できます。
サブ Agent として委任する
サブ Agent への委任は、組み込みの Agent ツールを通じて行われます。メインセッションの利用可能ツール集合に Agent が含まれていないと、モデルは委任を開始できません。SDK では、このツール呼び出し経路を事前承認し、prompt 内で対象サブ Agent を指名する方法がよく使われます。
const q = query({
prompt: 'Use the Explore agent to find where authentication is implemented.',
options: {
// Pre-authorize Agent tool calls. If options.tools restricts the tool set, include 'Agent' there too.
allowedTools: ['Agent'],
},
});
allowedTools: ['Agent'] は、この種類のツール呼び出しを許可または事前承認します。同時に options.tools でメインセッションのツール許可リストを絞る場合は、tools にも Agent を含めてください。Agent を disallowedTools に入れないでください。
サブ Agent 名は、大文字小文字を含めて検出結果と一致している必要があります。たとえば現在の組み込み名には Explore、Plan、general-purpose があります。不確かな場合は先に q.supportedAgents() を呼び出してください。
カスタムサブ Agent
組み込みサブ Agent が業務やプロジェクトの制約に合わない場合は、options.agents でカスタムサブ Agent を定義できます。例:
- 読み取り専用コードレビューサブ Agent:ファイルの読み取りと検索だけが可能で、コードは変更できません。
- テスト実行サブ Agent:テストコマンドを実行し、失敗理由を分析できます。
- セキュリティレビューサブ Agent:認証、認可、インジェクション、機密情報漏えいなどのリスクに集中します。
- 業務サポートサブ Agent:注文検索、チケット検索、内部ナレッジベース検索など、指定した MCP ツールだけを呼び出せます。
カスタムサブ Agent は通常、次の3ステップで構成します。
options.agents にサブ Agent 名、用途説明、システムプロンプトを定義する。
tools または disallowedTools で利用可能なツールを絞る。
- メインセッションから
Agent ツールで委任するか、options.agent でメインセッションを直接駆動させる。
Agent ツールは必須です:カスタム subagents は、メインセッションが組み込み Agent ツールを通じて委任する必要があります。Qoder は Agent ツール経由で subagents を呼び出すため、allowedTools に Agent tool を含める必要があります。
options.agents でカスタムサブ Agent を定義する
最小例:読み取り専用のコードレビューサブ Agent を登録します。
import { query } from '@qoder-ai/qoder-agent-sdk';
const q = query({
prompt: 'Use the code-reviewer agent to review the authentication module.',
options: {
// Pre-authorize Agent tool calls. If options.tools restricts the tool set, include 'Agent' there too.
allowedTools: ['Agent'],
agents: {
'code-reviewer': {
description:
'Reviews code for correctness, security issues, and maintainability problems.',
prompt: `You are a code review specialist.
Review the requested code and report concrete findings.
Sort findings by severity and include file paths when possible.`,
tools: ['Read', 'Grep', 'Glob'],
},
},
},
});
for await (const message of q) {
// Consume streamed messages according to your application needs.
}
この例の重要点は3つです。
options.agents は、このセッションで利用できるカスタムサブ Agent を登録します。
- サブ Agent 委任は
Agent ツールで行われるため、この例では allowedTools: ['Agent'] でその呼び出し経路を事前承認する必要があります。
- サブ Agent 自身の
tools は読み取りと検索だけを許可しているため、ファイル編集もコマンド実行もできません。
options.agents の入力
options.agents は、サブ Agent 名からサブ Agent 定義へのマップです。完全な型は AgentDefinition を参照してください。
| フィールド | 型 | 必須 | 設定例 | 説明 |
|---|
description | string | はい | このサブ Agent をいつ使うかを一文で説明 | モデル向けのルーティング説明。呼び出されるかに影響します |
prompt | string | はい | サブ Agent のロール、境界、出力要件 | そのサブ Agent のシステムプロンプト |
tools | string[] | いいえ | ['Read', 'Grep', 'Glob'] など | ツール許可リスト。設定後は列挙されたツールだけを使用できます |
disallowedTools | string[] | いいえ | ['Bash', 'Write'] など | ツール拒否リスト。少数のツールだけを除外する場合に適します |
model | string | いいえ | 'inherit'、'auto'、'performance' など | サブ Agent のモデル設定 |
maxTurns | number | いいえ | 8 など | サブ Agent が実行できる最大ターン数 |
effort | EffortLevel | いいえ | 'low'、'medium'、'high'、'max' など | 推論努力レベルを制御します |
permissionMode | PermissionMode | いいえ | 'default'、'acceptEdits'、'plan' など | サブ Agent 内部のツール呼び出し権限モードを制御します |
skills | string[] | いいえ | ['review'] など | サブ Agent コンテキストにプリロードする skill |
mcpServers | AgentMcpServerSpec[] | いいえ | MCP server 名または設定 | サブ Agent の MCP server を制限または追加します |
initialPrompt | string | いいえ | 最初の自動入力 | このサブ Agent が options.agent でメインセッションロールになった場合のみ有効です |
サブ Agent ロールの設定
description と prompt は、カスタムサブ Agent で最も重要な2つのフィールドです。
description
description は「いつこのサブ Agent を使うべきか」を説明します。モデルはそれに基づいて委任するかを判断します。
description:
'Runs project tests, analyzes failing output, and suggests fixes.'
よい description はタスクの境界を具体的に示します。A helpful agent や Helper のような汎用的な説明は避けてください。
prompt
prompt は、サブ Agent のロール、境界、出力形式を定義します。TypeScript ユーザーが prompt を渡さない場合は型チェックでエラーになります。JavaScript ユーザーも常にこのフィールドを渡してください。
prompt: `You are a code review specialist.
Only review the requested code; do not edit files.
Return findings sorted by severity, with file paths and suggested fixes.`
prompt が「あなたは役に立つアシスタントです」のように曖昧だと、モデルはそのサブ Agent を呼び出すかもしれませんが、挙動は汎用アシスタントに近くなります。何を優先すべきか、何を避けるべきか、結果をどう返すべきかが明確になりません。
prompt では少なくとも次の3点を説明することを推奨します。
| 説明すること | 例 |
|---|
| 担当タスク | Review code for security and maintainability issues. |
| してはいけないこと | Do not edit files. Do not run commands. |
| 結果の返し方 | Return findings sorted by severity with file paths. |
モデルと推論の設定
まず description と prompt を明確にしてから、model、effort、maxTurns を検討してください。前者はサブ Agent が正しく呼ばれるか、呼ばれた後に境界を理解できるかを決めます。後者は、ロールが明確になった後で品質、速度、コストを調整するためのものです。
| 設定 | 制御するもの | 優先的に調整する場面 |
|---|
model | サブ Agent が使うモデル階層 | このサブ Agent が固定タイプのタスクを継続的に担当し、能力と Credit コストを安定制御したい場合 |
effort | 同じモデルでどれだけ推論予算を使うか | 同じサブ Agent が、ときどきより複雑で慎重な推論を必要とするタスクを受ける場合 |
maxTurns | 最大実行ターン数 | タスクが深く探索しすぎる、長く走りすぎる、またはコストにハード上限を設けたい場合 |
判断順序の目安:
- 最初は設定しないか、
model: 'inherit' を設定して、サブ Agent をメインセッションのモデルに追従させる。
- 高頻度で単純、粗い回答を許容できる低リスクタスクでは、
model: 'efficient' または model: 'lite' でコストを下げる。
- アーキテクチャ設計、複雑なリファクタリング、複数モジュール横断レビューなど高リスクタスクでは、
model: 'performance' または model: 'ultimate' で能力上限を上げる。
- モデル階層は変えず、現在のタスクでもう少し考えさせたい場合は
effort を上げる。
- 探索の暴走が心配な場合は、
maxTurns で実行ターン数に上限を設ける。
よくある組み合わせ:
| シナリオ | 推奨設定 | 説明 |
|---|
| 簡単な Q&A、形式変換、単純な位置特定 | model: 'efficient'、effort: 'low'、maxTurns: 3 | 安価で速く、低リスクタスクに適しています |
| 読み取り専用のコード探索、モジュール関係の整理 | model: 'auto' または 'inherit'、effort: 'medium' | ルーティングまたはメインセッションモデルに能力階層を決めさせます |
| コードレビュー、セキュリティレビュー、移行計画 | model: 'performance'、effort: 'high' | 慎重な判断や問題発見が必要なタスクに適しています |
| 複雑なシステム設計、高難度の問題分析 | model: 'ultimate'、effort: 'high' または 'max' | コストは高く、重要で複雑なタスクにのみ推奨します |
| 大量の補助サブタスク | model: 'efficient'、effort: 'low'、小さめの maxTurns | 複数サブ Agent 協調時の総コスト制御に適しています |
例:サブ Agent ごとに異なる戦略を設定します。
agents: {
explorer: {
description: 'Quickly searches code and summarizes relevant files.',
prompt: 'Find relevant files and return a concise summary. Do not edit.',
tools: ['Read', 'Grep', 'Glob'],
model: 'efficient',
effort: 'low',
maxTurns: 5,
},
architect: {
description: 'Designs complex implementation plans across modules.',
prompt: 'Analyze tradeoffs carefully and return an implementation plan with risks.',
tools: ['Read', 'Grep', 'Glob'],
model: 'performance',
effort: 'high',
maxTurns: 10,
},
}
各フィールドの完全な値は Agents Reference - model、Agents Reference - maxTurns、Agents Reference - effort を参照してください。
サブ Agent のツール制御
カスタムサブ Agent とメインセッションは同じツール名を使います。組み込みツール名には Read、Grep、Glob、Bash などがあります。カスタム MCP ツール名は完全形式 mcp__{serverName}__{toolName} を使います。この設定は、サブ Agent がどのツールを見て呼び出せるかを決めます。
メインセッション側の Agent ツール
options.agents はサブ Agent を登録するだけで、モデルに自動で呼び出させるわけではありません。モデルがタスクを委任するには、メインセッションのツール集合に Agent が含まれている必要があります。デフォルトのツール集合には通常これが登録されています。options.tools でメインセッションの利用可能ツールを制限する場合は、Agent を含めてください。disallowedTools: ['Agent'] を設定すると、委任は無効になります。
tools はサブ Agent のツール許可リストです。設定後、このサブ Agent は列挙されたツールだけを使用できます。
tools: ['Read', 'Grep', 'Glob']
よく使うツール組み合わせ:
| シナリオ | 推奨ツール | 説明 |
|---|
| 読み取り専用分析 | Read, Grep, Glob | コードを読めますが、変更やコマンド実行はできません |
| テスト実行 | Bash, Read, Grep | テストコマンドを実行し、出力を分析できます |
| コード作成 | Read, Edit, Write, Grep, Glob | ファイルを読み書きできますが、直接コマンドは実行できません |
| 業務ツール呼び出し | mcp__server__tool | 指定したカスタムツールだけを許可します |
disallowedTools は「大部分のツールは許可し、少数だけ除外したい」場合に適しています。
disallowedTools: ['Bash', 'Write']
最終的なツール集合を明確に理解している場合を除き、通常は tools と disallowedTools を同時に設定しないでください。
メインセッションのツール設定との関係
サブ Agent の tools / disallowedTools は、そのサブ Agent にだけ作用します。メインセッションのツール許可リストや拒否リストの絞り込みは継承しません。メインセッション側で Agent という委任経路だけを事前承認していても、サブ Agent は自分に設定された Read、Grep などのツールを使用できます。
query({
prompt: 'Use the analyst agent to inspect the repository.',
options: {
// Pre-authorize the main session to call the Agent tool.
allowedTools: ['Agent'],
agents: {
analyst: {
description: 'Reads and summarizes code structure.',
prompt: 'Inspect relevant files and return a concise summary. Do not edit files.',
tools: ['Read', 'Grep', 'Glob'],
},
},
},
});
サブ Agent の権限制御
ツール集合は、サブ Agent が「どのツールを呼び出せるか」を決めます。権限設定は、それらのツール呼び出しを「どのように承認またはブロックするか」を決めます。サブ Agent に個別の permissionMode を設定し、内部ツール実行の権限挙動を制御できます。
権限モード:permissionMode
agents: {
planner: {
description: 'Plans implementation work without making changes.',
prompt: 'Read relevant files and return an implementation plan. Do not edit files.',
tools: ['Read', 'Grep', 'Glob'],
permissionMode: 'plan',
},
}
permissionMode の完全な選択肢と意味は Agents Reference - permissionMode を参照してください。ホストがツール呼び出しを毎回承認する必要がある場合は、セッションレベルの canUseTool コールバックを使用できます。
サブ Agent への skills 読み込み
skills は、特定のサブ Agent に専門スキルをプリロードするために使います。特定のワークフロー、チーム規約、ツール利用方法を毎回 prompt に入れるのではなく、サブ Agent に紐づけたい場合に適しています。
agents: {
reviewer: {
description: 'Reviews pull requests using the team review workflow.',
prompt: 'Review the requested changes and return actionable findings.',
tools: ['Read', 'Grep', 'Glob'],
skills: ['review'],
},
}
推奨:
| シナリオ | 設定方法 |
|---|
| サブ Agent が常に特定の専門フローで作業する | 対応する skill をそのサブ Agent の skills に入れる |
| メインセッションだけが skill を必要とする | セッションレベルの options.skills を使い、サブ Agent には入れない |
| プラグインが提供する skill | sdk-test-plugin:sdk-echo のようなプラグイン修飾名を使う |
サブ Agent の skills はそのサブ Agent のコンテキストだけに影響します。メインセッションの options.skills と同じではありません。セッションレベルの skill 挙動は Skills を参照してください。
サブ Agent の mcpServers 設定
mcpServers は、サブ Agent の MCP server を制限または追加するために使います。注文検索、チケット検索、内部ナレッジベース検索など、必要なサブ Agent だけに業務ツールを公開したい場合に適しています。
セッションですでに設定した MCP server を参照できます。
const q = query({
prompt: 'Use the support agent to check the latest order status.',
options: {
mcpServers: {
orders: {
type: 'stdio',
command: 'node',
args: ['servers/orders.js'],
},
},
allowedTools: ['Agent'],
agents: {
support: {
description: 'Answers customer support questions using order tools.',
prompt: 'Use order tools when needed and return a concise answer.',
mcpServers: ['orders'],
tools: ['mcp__orders__get_order'],
},
},
},
});
特定のサブ Agent 専用の MCP server を設定することもできます。
agents: {
knowledge: {
description: 'Searches the internal knowledge base.',
prompt: 'Search the knowledge base and cite the relevant entries.',
mcpServers: [
{
kb: {
type: 'stdio',
command: 'node',
args: ['servers/kb.js'],
},
},
],
tools: ['mcp__kb__search'],
},
}
推奨:
| シナリオ | 設定方法 |
|---|
| 複数のサブ Agent が同じ MCP server を共有する | セッションレベルの options.mcpServers に server を設定し、サブ Agent の mcpServers で名前を参照する |
| あるサブ Agent だけが業務ツールを必要とする | server をそのサブ Agent の mcpServers に入れ、tools で呼び出し可能ツールを制限する |
| 特定の MCP ツールだけを呼び出したい | 同時に tools: ['mcp__server__tool'] を設定し、server の全ツールを公開しない |
MCP server の設定構造は MCP と Agents Reference - mcpServers を参照してください。
サブ Agent の呼び出し
サブ Agent にはよく使われる3つの呼び出し方法があります。
自動呼び出し
モデルはタスクと各サブ Agent の description に基づいて、呼び出すかどうかを自動で判断します。description を明確にすると命中精度が上がります。
agents: {
tester: {
description: 'Runs tests and analyzes test failures.',
prompt: 'Run relevant tests and explain any failures clearly.',
tools: ['Bash', 'Read', 'Grep'],
},
}
明示的な指名呼び出し
モデルに特定のサブ Agent を使わせたい場合は、prompt で名前を指定します。
const q = query({
prompt: 'Use the tester agent to run the unit tests and summarize failures.',
options: {
// Pre-authorize Agent tool calls. If options.tools restricts the tool set, include 'Agent' there too.
allowedTools: ['Agent'],
agents: {
tester: {
description: 'Runs tests and analyzes failures.',
prompt: 'Run the requested tests and explain failures clearly.',
tools: ['Bash', 'Read', 'Grep'],
},
},
},
});
メインセッションロールとして実行する
options.agent は、メインセッションをあるサブ Agent の身分で直接実行させます。
const q = query({
prompt: 'Plan the refactor for the payment module.',
options: {
agents: {
planner: {
description: 'Plans implementation work before code changes.',
prompt: 'Break the task into clear steps, risks, and validation checks.',
tools: ['Read', 'Grep', 'Glob'],
model: 'inherit',
},
},
agent: 'planner',
},
});
options.agent は、options.agents のカスタムサブ Agent だけでなく、現在の CLI が検出した組み込み、ユーザー、プロジェクト、プラグインのサブ Agent も参照できます。
サブ Agent のコンテキストと結果
サブ Agent は独立したコンテキストで実行されます。自分のシステムプロンプトと委任 prompt は受け取りますが、親セッションの完全な履歴を直接継承しません。
| サブ Agent が見えるもの | サブ Agent が見えないもの |
|---|
自分の prompt | 親セッションの完全な会話履歴 |
メインセッションが Agent ツールを通じて渡したタスク prompt | 親セッションの中間ツール結果 |
| 自分が利用可能なツール定義 | 渡されていない親セッションの非公開推論過程 |
| 設定でプリロードされた skills | 他の Agent の中間コンテキスト |
親セッションからサブ Agent へ情報を渡す主な経路は、Agent ツール呼び出し時に渡すタスク prompt です。サブ Agent が特定のファイルパス、エラー情報、業務背景を必要とする場合は、メインセッションが委任時に明示的に渡すようにしてください。
サブ Agent 完了後、親セッションが受け取るのはその最終応答であり、内部の各ツール呼び出しの全コンテキストではありません。これが、サブ Agent によるコンテキスト分離の主な価値です。
組み合わせ例
複数ロール協調
異なるロールを持つ複数のサブ Agent を登録し、メインセッションがタスクに応じて呼び出すか、いつ呼び出すかを判断します。
const q = query({
prompt: 'Add input validation to the user registration endpoint.',
options: {
// Pre-authorize Agent tool calls. If options.tools restricts the tool set, include 'Agent' there too.
allowedTools: ['Agent'],
agents: {
researcher: {
description: 'Reads existing code to understand patterns and constraints.',
prompt: 'Research relevant files and report implementation constraints. Do not edit.',
tools: ['Read', 'Grep', 'Glob'],
maxTurns: 8,
},
implementer: {
description: 'Implements code changes following existing project conventions.',
prompt: 'Implement the requested change with minimal, idiomatic edits.',
tools: ['Read', 'Edit', 'Write', 'Grep', 'Glob'],
maxTurns: 12,
},
tester: {
description: 'Runs tests and explains failures.',
prompt: 'Run relevant tests, summarize results, and identify failing cases.',
tools: ['Bash', 'Read', 'Grep'],
maxTurns: 6,
},
},
},
});
起動後に最初のタスクを自動実行する
initialPrompt は、そのサブ Agent が options.agent によりメインセッションロールになった場合だけ有効です。
const q = query({
prompt: '',
options: {
agents: {
auditor: {
description: 'Audits code for security risks.',
prompt: 'Scan code for security risks and produce a concise report.',
initialPrompt: 'Start with the authentication and session management code.',
tools: ['Read', 'Grep', 'Glob'],
effort: 'high',
},
},
agent: 'auditor',
},
});
auditor がメインセッションから Agent ツールでサブ Agent として呼び出された場合、initialPrompt は無視されます。
よくある落とし穴
- 組み込みサブ Agent を直接使う場合、明示的に上書きしたいのでなければ、
options.agents に同名のサブ Agent を再定義しないでください。
- サブ Agent の呼び出しは、メインセッションの
Agent ツールに依存します。allowedTools: ['Agent'] は事前承認です。options.tools でメインセッションの利用可能ツールを制限する場合は、そこにも Agent を含めてください。
- サブ Agent 名は現在の検出結果と大文字小文字まで一致する必要があります。たとえば
Explore と Plan は先頭が大文字です。
description はモデルにいつ呼び出すかを伝え、prompt は呼び出された後にサブ Agent がどう動くかを伝えます。
- サブ Agent はさらに自分のサブ Agent を生成できません。サブ Agent の
tools に Agent を入れないでください。
initialPrompt は options.agent で指定されたメインセッションロールにだけ有効で、サブ Agent として委任された場合は無視されます。
- サブ Agent 専用 MCP server は
mcpServers で設定できます。MCP の接続方式は MCP を参照してください。
続きを読む
- Agents Reference:
AgentDefinition、AgentInfo、options.agent、options.agents の完全なリファレンス。
- Tools:組み込みツール、カスタムツール、ツール権限。
- 権限制御:
permissionMode、allowedTools、canUseTool。
- Skills:セッションレベルとサブ Agent レベルの skill 設定。