中級
カスタムエージェントで専門タスクを委任する
.claude/agents/ にエージェント定義を作成し、専門的なタスクをサブエージェントに委任する方法
agentssub-agentsdelegation
カスタムエージェントで専門タスクを委任する
Claude Code のカスタムエージェント機能を使うと、特定の役割に特化したサブエージェントを定義し、メインセッションからタスクを委任できます。各エージェントは独自のツール制限、モデル、メモリスコープを持ちます。
エージェント定義の構造
.claude/agents/ ディレクトリに Markdown ファイルを作成します。
.claude/agents/
code-reviewer.md
test-generator.md
security-reviewer.md
refactorer.md
エージェント定義の例
---
name: security-reviewer
description: "Security-focused code review. Use when reviewing authentication, authorization, data handling, or API endpoint changes."
model: sonnet
tools: ["Read", "Grep", "Glob", "Bash"]
disallowedTools: ["Write", "Edit"]
permissionMode: default
---
You are a senior security reviewer. Analyze code for:
1. **Injection vulnerabilities** - SQL injection, XSS, command injection
2. **Authentication issues** - Session handling, token validation
3. **Data exposure** - Sensitive data in logs, responses, error messages
4. **Access control** - Missing authorization checks
For each finding, provide:
- Severity: CRITICAL / HIGH / MEDIUM / LOW
- Location: file:line
- Description: What the issue is
- Recommendation: How to fix it
Frontmatter の設定項目
| 項目 | 型 | 説明 |
|---|---|---|
name | string | 一意な識別子(小文字・ハイフン) |
description | string | いつ委任すべきかの定義(重要) |
tools | array | 利用可能なツール一覧 |
disallowedTools | array | 除外するツール |
model | string | sonnet, opus, haiku、またはフルモデルID |
permissionMode | string | default, acceptEdits, dontAsk, plan |
maxTurns | integer | 最大ターン数 |
skills | array | 起動時にロードするスキル |
mcpServers | object | サブエージェント用MCPサーバー |
hooks | object | サブエージェントにスコープされたフック |
memory | string | user, project, local |
background | boolean | バックグラウンドタスクとして実行 |
isolation | string | worktree を設定すると独立環境で実行 |
ツール制限の連鎖
エージェント定義の tools フィールドで、そのエージェントがスポーンできるエージェントタイプも制限できます。
# 制限付き: worker と researcher のみスポーン可能
tools: ["Agent(worker, researcher)", "Read", "Write"]
# 無制限: 任意のエージェントをスポーン可能
tools: ["Agent"]
# Agent 省略: サブエージェントのスポーン不可
tools: ["Read", "Write"]
実践的なエージェント構成例
---
name: test-generator
description: "Generate unit tests for TypeScript/JavaScript code. Use after writing new functions or when test coverage is low."
model: sonnet
tools: ["Read", "Write", "Glob", "Grep", "Bash"]
permissionMode: acceptEdits
effort: high
---
Generate comprehensive unit tests using vitest. Follow these rules:
- Test file next to source file: `auth.ts` -> `auth.test.ts`
- Cover: happy path, edge cases, error handling
- Use descriptive test names in Japanese
- Target 80%+ coverage
エージェントの呼び出し
メインセッションでエージェントを呼び出すと、Claude が description に基づいて適切なタイミングで委任します。また、--agent フラグで直接起動することも可能です。
claude --agent security-reviewer
エージェントの description は「いつ使うべきか」を明確に記述することが重要です。Claude はこの description を見て委任を判断します。
あわせて読む
- サブエージェントを並列でスポーンしてタスクを分散する - 並列エージェントの活用方法
- エージェントチームで大規模タスクを並列遂行する - チーム編成とオーケストレーション
関連コンテンツ
ワークフロー
フェーズ4: 実装
計画に基づいてコードを記述するフェーズ。TDD、サブエージェント駆動開発、並列実行などの手法を活用し、品質と効率を両立させる手法を解説。
ワークフロー
フェーズ5: レビュー
実装されたコードの品質、セキュリティ、要件との合致を検証するフェーズ。自動レビューと人間によるレビューを組み合わせ、品質ゲートを確実に通過する手法を解説。
Tips
サブエージェントを並列でスポーンしてタスクを分散する
Agent ツールと TodoWrite を組み合わせて、独立したタスクを複数のサブエージェントに並列分散する方法
Tips
Superpowers のサブエージェント駆動開発パイプライン
brainstorming -> writing-plans -> subagent-driven-development -> finishing の全工程を Superpowers で自動化する方法