OmniscioGuidesA SessionStart context hook
A Claude Code SessionStart hook that loads project context
Every session starts with Claude working out where it is: which branch, what changed, what the last few commits were. It can run those commands itself, which costs a turn and a round of tool calls, or a hook can put the answers in front of it before the first prompt.
Checked against Anthropic’s Claude Code hooks documentation on November 25, 2026. By the Omniscio team. Updated .
The hook, complete
Save the script to .claude/hooks/session-context.sh in the project and run chmod +x .claude/hooks/session-context.sh. It prints plain text, and Claude Code adds a SessionStart hook’s plain-text stdout to Claude’s context. Everything it calls — git, printf, wc — is already on the machine; the JSON form below adds one dependency, jq.
Then put the block in .claude/settings.json at the project root. If that file already has a "hooks" object, add the SessionStart array inside it; if it already has a SessionStart array, add this entry to it. A second top-level "hooks" key is not valid JSON, and an unreadable settings file gives you no hooks at all.
#!/bin/bash
set -u
cd "${CLAUDE_PROJECT_DIR:-$(pwd)}" 2>/dev/null || exit 0
git rev-parse --is-inside-work-tree >/dev/null 2>&1 || exit 0
branch=$(git branch --show-current 2>/dev/null)
dirty=$(git status --porcelain 2>/dev/null | wc -l | tr -d ' ')
printf 'Current branch: %s\nUncommitted files: %s\nRecent commits:\n' \
"${branch:-detached HEAD}" "$dirty"
git log --oneline -5 2>/dev/null
exit 0{
"hooks": {
"SessionStart": [
{
"matcher": "startup|resume",
"hooks": [
{
"type": "command",
"command": "\"$CLAUDE_PROJECT_DIR\"/.claude/hooks/session-context.sh",
"timeout": 10
}
]
}
]
}
}On Windows the shell that runs this is Git Bash, so the script needs Git for Windows; without it the command falls back to PowerShell and a .sh file will not start. If you would rather stay in PowerShell, set "shell": "powershell" on the handler and rewrite the script.
If you also want the compacted conversation to get the context back, add compact to the matcher, or run a second block for it. Anthropic’s guide gives that as its own example, because compaction is where the details that mattered get summarised away.
The problem it solves
A fresh session knows the files and nothing else. Which branch it is on, whether the tree was already dirty when it arrived, what the last commit did — all of it is one command away, and all of it costs a turn to discover.
Worse than the cost is the failure: Claude guesses. It edits a file believing it is on a feature branch, or writes a summary of work it thinks was already committed. Context injected before the first prompt removes the guess, and unlike an instruction in CLAUDE.md it is written fresh each time.
What each line does
| Part | Job |
|---|---|
"SessionStart" | The event: it runs when a session starts, before the first prompt — on a new session, a resume, a /clear, a compaction or a fork, with the matcher choosing which. |
"matcher": "startup|resume" | Filters on how the session started, not on a tool. startup is a new session, resume is --resume, --continue or /resume. |
"type": "command" | Only command and mcp_tool handlers are supported on this event. |
cd "${CLAUDE_PROJECT_DIR:-$(pwd)}" | Runs the git commands at the project root, whatever directory the session opened in. |
git rev-parse --is-inside-work-tree | Exits quietly when the folder is not a repository, so the hook is harmless anywhere. |
git branch --show-current | Empty on a detached HEAD, which is why the printf falls back to a label rather than leaving a blank. |
plain printf on stdout | Claude Code adds a SessionStart hook’s plain-text stdout to Claude’s context. No JSON is needed for text alone. |
timeout: 10 | Seconds. The default is ten minutes, and a hook that slow would hold the session’s first response for ten minutes. |
The JSON form, which can also name the session
Plain text is enough for context. The JSON form is worth the extra lines when you want to set the session title as well, which makes a list of sessions readable at a glance — the branch name is usually the title you wanted. It needs jq: brew install jq on macOS, sudo apt-get install jq on Debian and Ubuntu, or winget install jqlang.jq on Windows.
Use it instead of the last three lines of the script above — keep the shebang, the cd, the repository check and the two variables. Appending it would put plain text and JSON on stdout together, which is exactly the failure described below.
ctx=$(printf 'Current branch: %s\nUncommitted files: %s\nRecent commits:\n%s' \
"${branch:-detached HEAD}" "$dirty" "$(git log --oneline -5 2>/dev/null)")
jq -nc --arg ctx "$ctx" --arg t "${branch:-detached HEAD}" \
'{hookSpecificOutput: {hookEventName: "SessionStart", additionalContext: $ctx, sessionTitle: $t}}'
exit 0Two rules come with it. The object has to be the only thing on stdout — other text around it, such as a shell profile that echoes on startup, stops Claude Code reading it as JSON. And once you print JSON, the plain-text path is gone: the text Claude receives is whatever you put in additionalContext, so a script that prints an object with only sessionTitle adds no context at all.
The context text is capped at 10,000 characters. Past the cap Claude Code writes it to a file in the session directory and gives Claude the path with a preview, so a large output is not lost — it is just no longer in front of Claude. The cap applies to additionalContext, systemMessage, initialUserMessage and plain stdout; sessionTitle is a short label and is not capped.
Which sessions it fires on
The matcher on this event is the reason people think a working hook is broken. It does not filter a tool name; it filters how the session started.
| Matcher | When it fires |
|---|---|
startup | A new session |
resume | --resume, --continue or /resume |
clear | /clear |
compact | Automatic or manual compaction |
fork | A session forked from an existing one |
Omit the matcher and it runs on all of them. That is usually what you want for context, and it matters more than it looks: the hook runs again on a resume with source set to "resume", so the branch and commit list are refreshed rather than replayed from the old turn.
How to test that it fires
- Run the script by hand from the project root. It never reads the event, so nothing needs to be piped in:
.claude/hooks/session-context.sh
For the JSON form, run it the same way and check the output parses:.claude/hooks/session-context.sh | jq .should print one object withadditionalContextandsessionTitle. You should see the branch, a count and up to five commits — a new repository has fewer. Nothing printed means thecdor the repository check failed.Permission deniedmeans thechmodabove was skipped. - Check the hook is registered. Type
/hooks: a read-only browser that lists each hook under its event and says where it comes from. - Start a session and look at its first reply. Ask something that depends on the branch, such as “what am I working on?”. Claude should answer without running
git statusitself, which is the visible proof the context arrived. - Read the debug log when it does not. Start with
claude --debug-file /tmp/claude.log, or run/debugmid-session. A hook that fails to start leaves the interpreter’s message there.
A context hook never prints anything in the conversation, because the text goes into the context rather than into a message. Silence is success here, so the transcript is the wrong place to look.
What makes this hook fail
1. The matcher is startup and you resumed
The most common report: the hook worked once and never again. A resumed conversation fires the resume matcher, so a block that named only startup is not eligible. Omit the matcher unless you have a reason to narrow it.
2. It prints JSON without additionalContext
A script that emits {"hookSpecificOutput":{"hookEventName":"SessionStart"}} is valid, exits 0, and adds nothing, because the plain-text path does not apply once the output is JSON. If the title changes and the context does not, this is why.
3. Something else prints to stdout
For a hook that is not async, stdout counts as JSON only when it is one JSON object with nothing around it. A shell profile that echoes a welcome line breaks that: output that does not start with { is read as plain text, so the object is ignored. Output that starts like JSON and is not valid JSON is the other case, a parse failure, and there the notice carries the parse or validation message. The debug log records Hook output does not start with { when that is what happened.
4. It is slow, and the session waits
At an interactive launch the hooks run in the background and you can type immediately, but Claude’s first response waits for them to finish, and so does a prompt you send in the meantime. A git fetch, an npm install or an API call in this hook is felt as a session that takes seconds to start. Keep it to local reads.
5. You added async, and the text stopped appearing
The hook above is synchronous, so this one cannot bite it. If you add "async": true for speed, Claude Code skips the hook’s output when the additionalContext and systemMessage text matches what it last received from that hook, unless a synchronous SessionStart hook has added other text since. It delivers the text again after a compaction or a /clear. Static text therefore appears once and then not again — fine for a reminder, wrong for a context hook, which is why this recipe leaves async off.
Where Omniscio fits
A context hook is a project file: commit .claude/settings.json and every teammate starts with the same picture, which is worth more than any one person’s configuration. Omniscio takes the other angle. Its Hooks panel composes the hooks it ships, and any you define, into every session it spawns, keeping them in its own database rather than writing into your global Claude configuration, with a global switch and per-project overrides.
The hooks examples hub collects the other recipes, the Claude Code hooks guide covers every event, and the hooks builder assembles a block like this one. Omniscio runs on Windows, macOS and Linux, and the pricing page covers the plans.
How this page was written
Everything about the event, its matchers, supported handler types, input fields, plain-text and JSON output, the context cap, timing and async limits was read from Anthropic’s Claude Code hooks reference and its hooks guide on November 25, 2026, quoted from those pages as they read today.
The script was run on this machine against a real repository. No live Claude Code session was configured to run it, so the page describes the documented behaviour of the block and the measured behaviour of the script.
Sources
- Anthropic’s Hooks reference, for
SessionStart, its matchers, handler types, input fields, decision control, the context cap and async behaviour, read November 25, 2026. - Anthropic’s Automate actions with hooks, for the re-inject-context-after-compaction example and the advice to prefer CLAUDE.md for static context, read November 25, 2026.
Common questions
How do I give Claude context at the start of a session?
Use a SessionStart hook. Anything a command hook prints to stdout as plain text is added to Claude’s context at the start of the conversation, before the first prompt. For more than text, print one JSON object with hookSpecificOutput.additionalContext.
Why does it not run on resume?
The matcher decides when it fires: startup for a new session, resume for --resume, --continue or /resume, clear, compact and fork. A hook with matcher startup never fires on a resumed conversation. Omit the matcher, or list the ones you want.
Can it block anything?
No. SessionStart is a context-only event: Anthropic lists it with additionalContext and no decision control, and an exit code of 2 there shows stderr to the user without stopping the session. Only command and mcp_tool handlers are supported.
A hook or CLAUDE.md?
CLAUDE.md for anything that never changes, a hook for anything computed. Anthropic’s documentation says to use CLAUDE.md for static context that does not need a script, and warns that SessionStart runs on every session, so its hooks must stay fast — the matcher narrows that to the sessions you listed.
Can it set the session title?
Yes, with hookSpecificOutput.sessionTitle, which has the same effect as /rename and applies when the source is startup, resume or fork. A hook can check the session_title input field first if it should not overwrite a title you set yourself.
Every session starts oriented
Omniscio runs your Claude Code sessions side by side and composes the hooks it ships, and any you define, into every one it spawns, so a context rule does not depend on which folder you opened.
The features page covers what else it does, pricing covers the plans, and you can download it for Windows, macOS or Linux.