Expose AGENT_DEPTH env var to hooks and subagent sessions
Resolved 💬 3 comments Opened Apr 13, 2026 by melvenac Closed Apr 17, 2026
Problem
When dispatching subagents via the Agent tool, there is no deterministic way for hooks, scripts, or skill files to detect whether they are running inside a subagent vs. a top-level session.
This causes infinite loops when plugins (e.g., superpowers) inject "MUST invoke skills" instructions into subagent sessions. The subagent sees /start in the skill list, invokes it, /start dispatches another subagent, and the cycle repeats.
Current workarounds (all non-deterministic)
- Prompt-level instructions in subagent prompts ("do NOT invoke skills") — LLM can ignore
SUBAGENT-STOPdirectives in skill files — requires LLM to parse and obey- Lock files (
.start-lock) — only works for specific command flows, not general subagent detection - Removing imperative text from SessionStart hooks — helps but SessionStart hooks don't fire for subagents anyway, so doesn't address the root cause
What we verified
- SessionStart hooks do NOT fire for Agent-dispatched subagents (confirmed via diagnostic logging)
- Subagents inherit the full system context (CLAUDE.md, plugin system reminders, skill list)
- The superpowers plugin's "MUST invoke skills" instruction gets amplified in subagent context
- No existing environment variable distinguishes subagent from top-level session
Proposed Solution
Expose an AGENT_DEPTH (or CLAUDE_CODE_AGENT_DEPTH) environment variable:
- Top-level session:
AGENT_DEPTH=0(or unset) - First-level subagent:
AGENT_DEPTH=1 - Nested subagent:
AGENT_DEPTH=2, etc.
This should be available:
- In the subagent's environment (so hooks CAN use it if SessionStart is ever enabled for subagents)
- As a system-level context variable injected into the subagent's conversation (so skills and plugins can branch on it)
Impact
This would allow:
- Skills to deterministically skip execution in subagent context (e.g.,
/startrefuses to run when depth > 0) - Plugins to conditionally suppress instructions for subagents
- Hooks to detect subagent sessions if SessionStart is later enabled for them
Environment
- Claude Code v2.1.104
- Windows 10, bash shell
- Superpowers plugin enabled
- Custom SessionStart hooks + slash commands
🤖 Generated with Claude Code
This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗