claude-code - 💡(How to fix) Fix Expose CLAUDE_SESSION_ID as environment variable in tool execution context [1 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#47018Fetched 2026-04-13 05:43:41
View on GitHub
Comments
1
Participants
2
Timeline
5
Reactions
0
Timeline (top)
labeled ×4commented ×1

Fix Action

Fix / Workaround

Custom skills and hooks that write per-session files (e.g., session logs, caches, temp state) need a stable unique key to avoid collisions when multiple sessions run concurrently. Today, workarounds like {repo}-{branch} are fragile — two sessions in the same repo on the same branch will collide.

Code Example

# Only these Claude env vars exist:
$ env | grep CLAUDE
CLAUDECODE=1
CLAUDE_CODE_ENTRYPOINT=cli
CLAUDE_CODE_EXECPATH=C:\Users\Chad\.local\bin\claude.exe

# No session ID:
$ echo "$CLAUDE_SESSION_ID"
(empty)

# PID chain is broken (MinGW on Windows):
$ echo "$$=$$ PPID=$PPID"
$$=1338 PPID=1

# But the Claude process and session data exist:
$ tasklist /FI "PID eq 57116"
claude.exe  57116  Console  1  636,128 K

$ cat ~/.claude/sessions/57116.json
{"pid":57116,"sessionId":"736848ea-e359-4f8c-9e44-7f2dea2ad9b1",...}

---

# Desired behavior:
$ echo "$CLAUDE_SESSION_ID"
736848ea-e359-4f8c-9e44-7f2dea2ad9b1
RAW_BUFFERClick to expand / collapse

Problem

Tools and hooks invoked within a Claude Code session have no reliable way to identify which session they belong to at the shell level. While ${CLAUDE_SESSION_ID} string substitution was added for skill prompt text in v2.1.9, the session ID is not available as an environment variable in the Bash tool execution context or in hook shell commands.

Current state (v2.1.104)

MechanismAvailable?Notes
$CLAUDE_SESSION_ID env var in Bash toolNoecho $CLAUDE_SESSION_ID returns empty
${CLAUDE_SESSION_ID} in skill prompt textYes (since 2.1.9)String substitution only — not accessible from shell commands
Shell PID ($$) mapping to Claude PIDNo$$ returns a child bash PID; $PPID shows 1 (orphaned) on Windows/MinGW
Session JSON files in ~/.claude/sessions/YesContains sessionId + pid, but tools can't discover which file is theirs
snapshot-unknown.txtBrokenWrites session=unknown — cannot resolve session ID

Evidence

# Only these Claude env vars exist:
$ env | grep CLAUDE
CLAUDECODE=1
CLAUDE_CODE_ENTRYPOINT=cli
CLAUDE_CODE_EXECPATH=C:\Users\Chad\.local\bin\claude.exe

# No session ID:
$ echo "$CLAUDE_SESSION_ID"
(empty)

# PID chain is broken (MinGW on Windows):
$ echo "$$=$$ PPID=$PPID"
$$=1338 PPID=1

# But the Claude process and session data exist:
$ tasklist /FI "PID eq 57116"
claude.exe  57116  Console  1  636,128 K

$ cat ~/.claude/sessions/57116.json
{"pid":57116,"sessionId":"736848ea-e359-4f8c-9e44-7f2dea2ad9b1",...}

Use case

Custom skills and hooks that write per-session files (e.g., session logs, caches, temp state) need a stable unique key to avoid collisions when multiple sessions run concurrently. Today, workarounds like {repo}-{branch} are fragile — two sessions in the same repo on the same branch will collide.

The skill prompt substitution (${CLAUDE_SESSION_ID}) partially helps, but requires the model to relay the value into shell commands, which is indirect and unreliable for hooks that run without model mediation.

Proposed solution

Set CLAUDE_SESSION_ID as an environment variable in the tool/hook execution context, matching the sessionId from the session JSON. This would join the existing CLAUDECODE, CLAUDE_CODE_ENTRYPOINT, and CLAUDE_CODE_EXECPATH variables.

# Desired behavior:
$ echo "$CLAUDE_SESSION_ID"
736848ea-e359-4f8c-9e44-7f2dea2ad9b1

Secondary fix

The snapshot file (~/.claude/sessions/snapshot-unknown.txt) should write the actual session ID instead of session=unknown.

Environment

  • Claude Code v2.1.104
  • Windows 11 Pro (MinGW64 bash)
  • Also likely affects macOS/Linux (PID mapping may work better there, but env var is still missing)

extent analysis

TL;DR

Set the CLAUDE_SESSION_ID environment variable in the tool/hook execution context to the sessionId from the session JSON.

Guidance

  • Identify the session JSON file corresponding to the current process by matching the pid in the JSON file with the process ID of the Claude process.
  • Read the sessionId from the identified session JSON file and set it as the CLAUDE_SESSION_ID environment variable.
  • Update the snapshot file (~/.claude/sessions/snapshot-unknown.txt) to write the actual session ID instead of session=unknown.
  • Verify that the CLAUDE_SESSION_ID environment variable is set correctly by echoing its value in the tool/hook execution context.

Example

# Assuming the session JSON file is in ~/.claude/sessions/
SESSION_JSON_FILE=$(find ~/.claude/sessions/ -name "*.json" -exec grep -l "pid":57116 {} \;)
SESSION_ID=$(jq -r '.sessionId' "$SESSION_JSON_FILE")
export CLAUDE_SESSION_ID="$SESSION_ID"
echo "$CLAUDE_SESSION_ID"

Notes

The proposed solution relies on the existence of the session JSON file and the ability to match the pid in the JSON file with the process ID of the Claude process. This may not work if the pid mapping is broken or if the session JSON file is not available.

Recommendation

Apply the proposed workaround by setting the CLAUDE_SESSION_ID environment variable in the tool/hook execution context, as this provides a reliable way to identify the session ID in the shell level.

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