環境変数

CLAUDE_CODE_CHILD_SESSION

Claude Code が Bash、PowerShell、Monitor ツール、フックコマンド、ステータスラインコマンド経由で起動するサブプロセス内で 1 に設定される。stdio MCP サーバーのサブプロセスには設定されない。これらは長期間存続し、起動元のセッションより長く生きるためである。CLAUDECODE と異なり、この変数は IDE 拡張ではなく Claude Code 自身がサブプロセスを起動するときにのみ設定されるため、ネストされたセッションを IDE 統合ターミナルで起動したトップレベルの claude と確実に区別できる。この方法で起動されたネストされた対話型 claude TUI は、--resume、--continue、上矢印キーの履歴、claude agents list から自動的に除外される。非対話型の claude -p セッションは引き続き保存される。この除外を上書きするには CLAUDE_CODE_FORCE_SESSION_PERSISTENCE=1 を設定する。Claude Code v2.1.172 以降が必要

使い方・用途

  • Claude Code 自身が Bash や hook などで起動したサブプロセス内でのネストしたセッションを、トップレベルのセッションと確実に区別したい場合に使う
  • Claude が起動した対話セッションのトランスクリプトが --resume の履歴リストを埋めてしまうのを防ぎたい場合に使う
  • IDE 拡張が統合ターミナルで設定する CLAUDECODE とは異なり Claude Code 自身が設定する変数なので、IDE 統合ターミナルで直接起動されたトップレベルの claude とネストしたセッションを判別したい場合に使う
英語原文(公式ドキュメントより)

Set to 1 in subprocesses Claude Code spawns via the Bash, PowerShell, and Monitor tools, hook commands, and status line commands. Not set for stdio MCP server subprocesses, which are long-lived and outlive the session that spawned them. Unlike CLAUDECODE, this is only set by Claude Code itself when it launches a subprocess and not by IDE extensions, so it reliably distinguishes a nested session from a top-level claude launched in an IDE-integrated terminal. A nested interactive claude TUI started this way is automatically excluded from --resume, --continue, up-arrow history, and the claude agents list. Non-interactive claude -p sessions still persist. Set CLAUDE_CODE_FORCE_SESSION_PERSISTENCE=1 to override this exclusion. Requires Claude Code v2.1.172 or later

関連ドキュメント