[Bug Report: Unable to determine issue from minimal input]

Resolved 💬 3 comments Opened Nov 14, 2025 by kitaekatt Closed Nov 18, 2025

Summary

The ClaudeCode SDK's context.is_subagent() method returns False even when code is executing in a subagent context (Task tool delegation). This prevents implementing tool access gating that distinguishes between main Claude and delegated subagents.

Additionally, there is no mechanism to identify which specific subagent is executing code within a given session, preventing per-subagent state tracking or resource limits.

---

Current Behavior

When a subagent (spawned via Task tool) invokes hooks or calls context.is_subagent():

  • Returns: False (indicates main Claude)
  • Expected: True (indicates subagent)

Test Evidence:

Main Claude context.is_subagent(): False ✓ (correct)
Subagent context.is_subagent(): False ✗ (should be True)

This makes it impossible to reliably detect whether code is running in a delegated subagent context.

---

Expected Behavior

1. Fix: is_subagent() Returns True for Task-Delegated Code

Current:

context.is_subagent()  # False (wrong)

Expected:

context.is_subagent()  # True when called from Task subagent

When a hook or any code executes within a Task tool invocation (subagent), context.is_subagent() should return True to indicate it's running in delegated/isolated context rather than main Claude.

2. New Feature: subagent_id() Returns Unique Identifier

Requested New Function:

context.subagent_id()  # Returns: "subagent-uuid-for-this-task"

The function should return:

  • Unique identifier per Task delegation
  • Consistent within a session (same Task = same ID)
  • Different across delegations (Task A ≠ Task B)
  • Format: Something like subagent-<uuid> or Task-provided ID

Use Cases:

  • Track per-subagent state (execution count, resource usage)
  • Log which subagent performed actions
  • Implement per-subagent rate limiting or access control
  • Correlate subagent actions with Task delegations

---

Impact and Use Case

What This Enables

With these SDK features, developers can implement:

  1. Tool Access Isolation: Different tool permissions for main Claude vs subagents

``python
if context.is_subagent():
# Subagent: require skill invocation before tool access
require_skill("tool-access")
``

  1. Subagent Tracking: Monitor which subagents accessed which resources

``python
subagent_id = context.subagent_id()
log(f"Subagent {subagent_id} accessed file: {path}")
``

  1. Resource Limits: Rate limit per subagent

``python
subagent_id = context.subagent_id()
if subagent_id not in rate_limiter:
rate_limiter[subagent_id] = RateLimiter()
rate_limiter[subagent_id].check()
``

Current Problem

Without reliable subagent detection:

  • Cannot distinguish main Claude from subagents in hooks
  • Cannot implement tool access gating based on execution context
  • Cannot track per-subagent behavior or resources
  • Subagent isolation defaults to being open/unsafe

---

Investigation Results

Signals Investigated

We attempted to detect subagents using multiple environmental signals, all of which failed:

| Signal | Main Value | Subagent Value | Result |
|--------|-----------|-----------------|--------|
| context.is_subagent() | False | False ❌ | NO DIFFERENCE |
| session_id | Variable | Sometimes different, sometimes same | UNRELIABLE |
| claude_project_dir | ~/.claude | ~/.claude ❌ | IDENTICAL |
| cwd | ~/.claude | ~/.claude ❌ | IDENTICAL |
| transcript_path | ...projects/-home-christina--claude/... | ...projects/-home-christina--claude/... ❌ | IDENTICAL |
| VIRTUAL_ENV env var | Not set | Not set ❌ | NOT SET |
| CLAUDE_PROJECT_DIR env var | Not set | Not set ❌ | NOT SET |
| sys.path | Standard Python paths | Standard Python paths ❌ | IDENTICAL |

Key Finding: The Task tool does not create any environmental isolation. Both main Claude and subagents share identical contexts, paths, and environments, making differentiation impossible without SDK support.

Debug Output

From detection hook logs:

Signal 0 (is_subagent method): False vs False → INCONCLUSIVE
Signal 1 (transcript_project_encoding): -home-christina--claude vs -home-christina--claude → INCONCLUSIVE
Signal 2 (claude_project_dir): ~/.claude vs ~/.claude → INCONCLUSIVE
Signal 3 (cwd): ~/.claude vs ~/.claude → INCONCLUSIVE

Questions for ClaudeCode Team

  1. Is there any current mechanism in the SDK to detect Task tool invocation context?
  2. Can the Task tool set a flag in the hook context when spawning subagents?
  3. Would it be feasible to provide both is_subagent() fix and subagent_id() function?
  4. Are there planned features for subagent isolation that would address this?

View original on GitHub ↗

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