Skip to content
mentoor

Topic 4 of 6 · 5 min read

Injecting context at session start and prompt submit

Goal: Decide which facts a session-start or prompt-submit hook should inject and cut an injection that wastes context on every turn.

A context hook adds a few facts the agent could not know on its own, once, and keeps them short, because everything you inject takes space on every turn.

Check yourself · 1 of 3

A session-start hook is best used for which kind of fact?

Choose one answer

Why it matters

A coding agent starts each session knowing your code only as far as it reads it. It does not know which branch you are on, which ticket you are working on, or that the staging database is read-only today. You can type this each time, or a hook can add it. In Claude Code, text a SessionStart or UserPromptSubmit hook prints to standard output (with exit code 0) is added to the context. The model sees it as if you had written it.

How to do it

  1. Use SessionStart for facts that hold for the whole session: the git branch, the ticket id parsed from the branch name, the Node version, the open pull request.
  2. Use UserPromptSubmit for facts that change per prompt, such as a reminder when the prompt mentions "deploy". It runs on every prompt, so keep it small and quick.
  3. Keep each injection to a few lines. Prefer pointers over content: "Ticket PAY-142; spec at docs/pay-142.md" beats pasting the whole spec.
  4. Inject only what is true and current. Compute it in the hook, do not hard-code it.
  5. Put stable, permanent rules in the memory file. Hooks are for things that change.

Before and after

Before: a prompt hook prints the full contents of CONTRIBUTING.md, 2,000 words, on every prompt. After ten turns the same text sits in the context ten times, and the useful instructions are buried.

After: a session-start hook prints three lines.

Branch: feature/PAY-142-refund-limits
Ticket: PAY-142 (spec: docs/pay-142.md)
Tests: run "npm run test:unit"; integration tests need the VPN.

What goes wrong

  • Injecting secrets. Anything printed is stored in the transcript and sent to the model provider. Never print tokens or passwords.
  • Slow hooks. A prompt hook that calls a remote API makes every prompt wait. Cache the result or move it to session start.
  • Stale facts. A hook that prints yesterday's cached branch sends the agent to the wrong place.
  • Untrusted text. A ticket title written by a stranger can contain instructions. Treat injected external text as data, and keep it out of anything that decides permissions.

Examples

Branch and ticket at session start

#!/usr/bin/env bash
branch=$(git rev-parse --abbrev-ref HEAD 2>/dev/null) || exit 0
ticket=$(echo "$branch" | grep -oE '[A-Z]+-[0-9]+' | head -n1)
echo "Branch: $branch"
[ -n "$ticket" ] && echo "Ticket: $ticket (spec in docs/$ticket.md if present)"

Two lines, computed fresh, no secrets.

Cut the wasteful injection

A prompt hook prints the last 50 lines of git log on every prompt. After 20 turns that is 1,000 lines of repeated history. Fix: print the current branch and the last three commit subjects once at session start, and drop the prompt hook.

Key points

  • Use session-start hooks for facts that hold all session, and prompt-submit hooks only for per-prompt reminders.
  • Inject pointers and a few lines, not whole documents, because the text stays in context.
  • Compute facts at run time so they are current, and keep permanent rules in the memory file.
  • Never print secrets; injected output enters the transcript and goes to the model provider.
  • Keep prompt-submit hooks fast, since the user waits for them on every turn.

Common mistakes

  • Injecting a long document on every prompt.
  • Hard-coding facts that change, such as the branch name.
  • Printing credentials or customer data into the context.
  • Slowing every prompt with a network call.

Check yourself · 2 of 3

A prompt-submit hook prints a 2,000-word guide on every prompt. What is the main cost?

Choose one answer

Check yourself · 3 of 3

Which injected line is the best use of context?

Choose one answer

Practice items, not counted towards your mastery.

Start here