Feature: Expose session ID as $CLAUDE_SESSION_ID environment variable
Problem
Claude Code agents have no way to programmatically determine their own session ID. This matters when:
- Session mining — An agent wants to query its own session history (e.g., via a SQLite index of sessions) but doesn't know which session ID to filter by
- Parallel sessions — Multiple Claude Code sessions can run simultaneously in the same project directory, so heuristics like "most recently modified JSONL file" are non-deterministic
- Resumed sessions — When a session resumes after context loss (e.g., failed compaction), the agent needs to recover its own history
What we tried (all failed)
| Approach | Why it fails |
|---|---|
| ls -lt ~/.claude/projects/*/*.jsonl \| head -1 | Race condition with parallel sessions |
| lsof -p $PPID \| grep jsonl | Claude Code uses open/append/close — no persistent file handle |
| $CLAUDE_SESSION_ID env var | Doesn't exist |
| Scanning conversation text for a session ID | Could be ANY session ID, not necessarily the current one |
Current workaround
Ask the user to run /status and paste the session ID. Works, but breaks autonomous workflows.
Proposed Solution
Expose the session ID as an environment variable available to all child processes (Bash tool, MCP servers, etc.):
echo $CLAUDE_SESSION_ID
# → 4721a313-bd20-426e-b9c5-24ebcb598a52
This would be analogous to how CLAUDECODE=1 and CLAUDE_CODE_ENTRYPOINT=cli are already exposed.
This issue has 7 comments on GitHub. Read the full discussion on GitHub ↗