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
    ガイド

    セッション管理

    Claude Agent SDKがセッションとセッション再開をどのように処理するかの理解

    Was this page helpful?

    • セッションIDの取得

    セッション管理

    Claude Agent SDKは、会話の状態と再開を処理するためのセッション管理機能を提供します。セッションを使用すると、完全なコンテキストを維持しながら、複数のインタラクションにわたって会話を継続できます。

    セッションの仕組み

    新しいクエリを開始すると、SDKは自動的にセッションを作成し、最初のシステムメッセージでセッションIDを返します。このIDをキャプチャして、後でセッションを再開できます。

    セッションIDの取得

    import { query } from "@anthropic-ai/claude-agent-sdk"
    
    let sessionId: string | undefined
    
    const response = query({
      prompt: "Help me build a web application",
      options: {
        model: "claude-opus-4-6"
      }
    })
    
    for await (const message of response) {
      // The first message is a system init message with the session ID
      if (message.type === 'system' && message.subtype === 'init') {
        sessionId = message.session_id
        console.log(`Session started with ID: ${sessionId}`)
        // You can save this ID for later resumption
      }
    
      // Process other messages...
      console.log(message)
    }
    
    // Later, you can use the saved sessionId to resume
    if (sessionId) {
      const resumedResponse = query({
        prompt: "Continue where we left off",
        options: {
          resume: sessionId
        }
      })
    }

    セッションの再開

    SDKは以前の会話状態からのセッション再開をサポートしており、継続的な開発ワークフローを可能にします。resume オプションにセッションIDを指定して、以前の会話を継続します。

    SDKはセッションを再開する際に会話履歴とコンテキストの読み込みを自動的に処理し、Claudeが中断したところから正確に継続できるようにします。

    セッション間でファイルの変更を追跡および元に戻すには、ファイルチェックポイントを参照してください。

    セッションのフォーク

    セッションを再開する際、元のセッションを継続するか、新しいブランチにフォークするかを選択できます。デフォルトでは、再開は元のセッションを継続します。forkSession オプション(TypeScript)または fork_session オプション(Python)を使用して、再開状態から開始する新しいセッションIDを作成します。

    セッションをフォークするタイミング

    フォークは以下のような場合に便利です:

    • 同じ開始点から異なるアプローチを探索する
    • 元のセッションを変更せずに複数の会話ブランチを作成する
    • 元のセッション履歴に影響を与えずに変更をテストする
    • 異なる実験のために別々の会話パスを維持する

    フォークと継続の比較

    動作forkSession: false(デフォルト)forkSession: true
    セッションID元と同じ新しいセッションIDが生成される
    履歴元のセッションに追加再開ポイントから新しいブランチを作成
    元のセッション変更される変更されずに保持
    ユースケース線形的な会話を継続代替案を探索するためにブランチ

    例:セッションのフォーク

    import { query } from "@anthropic-ai/claude-agent-sdk"
    
    // Resume a previous session using its ID
    const response = query({
      prompt: "Continue implementing the authentication system from where we left off",
      options: {
        resume: "session-xyz", // Session ID from previous conversation
        model: "claude-opus-4-6",
        allowedTools: ["Read", "Edit", "Write", "Glob", "Grep", "Bash"]
      }
    })
    
    // The conversation continues with full context from the previous session
    for await (const message of response) {
      console.log(message)
    }
    import { query } from "@anthropic-ai/claude-agent-sdk"
    
    // First, capture the session ID
    let sessionId: string | undefined
    
    const response = query({
      prompt: "Help me design a REST API",
      options: { model: "claude-opus-4-6" }
    })
    
    for await (const message of response) {
      if (message.type === 'system' && message.subtype === 'init') {
        sessionId = message.session_id
        console.log(`Original session: ${sessionId}`)
      }
    }
    
    // Fork the session to try a different approach
    const forkedResponse = query({
      prompt: "Now let's redesign this as a GraphQL API instead",
      options: {
        resume: sessionId,
        forkSession: true,  // Creates a new session ID
        model: "claude-opus-4-6"
      }
    })
    
    for await (const message of forkedResponse) {
      if (message.type === 'system' && message.subtype === 'init') {
        console.log(`Forked session: ${message.session_id}`)
        // This will be a different session ID
      }
    }
    
    // The original session remains unchanged and can still be resumed
    const originalContinued = query({
      prompt: "Add authentication to the REST API",
      options: {
        resume: sessionId,
        forkSession: false,  // Continue original session (default)
        model: "claude-opus-4-6"
      }
    })