claude-code - 💡(How to fix) Fix [FEATURE] Expose agent instance ID in API request headers [2 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
anthropics/claude-code#49205Fetched 2026-04-17 08:47:55
View on GitHub
Comments
2
Participants
2
Timeline
7
Reactions
0
Author
Timeline (top)
labeled ×3commented ×2closed ×1cross-referenced ×1

Fix Action

Fix / Workaround

  1. A team runs Claude Code through a recording proxy to track usage and costs
  2. A developer asks Claude to "launch 3 Explore agents in parallel to search different parts of the codebase"
  3. The proxy records 30+ interleaved Haiku requests from the 3 agents
  4. With agent ID headers: each request is tagged to its specific agent instance, enabling per-agent grouping, cost attribution, and conversation reconstruction
  5. Without: all 30 requests merge into one "Explore Agent" blob with no way to separate them

Code Example

x-claude-code-agent-id: <unique ID per agent instance>
x-claude-code-parent-agent-id: <agent ID of the parent, absent for main>
RAW_BUFFERClick to expand / collapse

Preflight Checklist

  • I have searched existing requests and this feature hasn't been requested yet
  • This is a single feature request (not multiple features)

Problem Statement

When building observability/proxy tools that sit between Claude Code and the Anthropic API, there is no way to identify which agent instance a request belongs to. The only option is fragile substring matching on system prompt content, which:

  • Breaks when Claude Code updates its prompts
  • Cannot distinguish parallel instances of the same agent type (e.g., 3 concurrent Explore agents)
  • Fails entirely for anonymized/stripped traces
  • Cannot detect custom user-defined agents

Claude Code already generates per-agent instance IDs internally (createAgentId() in AgentTool.tsx, stored in AsyncLocalStorage via runWithAgentContext), but these are not included in outgoing API requests.

Proposed Solution

Add two headers to outgoing Anthropic API requests:

x-claude-code-agent-id: <unique ID per agent instance>
x-claude-code-parent-agent-id: <agent ID of the parent, absent for main>

The agentId and parent context already exist in the agent context object (agentId, parentSessionId, invokingRequestId). They just need to be forwarded to the HTTP client layer, similar to how x-claude-code-session-id is already sent.

Alternative Solutions

Currently we pattern-match against system prompt text (e.g., "file search specialist" → Explore Agent, "You are an agent for Claude Code" → General Agent). This requires maintaining a list of 13+ fragile substring patterns and still can't handle custom agents or parallel same-type instances.

We also explored hashing the parent's Agent tool_use prompt and matching against child requests' first user message, but this has race conditions, doesn't survive anonymization, and requires cross-request correlation.

Priority

Nice to have - Would improve my workflow

Feature Category

CLI commands and flags

Use Case Example

  1. A team runs Claude Code through a recording proxy to track usage and costs
  2. A developer asks Claude to "launch 3 Explore agents in parallel to search different parts of the codebase"
  3. The proxy records 30+ interleaved Haiku requests from the 3 agents
  4. With agent ID headers: each request is tagged to its specific agent instance, enabling per-agent grouping, cost attribution, and conversation reconstruction
  5. Without: all 30 requests merge into one "Explore Agent" blob with no way to separate them

Additional Context

The agentId already flows through AsyncLocalStorage in runWithAgentContext() with fields including agentId, parentSessionId, agentType, subagentName, isBuiltIn, and invokingRequestId. Exposing even just agentId and parentAgentId as request headers would enable deterministic agent identification for any downstream tool.

extent analysis

TL;DR

Add x-claude-code-agent-id and x-claude-code-parent-agent-id headers to outgoing Anthropic API requests to enable agent instance identification.

Guidance

  • Modify the HTTP client layer to include the agentId and parentAgentId from the agent context object in the request headers.
  • Update the runWithAgentContext function to expose the agentId and parentAgentId as request headers, similar to how x-claude-code-session-id is already sent.
  • Verify that the headers are correctly set by inspecting the outgoing requests or using a proxy tool to track the requests.
  • Test the solution with parallel instances of the same agent type and custom user-defined agents to ensure correct identification.

Example

// Example of how to add the headers to the request
const agentId = AsyncLocalStorage.getStore().agentId;
const parentAgentId = AsyncLocalStorage.getStore().parentSessionId;
const headers = {
  'x-claude-code-agent-id': agentId,
  'x-claude-code-parent-agent-id': parentAgentId,
};
// Add the headers to the outgoing request

Notes

The proposed solution relies on the existing agentId and parentAgentId values being correctly set in the agent context object. If these values are not correctly set, the solution may not work as expected.

Recommendation

Apply the proposed solution by adding the x-claude-code-agent-id and x-claude-code-parent-agent-id headers to outgoing Anthropic API requests, as it provides a deterministic way to identify agent instances and addresses the limitations of the current substring matching approach.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING