スラッシュコマンドは、/ で始まる特別なコマンドでClaude Codeセッションを制御する方法を提供します。これらのコマンドはSDKを通じて送信でき、会話履歴のクリア、メッセージの圧縮、ヘルプの取得などのアクションを実行できます。
Claude Agent SDKは、システム初期化メッセージで利用可能なスラッシュコマンドに関する情報を提供します。セッション開始時にこの情報にアクセスできます:
import { query } from "@anthropic-ai/claude-agent-sdk";
for await (const message of query({
prompt: "Hello Claude",
options: { maxTurns: 1 }
})) {
if (message.type === "system" && message.subtype === "init") {
console.log("Available slash commands:", message.slash_commands);
// 出力例: ["/compact", "/clear", "/help"]
}
}スラッシュコマンドは、通常のテキストと同様にプロンプト文字列に含めて送信します:
/compact - 会話履歴の圧縮/compact コマンドは、重要なコンテキストを保持しながら古いメッセージを要約することで、会話履歴のサイズを削減します:
/clear - 会話のクリア/clear コマンドは、以前の履歴をすべてクリアして新しい会話を開始します:
組み込みのスラッシュコマンドの使用に加えて、SDKを通じて利用可能な独自のカスタムコマンドを作成できます。カスタムコマンドは、サブエージェントの設定と同様に、特定のディレクトリにマークダウンファイルとして定義されます。
カスタムスラッシュコマンドは、スコープに基づいて指定されたディレクトリに保存されます:
.claude/commands/ - 現在のプロジェクトでのみ利用可能~/.claude/commands/ - すべてのプロジェクトで利用可能各カスタムコマンドはマークダウンファイルで、以下の特徴があります:
.md 拡張子を除く)がコマンド名になります.claude/commands/refactor.md を作成:
Refactor the selected code to improve readability and maintainability.
Focus on clean code principles and best practices.これにより、SDKを通じて使用できる /refactor コマンドが作成されます。
.claude/commands/security-check.md を作成:
---
allowed-tools: Read, Grep, Glob
description: Run security vulnerability scan
model: claude-opus-4-6
---
Analyze the codebase for security vulnerabilities including:
- SQL injection risks
- XSS vulnerabilities
- Exposed credentials
- Insecure configurationsファイルシステムで定義されると、カスタムコマンドはSDKを通じて自動的に利用可能になります:
カスタムコマンドはプレースホルダーを使用した動的な引数をサポートします:
.claude/commands/fix-issue.md を作成:
---
argument-hint: [issue-number] [priority]
description: Fix a GitHub issue
---
Fix issue #$1 with priority $2.
Check the issue description and implement the necessary changes.SDKでの使用:
カスタムコマンドはbashコマンドを実行し、その出力を含めることができます:
.claude/commands/git-commit.md を作成:
---
allowed-tools: Bash(git add:*), Bash(git status:*), Bash(git commit:*)
description: Create a git commit
---
## Context
- Current status: !`git status`
- Current diff: !`git diff HEAD`
## Task
Create a git commit with appropriate message based on the changes.@ プレフィックスを使用してファイルの内容を含めます:
.claude/commands/review-config.md を作成:
---
description: Review configuration files
---
Review the following configuration files for issues:
- Package config: @package.json
- TypeScript config: @tsconfig.json
- Environment config: @.env
Check for security issues, outdated dependencies, and misconfigurations.サブディレクトリでコマンドを整理して、より良い構造にします:
.claude/commands/
├── frontend/
│ ├── component.md # /component (project:frontend) を作成
│ └── style-check.md # /style-check (project:frontend) を作成
├── backend/
│ ├── api-test.md # /api-test (project:backend) を作成
│ └── db-migrate.md # /db-migrate (project:backend) を作成
└── review.md # /review (project) を作成サブディレクトリはコマンドの説明に表示されますが、コマンド名自体には影響しません。
.claude/commands/code-review.md を作成:
---
allowed-tools: Read, Grep, Glob, Bash(git diff:*)
description: Comprehensive code review
---
## Changed Files
!`git diff --name-only HEAD~1`
## Detailed Changes
!`git diff HEAD~1`
## Review Checklist
Review the above changes for:
1. Code quality and readability
2. Security vulnerabilities
3. Performance implications
4. Test coverage
5. Documentation completeness
Provide specific, actionable feedback organized by priority..claude/commands/test.md を作成:
---
allowed-tools: Bash, Read, Edit
argument-hint: [test-pattern]
description: Run tests with optional pattern
---
Run tests matching pattern: $ARGUMENTS
1. Detect the test framework (Jest, pytest, etc.)
2. Run tests with the provided pattern
3. If tests fail, analyze and fix them
4. Re-run to verify fixesSDKを通じてこれらのコマンドを使用します:
Was this page helpful?
import { query } from "@anthropic-ai/claude-agent-sdk";
// スラッシュコマンドを送信
for await (const message of query({
prompt: "/compact",
options: { maxTurns: 1 }
})) {
if (message.type === "result") {
console.log("Command executed:", message.result);
}
}import { query } from "@anthropic-ai/claude-agent-sdk";
for await (const message of query({
prompt: "/compact",
options: { maxTurns: 1 }
})) {
if (message.type === "system" && message.subtype === "compact_boundary") {
console.log("Compaction completed");
console.log("Pre-compaction tokens:", message.compact_metadata.pre_tokens);
console.log("Trigger:", message.compact_metadata.trigger);
}
}import { query } from "@anthropic-ai/claude-agent-sdk";
// 会話をクリアして新しく開始
for await (const message of query({
prompt: "/clear",
options: { maxTurns: 1 }
})) {
if (message.type === "system" && message.subtype === "init") {
console.log("Conversation cleared, new session started");
console.log("Session ID:", message.session_id);
}
}import { query } from "@anthropic-ai/claude-agent-sdk";
// カスタムコマンドを使用
for await (const message of query({
prompt: "/refactor src/auth/login.ts",
options: { maxTurns: 3 }
})) {
if (message.type === "assistant") {
console.log("Refactoring suggestions:", message.message);
}
}
// カスタムコマンドはslash_commandsリストに表示されます
for await (const message of query({
prompt: "Hello",
options: { maxTurns: 1 }
})) {
if (message.type === "system" && message.subtype === "init") {
// 組み込みコマンドとカスタムコマンドの両方が含まれます
console.log("Available commands:", message.slash_commands);
// 例: ["/compact", "/clear", "/help", "/refactor", "/security-check"]
}
}import { query } from "@anthropic-ai/claude-agent-sdk";
// カスタムコマンドに引数を渡す
for await (const message of query({
prompt: "/fix-issue 123 high",
options: { maxTurns: 5 }
})) {
// コマンドは $1="123" と $2="high" で処理されます
if (message.type === "result") {
console.log("Issue fixed:", message.result);
}
}import { query } from "@anthropic-ai/claude-agent-sdk";
// コードレビューを実行
for await (const message of query({
prompt: "/code-review",
options: { maxTurns: 3 }
})) {
// レビューフィードバックを処理
}
// 特定のテストを実行
for await (const message of query({
prompt: "/test auth",
options: { maxTurns: 5 }
})) {
// テスト結果を処理
}