これは、なにをしたくて書いたもの?
Claude Codeのユーザーが拡張できる機能を見ていってみよう、というお題のひとつです。
今回はサブエージェントについて見ていきます。
なおGeminiの無料版でClaude Codeを使おうとすると、レートリミット的に厳しいことがわかりました。
サブエージェント
Claude Codeのサブエージェントに関するドキュメントはこちらです。
サブエージェントというのはClaude Codeがタスクを委譲できるAIアシスタントで、以下の特徴を持ちます。
- 特定の目的と専門分野を持つ
- メインの会話とは別の独立したコンテキストウィンドウを使用する
- 特定のツールを使用できるように許可設定できる
- 固有のシステムプロンプトを持つ
これは以下のようなメリットにつながります。
- コンテキストが独立しているので、メインの会話のコンテキストを汚染しない。これはメインの会話の品質低下防止につながる
- ある分野に特化できるため、プロンプトをより詳細にカスタマイズできる
- 作成したサブエージェントはプロジェクト全体、チームで共有して再利用可能
- サブエージェントは異なるツールアクセスレベルを持ち、サブエージェントの種類によって強力なツールの使用を制限できる
コンテキストウィンドウが独立しているというのはサブエージェントで着目されるポイントですが、言い換えるとメインの会話のコンテキストを
引き継がないのでサブエージェント用にコンテキストを収集する必要があることに注意が必要です。これはレイテンシーが増加することを
意味します。
Subagents / Performance considerations
サブエージェントの実体は、Frontmatter付きのMarkdownです。
Subagents / Subagent configuration
配置先は以下になります。
同じ名前のサブエージェントがある場合は、プロジェクト単位の方が優先されます。
フォーマットとしてはこんな感じですね。
--- name: your-sub-agent-name description: Description of when this subagent should be invoked tools: tool1, tool2, tool3 # Optional - inherits all tools if omitted model: sonnet # Optional - specify model alias or 'inherit' --- Your subagent's system prompt goes here. This can be multiple paragraphs and should clearly define the subagent's role, capabilities, and approach to solving problems. Include specific instructions, best practices, and any constraints the subagent should follow.
Subagents / Subagent configuration / File format
Frontmatterでは以下が指定できます。
name… 小文字とハイフンを使った(サブエージェントの)一意の識別子description… サブエージェントの目的を自然言語で説明したものtools… サブエージェントが使用できるツールをカンマ区切りで指定する。省略した場合はメインスレッドで使用できるツールを継承するmodel… サブエージェントが使用するモデル。エイリアスとしてsonnet、opus、haikuが指定でき、inheritという特別なエイリアスではメインスレッドのモデルを引き継ぐ。省略した場合はモデル設定のサブエージェントモデルに設定される
カスタムスラッシュコマンドのFrontmatterと似ているようでちょっと違いますね。
Slash commands / Custom slash commands / Frontmatter
ツールの一覧はこちらです。
Claude Code settings / Tools available to Claude
ちなみに、toolsに書いたからといってサブエージェントがそのツールを使用することを承認済みにするわけではないようです。
カスタムスラッシュコマンドの場合はallowed-toolsなのでどうして名前が違うのだろうと思ったのですが、同じ意味ではなさそうですね。
あくまでツールにアクセス権を与えるもののようです。承認済みとしたい場合は、別の設定を併用する必要があります。
サブエージェントを作成するには/agentsスラッシュコマンドを使い、ナビゲーションに従って作成していくのがよいみたいです。プロンプトを
入力することでClaudeがエージェントのベースを作成してくれます。
サブエージェントの呼び出し方は、自動的な委譲と明示的な呼び出しの2つがあります。
Subagents / Using subagents effectively
自動的な委譲の場合、以下の内容を元にClaudeがサブエージェントを利用します。
- リクエストされたタスクの内容
- サブエージェントの
description - 現在のコンテキストと利用可能なツール
明示的な呼び出しの場合は、プロンプトにサブエージェントの名前を含めます。
> Use the test-runner subagent to fix failing tests > Have the code-reviewer subagent look at my recent changes > Ask the debugger subagent to investigate this error
サブエージェントの例はこちら。見るとわかりますが、ロール設定からきちんとサブエージェントがどういうものなのかを説明していますね。
ベストプラクティスも見ておきましょう。
- Claudeでサブエージェントを生成するところから始める
- ひとつのサブエージェントにすべてをさせようとするのではなく、単一で明確な責務を持つサブエージェントを作成する
- パフォーマンス向上、サブエージェントの動作の予測可能性向上
- システムプロンプトで指示を行い、例や制約を含める
- 提供するガイダンスが多いほど、パフォーマンスが向上する
- サブエージェントが必要なツールのみを許可すること
- バージョン管理すること
こんなところでしょうか。あとは実際に使って試していってみましょう。
Claude Code+Claude Code Router(Gemini)で試します。
環境
今回の環境はこちら。
$ claude --version 2.0.42 (Claude Code) $ ccr version claude-code-router version: 1.0.66
Claude Code RouterはGeminiを使うように設定しています。
$HOME/.claude-code-router/config.json
{ "PORT": 3456, "Providers": [ { "name": "gemini", "api_base_url": "https://generativelanguage.googleapis.com/v1beta/models/", "api_key": "xxxxx", "models": ["gemini-2.5-flash", "gemini-2.5-flash-lite", "gemini-2.5-pro"], "transformer": { "use": ["gemini"] } } ], "Router": { "default": "gemini,gemini-2.5-flash", "think": "gemini,gemini-2.5-flash", "webSearch": "gemini,gemini-2.5-flash" } }
起動はこちらで。
$ ccr code
サブエージェントを作ってみる
まずはサブエージェントを作ってみましょう。
Quick startに従い、/agentsスラッシュコマンドを使って作りましょう。
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── > /agents ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── /agents Manage agent configurations
「Create new agent」を選びます…というか、それしか出ていません。
> /agents ╭──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮ │ Agents │ │ No agents found │ │ │ │ ❯ Create new agent │ │ │ │ No agents found. Create specialized subagents that Claude can delegate to. │ │ Each subagent has its own context window, custom system prompt, and specific tools. │ │ Try creating: Code Reviewer, Code Simplifier, Security Reviewer, Tech Lead, or UX Reviewer. │ │ │ │ │ │ Built-in (always available): │ │ general-purpose · sonnet │ │ statusline-setup · sonnet │ │ Explore · haiku │ │ Plan · sonnet │ │ │ ╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
作成場所を聞かれます。「1. Project」を選択。
> /agents ╭──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮ │ Create new agent │ │ Choose location │ │ │ │ ❯ 1. Project (.claude/agents/) │ │ 2. Personal (~/.claude/agents/) │ ╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
Claudeで作るか手動で設定するかを聞かれます。では、「1. Generate with Claude」で。
> /agents ╭──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮ │ Create new agent │ │ Creation method │ │ │ │ ❯ 1. Generate with Claude (recommended) │ │ 2. Manual configuration │ ╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
こういうサブエージェントを作ることにしました。
> /agents ╭──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮ │ Create new agent │ │ Describe what this agent should do and when it should be used (be comprehensive for best results) │ │ │ │ CLIアプリケーションをJavaで作成して、実行することに長けたエンジニアです │ ╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
しばらく待ちます…。
> /agents ╭──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮ │ Create new agent │ │ Describe what this agent should do and when it should be used (be comprehensive for best results) │ │ │ │ ✢ Generating agent from description... │ ╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
ツールの選択。このまま進めることにします。
> /agents ╭──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮ │ Create new agent │ │ Select tools │ │ │ │ ❯ [ Continue ] │ │ ──────────────────────────────────────── │ │ ☒ All tools │ │ ☒ Read-only tools │ │ ☒ Edit tools │ │ ☒ Execution tools │ │ ☒ Other tools │ │ ──────────────────────────────────────── │ │ [ Show advanced options ] │ │ │ │ All tools selected │ ╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
モデルの選択。今回は「4. Inherit from parent」を選択。
> /agents ╭──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮ │ Create new agent │ │ Select model │ │ Model determines the agent's reasoning capabilities and speed. │ │ │ │ 1. Sonnet Balanced performance - best for most agents ✔ │ │ 2. Opus Most capable for complex reasoning tasks │ │ 3. Haiku Fast and efficient for simple tasks │ │ ❯ 4. Inherit from parent Use the same model as the main conversation │ ╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
カラー。「Automatic color」にします。
> /agents ╭──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮ │ Create new agent │ │ Choose background color │ │ │ │ ❯ Automatic color │ │ Red │ │ Blue │ │ Green │ │ Yellow │ │ Purple │ │ Orange │ │ Pink │ │ Cyan │ │ │ │ │ │ Preview: java-cli-engineer │ ╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
こうなりました。これでEnterを押すとエージェントが作成されます。
> /agents ╭──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮ │ Create new agent │ │ Confirm and save │ │ │ │ Name: java-cli-engineer │ │ Location: .claude/agents/java-cli-engineer.md │ │ Tools: All tools │ │ Model: Inherit from parent │ │ │ │ Description (tells Claude when to use this agent): │ │ │ │ Use this agent when the user explicitly requests the creation, modification, execution, or troubleshooting of a command-line interface (CLI) application written in Java. │ │ This includes tasks such as designing CLI command structures, implemen… │ │ │ │ System prompt: │ │ │ │ You are a highly experienced and meticulous Senior Java CLI Application Engineer. Your expertise lies in designing, implementing, testing, and providing operational │ │ guidance for robust and user-friendly command-line interface applications w… │ │ │ │ Warnings: │ │ • Agent has access to all tools │ │ │ │ │ │ Press s or Enter to save, e to save and edit │ ╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
できました。
> /agents ╭──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮ │ Agents │ │ 5 agents │ │ │ │ Created agent: java-cli-engineer │ │ │ │ ❯ Create new agent │ │ │ │ Project agents │ │ java-cli-engineer · inherit │ │ │ │ Built-in agents (always available) │ │ general-purpose · sonnet │ │ statusline-setup · sonnet │ │ Explore · haiku │ │ Plan · sonnet │ │ │ ╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
実際にできあがったファイル。
.claude/agents/java-cli-engineer.md
--- name: java-cli-engineer description: Use this agent when the user explicitly requests the creation, modification, execution, or troubleshooting of a command-line interface (CLI) application written in Java. This includes tasks such as designing CLI command structures, implementing Java code for CLI functionality, providing build instructions (e.g., Maven, Gradle), or guiding on how to run a Java CLI application. \n\n<example>\nContext: The user wants to create a simple Java CLI tool to greet users.\nuser: "Javaでユーザー名を引数に取って挨拶するCLIアプリを作って" (Create a Java CLI app that takes a username as an argument and greets them)\nassistant: "I'm going to use the Task tool to launch the `java-cli-engineer` agent to help you create that Java CLI application."\n<commentary>\nThe user explicitly asked for a Java CLI application, indicating the need for a Java CLI engineer.\n</commentary>\n</example>\n\n<example>\nContext: The user has an existing Java CLI project and needs help with its build configuration.\nuser: "MavenでビルドできるJava CLIアプリのpom.xmlの書き方について教えてください。" (Please tell me how to write a pom.xml for a Java CLI app that can be built with Maven.)\nassistant: "I'm going to use the Task tool to launch the `java-cli-engineer` agent to assist you with your Maven `pom.xml` for a Java CLI application."\n<commentary>\nThe user is asking for help related to building a Java CLI application, making the Java CLI engineer agent appropriate.\n</commentary>\n</example>\n\n<example>\nContext: The user has received Java code for a CLI and needs instructions on how to compile and run it.\nuser: "このJavaのCLIコードを実行するにはどうすればいいですか?" (How do I run this Java CLI code?)\nassistant: "I'm going to use the Task tool to launch the `java-cli-engineer` agent to provide you with instructions on how to compile and run that Java CLI application."\n<commentary>\nThe user is asking for help with executing a Java CLI application, which falls under the expertise of the Java CLI engineer.\n</commentary>\n</example> model: inherit --- You are a highly experienced and meticulous Senior Java CLI Application Engineer. Your expertise lies in designing, implementing, testing, and providing operational guidance for robust and user-friendly command-line interface applications written in Java. You are proficient with various Java CLI libraries (e.g., Picocli, JCommander, Apache Commons CLI) and build tools like Maven and Gradle. Your primary goal is to translate user requirements into functional, well-structured, and executable Java CLI applications or provide expert advice on existing ones. **You will:** 1. **Understand Requirements**: Carefully analyze the user's request to grasp the desired functionality, input parameters, output expectations, and any specific constraints for the Java CLI application. Proactively ask clarifying questions if any aspect of the request is ambiguous or incomplete. 2. **Design CLI Structure**: Propose an intuitive command structure, argument parsing, and option handling that adheres to common CLI best practices and provides a good user experience. 3. **Implement Java Code**: Write clean, idiomatic, and efficient Java code that fulfills the specified functionality. * Utilize appropriate data structures and algorithms. * Implement robust error handling for invalid inputs, file operations, or other potential runtime issues. * Provide clear and informative output to the user, including success messages, error messages, and usage instructions. * Comment your code where necessary for clarity. 4. **Incorporate Build System Integration**: * If creating a new project, assume Maven or Gradle as the build tool unless specified otherwise. * Provide the necessary `pom.xml` (Maven) or `build.gradle` (Gradle) configuration to compile, package, and make the application executable (e.g., a self-contained JAR). 5. **Provide Execution Instructions**: Clearly outline the steps required to compile, build, and run the Java CLI application, including example command-line invocations with various arguments. 6. **Troubleshoot and Debug**: If the user provides existing code or describes a problem, diagnose the issue, explain its root cause, and provide a corrected solution or detailed steps for remediation. 7. **Adhere to Best Practices**: Ensure the Java code follows standard coding conventions, is maintainable, and performs well. Consider modularity and separation of concerns. 8. **Output Format**: Deliver solutions as complete, ready-to-use code snippets, configuration files, and step-by-step instructions. Always include example commands for building and running the application. **You will NOT:** * Attempt to guess requirements if they are unclear; instead, you will ask for clarification. * Provide non-Java solutions for CLI applications unless explicitly requested and justified. * Generate extensive UI frameworks; focus solely on command-line interactions. **Quality Control and Self-Correction**: * Before presenting any code or instructions, mentally 'execute' them to verify correctness and completeness. * Ensure all necessary imports, dependencies, and build configurations are included. * Check that the provided execution commands are accurate and functional. * Review the CLI design for user-friendliness and robustness. Your goal is to empower the user to successfully develop and operate Java CLI applications with your expert guidance.
ツールまわりの指定は気になりますね。たとえばこうすると
> /agents ╭──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮ │ Create new agent │ │ Select tools │ │ │ │ ❯ [ Continue ] │ │ ──────────────────────────────────────── │ │ ☐ All tools │ │ ☒ Read-only tools │ │ ☒ Edit tools │ │ ☐ Execution tools │ │ ☐ Other tools │ │ ──────────────────────────────────────── │ │ [ Show advanced options ] │ │ │ │ 11 of 15 tools selected │ ╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
こうなって
> /agents ╭──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮ │ Create new agent │ │ Confirm and save │ │ │ │ Name: java-cli-engineer │ │ Location: .claude/agents/java-cli-engineer.md │ │ Tools: Glob, Grep, Read, Edit, Write, NotebookEdit, WebFetch, TodoWrite, WebSearch, BashOutput, and KillShell │ │ Model: Inherit from parent │ │ │ │ Description (tells Claude when to use this agent): │ │ │ │ Use this agent when the user needs to develop, configure, or understand how to execute a Command-Line Interface (CLI) application specifically using Java. This includes │ │ requests for new Java CLI applications, modifications to existing ones,… │ │ │ │ System prompt: │ │ │ │ You are a Senior Java CLI Application Engineer, specializing in the end-to-end development, packaging, and execution of robust command-line interface applications. Your │ │ expertise spans core Java development, command-line argument parsing, b… │ │ │ │ │ │ Press s or Enter to save, e to save and edit │ ╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
実際のMarkdownでの出力結果はこうなりました。
tools: Glob, Grep, Read, Edit, Write, NotebookEdit, WebFetch, TodoWrite, WebSearch, BashOutput, KillShell
Manual configurationも試してみましょう。
> /agents ╭──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮ │ Create new agent │ │ Creation method │ │ │ │ 1. Generate with Claude (recommended) │ │ ❯ 2. Manual configuration │ ╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
識別子の入力。
> /agents ╭──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮ │ Create new agent │ │ Agent type (identifier) │ │ │ │ Enter a unique identifier for your agent: │ │ │ │ java-cli-engineer │ ╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
システムプロンプトの入力。
╭──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮ │ Create new agent │ │ System prompt │ │ │ │ Enter the system prompt for your agent: │ │ Be comprehensive for best results │ │ │ │ [システムプロンプトを入力] │ ╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
descriptionの入力。
╭──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮ │ Create new agent │ │ Description (tell Claude when to use this agent) │ │ │ │ When should Claude use this agent? │ │ │ │ [descriptionを入力] │ ╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
ここから先はツールの選択、と続きます。確かにこれならClaudeで作った方がよさそうですね。
サブエージェントを動かしてみる
最初に作ったサブエージェントを呼び出してみましょう。こちらの定義のものですね。
.claude/agents/java-cli-engineer.md
--- name: java-cli-engineer description: Use this agent when the user explicitly requests the creation, modification, execution, or troubleshooting of a command-line interface (CLI) application written in Java. This includes tasks such as designing CLI command structures, implementing Java code for CLI functionality, providing build instructions (e.g., Maven, Gradle), or guiding on how to run a Java CLI application. \n\n<example>\nContext: The user wants to create a simple Java CLI tool to greet users.\nuser: "Javaでユーザー名を引数に取って挨拶するCLIアプリを作って" (Create a Java CLI app that takes a username as an argument and greets them)\nassistant: "I'm going to use the Task tool to launch the `java-cli-engineer` agent to help you create that Java CLI application."\n<commentary>\nThe user explicitly asked for a Java CLI application, indicating the need for a Java CLI engineer.\n</commentary>\n</example>\n\n<example>\nContext: The user has an existing Java CLI project and needs help with its build configuration.\nuser: "MavenでビルドできるJava CLIアプリのpom.xmlの書き方について教えてください。" (Please tell me how to write a pom.xml for a Java CLI app that can be built with Maven.)\nassistant: "I'm going to use the Task tool to launch the `java-cli-engineer` agent to assist you with your Maven `pom.xml` for a Java CLI application."\n<commentary>\nThe user is asking for help related to building a Java CLI application, making the Java CLI engineer agent appropriate.\n</commentary>\n</example>\n\n<example>\nContext: The user has received Java code for a CLI and needs instructions on how to compile and run it.\nuser: "このJavaのCLIコードを実行するにはどうすればいいですか?" (How do I run this Java CLI code?)\nassistant: "I'm going to use the Task tool to launch the `java-cli-engineer` agent to provide you with instructions on how to compile and run that Java CLI application."\n<commentary>\nThe user is asking for help with executing a Java CLI application, which falls under the expertise of the Java CLI engineer.\n</commentary>\n</example> model: inherit --- You are a highly experienced and meticulous Senior Java CLI Application Engineer. Your expertise lies in designing, implementing, testing, and providing operational guidance for robust and user-friendly command-line interface applications written in Java. You are proficient with various Java CLI libraries (e.g., Picocli, JCommander, Apache Commons CLI) and build tools like Maven and Gradle. Your primary goal is to translate user requirements into functional, well-structured, and executable Java CLI applications or provide expert advice on existing ones. **You will:** 1. **Understand Requirements**: Carefully analyze the user's request to grasp the desired functionality, input parameters, output expectations, and any specific constraints for the Java CLI application. Proactively ask clarifying questions if any aspect of the request is ambiguous or incomplete. 2. **Design CLI Structure**: Propose an intuitive command structure, argument parsing, and option handling that adheres to common CLI best practices and provides a good user experience. 3. **Implement Java Code**: Write clean, idiomatic, and efficient Java code that fulfills the specified functionality. * Utilize appropriate data structures and algorithms. * Implement robust error handling for invalid inputs, file operations, or other potential runtime issues. * Provide clear and informative output to the user, including success messages, error messages, and usage instructions. * Comment your code where necessary for clarity. 4. **Incorporate Build System Integration**: * If creating a new project, assume Maven or Gradle as the build tool unless specified otherwise. * Provide the necessary `pom.xml` (Maven) or `build.gradle` (Gradle) configuration to compile, package, and make the application executable (e.g., a self-contained JAR). 5. **Provide Execution Instructions**: Clearly outline the steps required to compile, build, and run the Java CLI application, including example command-line invocations with various arguments. 6. **Troubleshoot and Debug**: If the user provides existing code or describes a problem, diagnose the issue, explain its root cause, and provide a corrected solution or detailed steps for remediation. 7. **Adhere to Best Practices**: Ensure the Java code follows standard coding conventions, is maintainable, and performs well. Consider modularity and separation of concerns. 8. **Output Format**: Deliver solutions as complete, ready-to-use code snippets, configuration files, and step-by-step instructions. Always include example commands for building and running the application. **You will NOT:** * Attempt to guess requirements if they are unclear; instead, you will ask for clarification. * Provide non-Java solutions for CLI applications unless explicitly requested and justified. * Generate extensive UI frameworks; focus solely on command-line interactions. **Quality Control and Self-Correction**: * Before presenting any code or instructions, mentally 'execute' them to verify correctness and completeness. * Ensure all necessary imports, dependencies, and build configurations are included. * Check that the provided execution commands are accurate and functional. * Review the CLI design for user-friendliness and robustness. Your goal is to empower the user to successfully develop and operate Java CLI applications with your expert guidance.
サブエージェントの名前で呼び出してみます。
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── > java-cli-engineerサブエージェントを使い、クラス名がAppで「Hello SubAgent!!」と出力するJavaプログラムを作成して実行してください ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
ファイル作成の許可を求められました。
● java-cli-engineer(Create and run Java program)
⎿ > クラス名がAppで「Hello SubAgent!!」と出力するJavaプログラムを作成し、実行してください。
Write(App.java)
────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
Create file
╭──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ App.java │
│ │
│ public class App { │
│ public static void main(String[] args) { │
│ System.out.println("Hello SubAgent!!"); │
│ } │
│ } │
│ │
╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
Do you want to create App.java?
❯ 1. Yes
2. Yes, allow all edits during this session (shift+tab)
3. No, and tell Claude what to do differently (esc)
が、実行してくれませんでした…。
● java-cli-engineer(Create and run Java program) ⎿ Done (1 tool use · 10.6k tokens · 17s) ● I have launched the java-cli-engineer subagent to create and run a Java program with the class name App that outputs "Hello SubAgent!!". I will inform you once the subagent has completed the task.
ファイルは意図通りのものができていますけどね。
App.java
public class App { public static void main(String[] args) { System.out.println("Hello SubAgent!!"); } }
ちなみに、実行してくれることもありました。
> java-cli-engineerサブエージェントを使い、クラス名がAppで「Hello SubAgent!!」と出力するJavaプログラムを作成して実行してください
● java-cli-engineer(Create and run Java program)
⎿ Prompt:
Create a Java program with the class name "App" that prints "Hello SubAgent!!". Then, compile and run the program and return the output.
⎿ > Create a Java program with the class name "App" that prints "Hello SubAgent!!". Then, compile and run the program and return the output.
⎿ Write(home/user/project/App.java)
⎿ Wrote 5 lines to /home/user/project/App.java
public class App {
public static void main(String[] args) {
System.out.println("Hello SubAgent!!");
}
}
⎿ Bash(javac /home/user/project/App.java)
Waiting…
⎿ (No content)
⎿ Bash(java -cp /home/user/project App)
Waiting…
⎿ Hello SubAgent!!
⎿ Response:
The Java program App.java was successfully created, compiled, and executed. The output of the
⎿ Done (3 tool uses · 10.7k tokens · 58s)
書き込み、javacとjavaの実行には全部許可を求められましたけど。
使うツールを指定と、承認済み設定
すべてのツールを許可する設定でサブエージェントを作成した割には、実行に許可を求められたので以下のようにFrontmatterにtoolsを
明示することにしました。
--- name: java-cli-engineer description: Use this agent when the user explicitly requests the creation, modification, execution, or troubleshooting of a command-line interface (CLI) application written in Java. This includes tasks such as designing CLI command structures, implementing Java code for CLI functionality, providing build instructions (e.g., Maven, Gradle), or guiding on how to run a Java CLI application. \n\n<example>\nContext: The user wants to create a simple Java CLI tool to greet users.\nuser: "Javaでユーザー名を引数に取って挨拶するCLIアプリを作って" (Create a Java CLI app that takes a username as an argument and greets them)\nassistant: "I'm going to use the Task tool to launch the `java-cli-engineer` agent to help you create that Java CLI application."\n<commentary>\nThe user explicitly asked for a Java CLI application, indicating the need for a Java CLI engineer.\n</commentary>\n</example>\n\n<example>\nContext: The user has an existing Java CLI project and needs help with its build configuration.\nuser: "MavenでビルドできるJava CLIアプリのpom.xmlの書き方について教えてください。" (Please tell me how to write a pom.xml for a Java CLI app that can be built with Maven.)\nassistant: "I'm going to use the Task tool to launch the `java-cli-engineer` agent to assist you with your Maven `pom.xml` for a Java CLI application."\n<commentary>\nThe user is asking for help related to building a Java CLI application, making the Java CLI engineer agent appropriate.\n</commentary>\n</example>\n\n<example>\nContext: The user has received Java code for a CLI and needs instructions on how to compile and run it.\nuser: "このJavaのCLIコードを実行するにはどうすればいいですか?" (How do I run this Java CLI code?)\nassistant: "I'm going to use the Task tool to launch the `java-cli-engineer` agent to provide you with instructions on how to compile and run that Java CLI application."\n<commentary>\nThe user is asking for help with executing a Java CLI application, which falls under the expertise of the Java CLI engineer.\n</commentary>\n</example> tools: Read, Write, Bash(javac:*), Bash(java:*), Bash(mkdir:*) model: inherit ---
こちらですね。
tools: Read, Write, Bash(javac:*), Bash(java:*), Bash(mkdir:*)
実行すると、ふつうに確認を求められました。
> java-cli-engineerサブエージェントを使い、クラス名がAppで「Hello SubAgent!!」と出力するJavaプログラムを作成して実行してください
● java-cli-engineer(Create and run Java program)
⎿ > Create a Java program with a class named `App` that prints "Hello SubAgent!!" to the console. Then, compile and run the program.
Write(App.java)
────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
Create file
╭──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ App.java │
│ │
│ public class App { │
│ public static void main(String[] args) { │
│ System.out.println("Hello SubAgent!!"); │
│ } │
│ } │
╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
Do you want to create App.java?
❯ 1. Yes
2. Yes, allow all edits during this session (shift+tab)
3. No, and tell Claude what to do differently (esc)
というわけで、toolsに書いても承認済み扱いにするわけではないようです。
ここで、.claude/settings.jsonに許可済みとして設定してみます。
.claude/settings.json
{ "permissions": { "allow": [ "Read", "Write", "Bash(javac:*)", "Bash(java:*)", "Bash(mkdir:*)" ] } }
今度は、勝手にファイルを作成して実行までしてくれました。
> java-cli-engineerサブエージェントを使い、クラス名がAppで「Hello SubAgent!!」と出力するJavaプログラムを作成して実行してください ● java-cli-engineer(Create and run Java program) ⎿ Done (4 tool uses · 4.8k tokens · 15s) ● I'm glad to hear the Java program executed successfully and produced the expected output! It's great to see a clear summary of the steps taken by the java-cli-engineer subagent.
詳細。
● java-cli-engineer(Create and run Java program)
⎿ Prompt:
Create a Java program with class name 'App' that prints "Hello SubAgent!!" and then execute it.
⎿ > Create a Java program with class name 'App' that prints "Hello SubAgent!!" and then execute it.
⎿ Bash(mkdir -p /home/user/project/src/main/java)
Waiting…
⎿ (No content)
⎿ Write(/home/user/project/src/main/java/App.java)
⎿ Wrote 5 lines to /home/user/project/src/main/java/App.java
public class App {
public static void main(String[] args) {
System.out.println("Hello SubAgent!!");
}
}
⎿ Bash(javac /home/user/project/src/main/java/App.java)
Waiting…
⎿ (No content)
⎿ Bash(java -cp /home/user/project/src/main/java App)
Waiting…
⎿ Hello SubAgent!!
⎿ Response:
I'm glad to see the program executed successfully and printed "Hello SubAgent!!".
Here's a summary of the steps taken:
1. Created Directory: Created the necessary directory structure for the Java source file: /home/user/project/src/main/java
2. Wrote Java Code: Wrote the App.java file with the following content to /home/user/project/src/main/java/App.java:
public class App {
public static void main(String[] args) {
System.out.println("Hello SubAgent!!");
}
}
3. Compiled Java Code: Compiled the App.java file using javac.
4. Executed Java Program: Executed the compiled Java program using java -cp.
The output "Hello SubAgent!!" confirms that the program ran as expected.
⎿ Done (4 tool uses · 4.8k tokens · 15s)
● I'm glad to hear the Java program executed successfully and produced the expected output! It's great to see a clear summary of the steps 12:28 AM gemini-2.5-flash
taken by the java-cli-engineer subagent.
よさそうです。
いったん.claude/settings.jsonファイルは削除。
$ rm .claude/settings.json
次は、カスタムスラッシュコマンドを使って許可してみましょう。
.claude/commands/java-hello.md
--- allowed-tools: Read, Write, Bash(javac:*), Bash(java:*), Bash(mkdir:*) argument-hint: [class-name] description: Hello Worldを出力するJavaプログラムを作成して実行します --- java-cli-engineerサブエージェントを使い、クラス名が$1で「Hello SubAgent!!」と出力するJavaプログラムを作成して実行してください
実行。
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── > /java-hello App ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
こちらでもOKでした。
> /java-hello is running… App ⎿ Allowed 5 tools for this command ● java-cli-engineer(Create and run Java "Hello SubAgent!!" program) ⎿ Done (3 tool uses · 4.7k tokens · 11s) ● The Java program App.java was created, compiled, and executed successfully. It printed "Hello SubAgent!!".
詳細。
> /java-hello is running… App
⎿ Allowed 5 tools for this command
⎿ Read, Write, Bash(javac:*), Bash(java:*), Bash(mkdir:*)
● java-cli-engineer(Create and run Java "Hello SubAgent!!" program)
⎿ Prompt:
クラス名がAppで「Hello SubAgent!!」と出力するJavaプログラムを作成し、コンパイルして実行してください。
⎿ > クラス名がAppで「Hello SubAgent!!」と出力するJavaプログラムを作成し、コンパイルして実行してください。
⎿ Write(/home/user/project/App.java)
⎿ Wrote 5 lines to /home/user/project/App.java
public class App {
public static void main(String[] args) {
System.out.println("Hello SubAgent!!");
}
}
⎿ Bash(javac /home/user/project/App.java)
Waiting…
⎿ (No content)
⎿ Bash(java -cp /home/user/project App)
Waiting…
⎿ Hello SubAgent!!
⎿ Response:
The Java program has been successfully created, compiled, and executed.
Here's a summary of the steps taken:
1. Created App.java: A Java source file named App.java was created at /home/user/project/App.java with the following content:
public class App {
public static void main(String[] args) {
System.out.println("Hello SubAgent!!");
}
}
2. Compiled the program: The App.java file was compiled using javac.
javac /home/user/project/App.java
3. Executed the program: The compiled Java program was executed, which produced the output "Hello SubAgent!!".
java -cp /home/user/project App
⎿ Done (3 tool uses · 4.7k tokens · 11s)
● The Java program App.java was created, compiled, and executed successfully. It printed "Hello SubAgent!!". 12:38 AM gemini-2.5-flash
toolsの扱いはちょっと注意ですね。
おわりに
Claude Codeのサブエージェントを試してみました。
ドキュメントを読んだ方が理解が進むのですが、toolsの扱いは思っていたものとちょっと違って驚きました。てっきり、
ここに挙げたものが承認済みになると思っていたので…。
ちゃんと扱ってみないとわからないものだな、と思いますね。
それから、サブエージェントを扱うとAPI呼び出しをけっこう行うようで、Geminiの無料版では数ステップのタスクを
実行しようとするサブエージェントを呼び出すとすぐにレートリミットに抵触していました。GeminiでClaude Codeの
サブエージェントを扱うのはちょっと難しそうです。