claude-code - 💡(How to fix) Fix [FEATURE] Add agent instance ID headers to API requests [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#49204Fetched 2026-04-17 08:47:57
View on GitHub
Comments
2
Participants
2
Timeline
8
Reactions
0
Author
Timeline (top)
labeled ×3commented ×2closed ×1cross-referenced ×1

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

Problem

Claude Code has per-agent instance IDs internally (createAgentId() in AgentTool.tsx, stored in AsyncLocalStorage), but these are not included in outgoing API requests. This makes it impossible for proxies and observability tools to:

  • Distinguish subagent requests from main agent requests
  • Group requests by agent instance (parallel same-type agents get merged)
  • Build agent invocation trees (who spawned whom)

Currently the only way to identify subagents is fragile substring matching on system prompt content, which breaks on prompt changes and fails entirely for anonymized/stripped traces.

Proposal

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 runWithAgentContext async local storage (agentId, parentSessionId, invokingRequestId). They just need to be forwarded to the API client layer.

Impact

This would enable deterministic agent identification for any proxy or logging tool sitting between Claude Code and the Anthropic API — no heuristics, no prompt parsing, works for anonymized traces.

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 deterministic agent identification.

Guidance

  • Identify the createAgentId() function in AgentTool.tsx and verify it generates unique IDs for each agent instance.
  • Modify the API client layer to include the x-claude-code-agent-id and x-claude-code-parent-agent-id headers in outgoing requests, using the agentId and parentSessionId values from the runWithAgentContext async local storage.
  • Test the modified API client to ensure the new headers are correctly included in requests.
  • Verify that proxies and observability tools can successfully distinguish subagent requests and group them by agent instance using the new headers.

Example

// Example of adding headers to an API request
const agentId = asyncLocalStorage.get('agentId');
const parentSessionId = asyncLocalStorage.get('parentSessionId');

const headers = {
  'x-claude-code-agent-id': agentId,
  'x-claude-code-parent-agent-id': parentSessionId || '', // empty string for main agent
};

// Include headers in API request
fetch(apiUrl, { headers });

Notes

This solution assumes that the createAgentId() function generates unique IDs for each agent instance and that the runWithAgentContext async local storage contains the necessary agentId and parentSessionId values.

Recommendation

Apply workaround: Add the proposed headers to outgoing API requests, as this will enable deterministic agent identification without requiring significant changes to the existing system.

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

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