Expose session_id and context_window usage to the AI model

Open 💬 7 comments Opened Mar 20, 2026 by guillespi

Problem

The AI model running inside Claude Code has no way to know:

  1. Its own session_id
  2. Its current context window usage (percentage, tokens used/remaining)

This information exists in the CLI — it's passed to the statusline script via stdin JSON (session_id, context_window.used_percentage, etc.) — but is never exposed to the model itself.

Why it matters

Skills and workflows (like implementing multiple features in sequence) need to check context usage to decide whether to continue or pause. Currently the model has to guess based on conversation length, which is unreliable (I estimated 85% when the real value was 33%, causing an unnecessary pause).

Current workaround

  1. The statusline script writes context % to a file: ~/.claude/context-usage/{session_id}
  2. The model reads the most recently modified file

This breaks with multiple concurrent sessions because the model doesn't know its own session_id to pick the right file.

Proposed solution

Expose session_id and context_window.used_percentage to the model via one of:

  • Environment variable (CLAUDE_SESSION_ID, CLAUDE_CONTEXT_USED_PCT) set when executing tool calls (Bash, etc.)
  • System message injected into the conversation context (like <system-reminder> tags already are)
  • A dedicated tool the model can call to query session metadata

The simplest would be env vars since the CLI already sets CLAUDECODE=1 in the tool execution environment.

Context

  • The CLI already has all this data (it sends it to statusline scripts)
  • The CLI already injects system-reminders into the conversation
  • The CLI already sets env vars for tool execution (CLAUDECODE=1)
  • Adding CLAUDE_SESSION_ID and CLAUDE_CONTEXT_PCT to the env would be minimal effort

🤖 Generated with Claude Code

View original on GitHub ↗

This issue has 7 comments on GitHub. Read the full discussion on GitHub ↗