Loading...
    • 開発者ガイド
    • API リファレンス
    • MCP
    • リソース
    • リリースノート
    Search...
    ⌘K
    はじめに
    Claude の紹介クイックスタート
    モデルと料金
    モデル概要モデルの選び方Claude 4.6 の新機能移行ガイドモデルの廃止料金
    Claude で構築する
    機能概要Messages API の使用停止理由の処理プロンプトのベストプラクティス
    モデルの機能
    拡張思考適応型思考エフォート高速モード(リサーチプレビュー)構造化出力引用メッセージのストリーミングバッチ処理PDF サポート検索結果多言語サポートエンベディングビジョン
    ツール
    概要ツール使用の実装方法Web 検索ツールWeb フェッチツールコード実行ツールメモリツールBash ツールコンピュータ使用ツールテキストエディタツール
    ツールインフラストラクチャ
    ツール検索プログラムによるツール呼び出しきめ細かいツールストリーミング
    コンテキスト管理
    コンテキストウィンドウコンパクションコンテキスト編集プロンプトキャッシングトークンカウント
    ファイルとアセット
    Files API
    Agent Skills
    概要クイックスタートベストプラクティスエンタープライズ向け SkillsAPI での Skills の使用
    Agent SDK
    概要クイックスタートTypeScript SDKTypeScript V2(プレビュー)Python SDK移行ガイド
    ストリーミング入力リアルタイムでレスポンスをストリーミング停止理由の処理権限の処理ユーザー承認と入力フックによる実行制御セッション管理ファイルチェックポイントSDK での構造化出力Agent SDK のホスティングAI エージェントの安全なデプロイシステムプロンプトの変更SDK での MCPカスタムツールSDK でのサブエージェントSDK でのスラッシュコマンドSDK での Agent Skillsコストと使用量の追跡Todo リストSDK でのプラグイン
    API での MCP
    MCP コネクタリモート MCP サーバー
    サードパーティプラットフォームでの Claude
    Amazon BedrockMicrosoft FoundryVertex AI
    プロンプトエンジニアリング
    概要プロンプトジェネレータープロンプトテンプレートの使用プロンプト改善ツール明確かつ直接的に例を使う(マルチショットプロンプティング)Claude に考えさせる(CoT)XML タグを使うClaude に役割を与える(システムプロンプト)複雑なプロンプトを連鎖させる長文コンテキストのヒント拡張思考のヒント
    テストと評価
    成功基準の定義テストケースの開発評価ツールの使用レイテンシの削減
    ガードレールの強化
    ハルシネーションの削減出力の一貫性を高めるジェイルブレイクの軽減ストリーミング拒否プロンプト漏洩の削減Claude をキャラクターに保つ
    管理とモニタリング
    Admin API 概要データレジデンシーワークスペース使用量とコスト APIClaude Code Analytics APIゼロデータリテンション
    Console
    Log in
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...

    Solutions

    • AI agents
    • Code modernization
    • Coding
    • Customer support
    • Education
    • Financial services
    • Government
    • Life sciences

    Partners

    • Amazon Bedrock
    • Google Cloud's Vertex AI

    Learn

    • Blog
    • Catalog
    • Courses
    • Use cases
    • Connectors
    • Customer stories
    • Engineering at Anthropic
    • Events
    • Powered by Claude
    • Service partners
    • Startups program

    Company

    • Anthropic
    • Careers
    • Economic Futures
    • Research
    • News
    • Responsible Scaling Policy
    • Security and compliance
    • Transparency

    Learn

    • Blog
    • Catalog
    • Courses
    • Use cases
    • Connectors
    • Customer stories
    • Engineering at Anthropic
    • Events
    • Powered by Claude
    • Service partners
    • Startups program

    Help and security

    • Availability
    • Status
    • Support
    • Discord

    Terms and policies

    • Privacy policy
    • Responsible disclosure policy
    • Terms of service: Commercial
    • Terms of service: Consumer
    • Usage policy
    ガイド

    SDKでのスラッシュコマンド

    SDKを通じてClaude Codeセッションを制御するためのスラッシュコマンドの使い方を学ぶ

    Was this page helpful?

    • /compact - 会話履歴の圧縮
    • /clear - 会話のクリア
    • SDKでのカスタムコマンドの使用

    スラッシュコマンドは、/ で始まる特別なコマンドで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 拡張子を除く)がコマンド名になります
    • ファイルの内容がコマンドの動作を定義します
    • オプションのYAMLフロントマターで設定を提供します

    基本的な例

    .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でのカスタムコマンドの使用

    ファイルシステムで定義されると、カスタムコマンドは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コマンドの実行

    カスタムコマンドは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 fixes

    SDKを通じてこれらのコマンドを使用します:

    関連項目

    • スラッシュコマンド - スラッシュコマンドの完全なドキュメント
    • SDKでのサブエージェント - サブエージェントの同様のファイルシステムベースの設定
    • TypeScript SDKリファレンス - 完全なAPIドキュメント
    • SDK概要 - 一般的なSDKの概念
    • CLIリファレンス - コマンドラインインターフェース
    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 }
    })) {
      // テスト結果を処理
    }