Claude Code hooks

Claude Code SessionEnd hook: clean up and log when a session ends

A SessionEnd hook runs when a Claude Code session ends. Use it for cleanup, for logging session statistics, or for saving session state. It cannot stop the session from ending.

This page covers the reasons, the short time limit, a script you can copy, and the traps. Every statement about Claude Code comes from the official hooks reference, linked at the end.

When the hook runs

SessionEnd runs when a Claude Code session ends. The per-turn event is Stop. The matcher filters on the reason the session ended. These values exist:

clear
The session was cleared with /clear.
resume
The session was switched through an interactive /resume.
logout
The user logged out.
prompt_input_exit
The user exited while the prompt input was visible.
other
Any other exit reason.

The reference also lists bypass_permissions_disabled, and notes that Claude Code removed it in v2.1.234 and no longer sends it. Drop it from your SessionEnd matchers.

What arrives on stdin

On top of the common fields, a SessionEnd hook receives reason. This is the example from the reference:

stdin
{
  "session_id": "abc123",
  "cwd": "/Users/...",
  "hook_event_name": "SessionEnd",
  "reason": "other"
}

The event has no decision control. A hook cannot block the end of the session. Claude Code discards its JSON output fields, such as systemMessage. Stderr is shown to the user only.

The 1.5-second budget

All SessionEnd hooks share a default budget of 1.5 seconds. It applies when you exit, run /clear, or switch sessions with an interactive /resume. A hook still running at that point is cancelled. You have two ways to give the hooks more time:

  • A timeout on the hook. The overall budget rises to the highest per-hook timeout in your settings files, up to 60 seconds. A hook without its own timeout still keeps the default. Timeouts on hooks that a plugin provides do not raise the budget.
  • CLAUDE_CODE_SESSIONEND_HOOKS_TIMEOUT_MS. Set it in milliseconds to override the budget. The value also becomes the timeout of each hook that has none of its own.
terminal
CLAUDE_CODE_SESSIONEND_HOOKS_TIMEOUT_MS=5000 claude

Before v2.1.268 the variable raised only the overall budget, and a hook without its own timeout was still cancelled after 1.5 seconds.

Example: log every session

The script below appends one line per session to .claude/sessions.log: time, session ID and reason. It runs as it stands: a test in our repository executes this exact code.

1. Add the hook

.claude/hooks/log-session.mjs
import { appendFileSync, mkdirSync, readFileSync } from "node:fs";
import { join } from "node:path";

const input = JSON.parse(readFileSync(0, "utf8"));

const dir = join(input.cwd, ".claude");
mkdirSync(dir, { recursive: true });

const line = [new Date().toISOString(), input.session_id, input.reason].join(" ");
appendFileSync(join(dir, "sessions.log"), line + "\n");

2. Register it

.claude/settings.json
{
  "hooks": {
    "SessionEnd": [
      {
        "hooks": [
          {
            "type": "command",
            "command": "node",
            "args": [
              "${CLAUDE_PROJECT_DIR}/.claude/hooks/log-session.mjs"
            ],
            "timeout": 5
          }
        ]
      }
    ]
  }
}

The hook has its own timeout of 5 seconds, so the shared budget rises to 5 seconds. A write of one line needs far less. Set a timeout like this only when your hook needs it.

Traps

  1. 1.5 seconds is short. A hook that makes a network call or runs a slow command is cancelled before it finishes. Keep the hook to local file writes, or raise the budget as shown above.
  2. Nothing reaches Claude. The session is over, so there is no conversation to add context to. To load notes in the next session, use a SessionStart hook.
  3. Do not rely on a message to the user. Claude Code discards systemMessage on this event. Only stderr is shown to the user.
  4. Plugin hooks cannot raise the budget. If a plugin ships a slow SessionEnd hook, its own timeout does not extend the shared 1.5 seconds. Set the budget in your settings files or with the environment variable.

Sources

Checked against the official docs on .

More: All Claude Code hook events · SessionStart hook · Stop hook · All docs