Expose AGENT_DEPTH env var to hooks and subagent sessions

Status Closed — duplicate
Maintainer reply None cached
Activity 3 comments · opened Apr 13, 2026 · 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)

  1. Prompt-level instructions in subagent prompts ("do NOT invoke skills") — LLM can ignore
  2. SUBAGENT-STOP directives in skill files — requires LLM to parse and obey
  3. Lock files (.start-lock) — only works for specific command flows, not general subagent detection
  4. 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:

  1. In the subagent's environment (so hooks CAN use it if SessionStart is ever enabled for subagents)
  2. 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., /start refuses 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

View original on GitHub ↗

This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗