Skip to content

Comments

fix: filter CLAUDECODE from subprocess environment#594

Open
gspeter-max wants to merge 1 commit intoanthropics:mainfrom
gspeter-max:claude-agent-sdk-python/issue_573
Open

fix: filter CLAUDECODE from subprocess environment#594
gspeter-max wants to merge 1 commit intoanthropics:mainfrom
gspeter-max:claude-agent-sdk-python/issue_573

Conversation

@gspeter-max
Copy link

Fixes #573

Root Cause

CLAUDECODE=1 environment variable leaks into child subprocess when SDK is used from within Claude Code (hooks, plugins, subagents). Child CLI detects this and rejects the session thinking it's a nested session.

Changes

  • Filter CLAUDECODE from parent environment before merging in SubprocessCLITransport.connect()
  • This prevents false nesting detection while allowing users to explicitly set CLAUDECODE via env option if needed
  • Add test_claudecode_env_var_is_filtered to verify CLAUDECODE is filtered
  • Add test_claudecode_can_be_explicitly_set to verify user override capability

Impact

  • Enables SDK usage from within Claude Code sessions (hooks, plugins, subagents)
  • Fixes primary use case for the SDK
  • Unblocks hook-based workflows (guardrails, prompt injection detection, etc.)

Technical Notes

  • CLAUDECODE=1 is a safety check in Claude Code CLI to prevent actual nesting
  • SDK subprocesses are isolated and safe, but the variable causes false positive
  • Implementation filters CLAUDECODE from os.environ before merge
  • Users can still override via env option (user-provided env takes precedence)
  • Approach: filter before merge for clarity and flexibility

Testing

  • Added test case test_claudecode_env_var_is_filtered
  • Added test case test_claudecode_can_be_explicitly_set
  • Verified CLAUDECODE is filtered while other env vars are preserved
  • Verified user can explicitly override CLAUDECODE if needed
  • All 158 existing tests pass

Co-Authored-By: Claude Sonnet 4.6 noreply@anthropic.com

ROOT CAUSE:
CLAUDECODE=1 environment variable leaks into child subprocess when SDK is used
from within Claude Code (hooks, plugins, subagents). Child CLI detects this and
rejects the session thinking it's a nested session.

CHANGES:
- Filter CLAUDECODE from parent environment before merging in SubprocessCLITransport.connect()
- This prevents false nesting detection while allowing users to explicitly set CLAUDECODE via env option if needed
- Add test_claudecode_env_var_is_filtered to verify CLAUDECODE is filtered
- Add test_claudecode_can_be_explicitly_set to verify user override capability

IMPACT:
- Enables SDK usage from within Claude Code sessions (hooks, plugins, subagents)
- Fixes primary use case for the SDK
- Unblocks hook-based workflows (guardrails, prompt injection detection, etc.)

TECHNICAL NOTES:
- CLAUDECODE=1 is a safety check in Claude Code CLI to prevent actual nesting
- SDK subprocesses are isolated and safe, but the variable causes false positive
- Implementation filters CLAUDECODE from os.environ before merge
- Users can still override via env option (self._options.env takes precedence)
- Approach C chosen: filter before merge for clarity and flexibility

FILES MODIFIED:
- src/claude_agent_sdk/_internal/transport/subprocess_cli.py
- tests/test_transport.py

Fixes anthropics#573

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Subprocess inherits CLAUDECODE=1 env var, preventing SDK usage from Claude Code hooks/plugins

2 participants