Skip to content

Feature: Expose session ID as $CLAUDE_SESSION_ID environment variable #25642

Description

@praveenaki

Problem

Claude Code agents have no way to programmatically determine their own session ID. This matters when:

  1. 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
  2. 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
  3. 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    duplicateThis issue or pull request already exists

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions