# CLAUDE_CODE_SESSION_ID

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

**Field:** Artificial Intelligence  
**Category:** Claude Code  
**Also known as:** $CLAUDE_CODE_SESSION_ID

## Think of it like

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

## 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

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

## Example code

```bash
echo "$(date) $CLAUDE_CODE_SESSION_ID done" >> ~/sessions.log
```

Log which session ran a step.

## Why it matters

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

## Common mistake

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

## Three principles

- **Current session id.** Set for you.
- **Matches hook input.** session_id.
- **Changes on /clear.** Not for MCP.

## Related terms

- [CLAUDECODE](https://learn.benibix.com/term/ai/claudecode-variable)
- [CLAUDE_PID](https://learn.benibix.com/term/ai/claude-pid-variable)
- [--session-id Flag](https://learn.benibix.com/term/ai/session-id-flag)

![Which variables Claude Code sets in a Bash command, a hook and an MCP server it starts.](https://learn.benibix.com/diagrams/env-subprocess-vars.svg)

---

Source: https://learn.benibix.com/term/ai/claude-code-session-id-variable
Author: Behnia Zarei (a developer in Vienna, Austria, who builds and teaches data, AI and business intelligence at BENIBIX)
Publisher: BENIBIX Learn (https://learn.benibix.com/)
Last updated: 2026-11-16
Licence: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Quote freely with attribution.
