agentsclimarketplace

Strip claudecode env for nested spawn

Skill kjuhwa/skills-hub/skills/agent-sdk/strip-claudecode-env-for-nested-spawn

When spawning a Claude Agent SDK subprocess from within another Claude Code / Agent SDK context, the SDK's nesting guard will reject the child unless you strip CLAUDECODE from the inherited env.From its SKILL.md

Install
npx -y skills add kjuhwa/skills-hub --skill strip-claudecode-env-for-nested-spawn

Assembled from the repository path, not quoted from the project. Check it against their README if it does not work.

One thing to look at

  • 0 stars0 stars. Stars are a popularity signal and not a quality one, but at this level it is likely that nobody has read this closely except its author, and you would be relying on your own review.

SKILL.md

2.3 KB, 436 tokens by cl100k_base, as published. Nobody here has run it

Strip CLAUDECODE env before spawning Claude SDK subprocess

When to use

  • You're building tooling (CLI, dev harness, test runner) that sometimes runs inside a Claude Code / Agent SDK session and spawns a server that ALSO uses the Claude Agent SDK.
  • The child process silently refuses to start, or the SDK throws an obscure "nesting detected" error.

How it works

The Claude Agent SDK sets CLAUDECODE=1 in its subprocess env. A nesting guard inside the SDK checks for this var and refuses to run to prevent infinite spawn loops. When you legitimately need to run an SDK subprocess from within an SDK session (e.g. bun run scripts/server.ts from Claude Code running a CLI), strip the var before spawn:

const { CLAUDECODE: _drop, ...parentEnv } = process.env;
spawn([...], { env: { ...parentEnv, /* your overrides */ } });

Example

// apps/cli/src/server-spawner.ts
const { CLAUDECODE: _, ...parentEnv } = process.env;
const proc = Bun.spawn(['bun', 'run', serverEntry], {
  env: {
    ...parentEnv,
    CRAFT_SERVER_TOKEN: token,
    CRAFT_RPC_PORT: '0',
  },
  stdout: 'pipe',
  stderr: 'pipe',
});

Gotchas

  • You may also need to strip CLAUDE_CODE_SSE_PORT, CLAUDE_CODE_OAUTH_TOKEN, or other CLAUDE_* vars depending on SDK version - check SDK release notes.
  • Don't blindly copy all env - the parent's CRAFT_SERVER_URL or debug flags may be inappropriate for the child.
  • This is NOT a security-relevant strip (the guard is for loop prevention); the token is still the real auth boundary.
  • After stripping, test both code paths: inside and outside a Claude session. Tests that only pass in one are a trap.
  • Document why you're doing it - a comment saves the next maintainer an hour.

What ships with it

Read from the repository

Just SKILL.md. No reference files, no scripts.

Keep looking

Skills are one crate of 326,144. Ordering is by how many stacks a row turns up in, so the top of any crate is what has actually been picked rather than what has the most stars.