I keep Claude Code sessions open in several repos at once, and the names in /resume and the terminal tabs don’t say which repo a session is in. The name is a short title Claude Code writes from the first prompt, like “Default apps per file extension”, and the repo isn’t part of it. I wanted every name to start with the repo: [dotfiles] Default apps per file extension.
No setting, two hooks
Claude Code (2.1.337 here) has no option for a prefix. claude --name <name> and /rename set a name by hand, and that name replaces the generated title instead of adding to it. The only thing that can set a name automatically is a hook: SessionStart and UserPromptSubmit hooks may return hookSpecificOutput.sessionTitle, which works like /rename. No other hook event has the field.
The catch was getting hold of the generated title, the part I wanted to keep. Hook input has a session_title field, but only when the session has a custom name, from --name, /rename or a hook. The generated title isn’t passed to hooks at all.
The title is in the transcript
Each session is a JSONL file under ~/.claude/projects/, and the hook input gives its path as transcript_path. Grepping one for the title turned up a line just for it:
{"type":"ai-title","aiTitle":"Default apps per file extension","sessionId":"a5336c73-..."}
Claude Code writes it in the background after the first prompt, sometimes more than once with the same text. Of the 104 sessions on my machine that have one, none ever changed its title, so reading it once loses nothing.
On every prompt the hook checks whether the session already has a name and stops if it does. If not, it works out the repo, reads the last ai-title line and returns [repo] title. After that, session_title is set on every later call and the hook exits at the first check. A name I give with /rename stays for the same reason.
On resume, not on fork
A new session gets its prefix from the second prompt on, because there is no generated title yet when the first one is sent.
A resumed session doesn’t have to wait. With the same script on SessionStart behind a resume matcher, a session is named the moment it opens, so an old session from before the hook gets its prefix too the next time I resume it.
I put it on fork too at first, and the forked session came out unnamed. Logging the hook input showed why: with source: "fork", transcript_path points to the new session’s file, and that file doesn’t exist yet. So the matcher leaves forks out, and a forked session gets its name on a later prompt.
The script
#!/usr/bin/env bash
# Name the session "[<repo>] <generated title>".
input=$(cat)
IFS=$'\037' read -r EVENT CUSTOM TRANSCRIPT CWD < <(printf '%s' "$input" | jq -r '
[ (.hook_event_name // ""),
(.session_title // ""),
(.transcript_path // ""),
(.cwd // "") ] | join("\u001f")')
# Already named (/rename, --name, or this hook on an earlier prompt): keep it.
[ -n "$CUSTOM" ] && exit 0
[ -f "$TRANSCRIPT" ] || exit 0
# The launch root, not a later cd, and the main repo's name, not a worktree's.
dir=${CLAUDE_PROJECT_DIR:-$CWD}
common=$(git -C "$dir" rev-parse --path-format=absolute --git-common-dir 2>/dev/null) || exit 0
case "$common" in
*/.git) repo=$(basename "$(dirname "$common")") ;;
*) repo=$(basename "$(git -C "$dir" rev-parse --show-toplevel 2>/dev/null)") ;;
esac
[ -n "$repo" ] || exit 0
ai_title=$(grep -F '"ai-title"' "$TRANSCRIPT" \
| jq -r 'select(.type == "ai-title") | .aiTitle // empty' 2>/dev/null \
| tail -n 1)
[ -n "$ai_title" ] || exit 0
jq -n --arg event "$EVENT" --arg title "[$repo] $ai_title" \
'{hookSpecificOutput: {hookEventName: $event, sessionTitle: $title}}'
And in ~/.claude/settings.json:
"hooks": {
"UserPromptSubmit": [
{ "hooks": [{ "type": "command", "command": "$HOME/.claude/session-title.sh" }] }
],
"SessionStart": [
{ "matcher": "resume",
"hooks": [{ "type": "command", "command": "$HOME/.claude/session-title.sh" }] }
]
}
--git-common-dir gives the main repo’s name inside a worktree, not the worktree folder’s. $CLAUDE_PROJECT_DIR is the directory the session started in, so a cd into another repo before the name is set doesn’t put the wrong repo in it. Outside a git repo the hook does nothing. The script lives in my dotfiles.
The weak spot is the ai-title line. It isn’t documented, and an update could change or drop it. If that happens nothing breaks: sessions just go back to plain generated titles.