Skip to content
BENIBIX Learn

CLAUDE_CODE_SESSION_ID

AI⌨️ Claude CodeConfiguration

Also known as $CLAUDE_CODE_SESSION_ID

It holds the current session's id, for scripts and hooks.

Think of it like

It is like the ticket you get when you wait in line at a shop.

Example

Lucy's hook writes a log line for each edit. Adding the session id lets her group the lines by chat later.

Why it matters

With several sessions running, the id is how you tell their logs apart.

Definition

CLAUDE_CODE_SESSION_ID holds the id of the current session. Claude Code sets it in Bash commands, hooks and local MCP servers. In Bash and hooks it matches the session_id a hook gets, and it changes on /clear. An MCP server keeps the id it started with. Use it to tie logs and tools to one session.

Example code

bash
echo "$(date) $CLAUDE_CODE_SESSION_ID done" >> ~/sessions.log
Log which session ran a step.
The second row: MCP keeps its first id.

Common mistake

Expecting an MCP server to see the new id after /clear. It keeps the one it was started with.

Key takeaways

  1. 1
    Current session id. Set for you.
  2. 2
    Matches hook input. session_id.
  3. 3
    Changes on /clear. Not for MCP.