brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: a UserPromptSubmit hook adds fresh context to every Claude Code prompt

Claude
~/tips/tip-claude-userpromptsubmit-hook-context

"hooks": {

Runs a command each time you send a prompt and gives its output to Claude as context.

Some facts change during a session, like the time or the branch you’re on. A hook can hand them to Claude with every message, so you don’t have to.

{
  "hooks": {
    "UserPromptSubmit": [
      {
        "hooks": [
          { "type": "command", "command": "date '+Local time: %Y-%m-%d %H:%M'" }
        ]
      }
    ]
  }
}

The hook runs when you submit a prompt, before Claude processes it. For this event, plain text on standard output with exit code 0 is added to Claude’s context. So Claude now knows the date and time at every turn, which helps for prompts like “what changed since this morning”.

UserPromptSubmit has no matcher, so it fires on every prompt. The hook gets the prompt text as JSON on standard input, so a script can decide to add something only for certain prompts. Exit code 2 blocks the prompt, with your standard error as the reason. For JSON output, the extra text goes in hookSpecificOutput.additionalContext; at the top level it’s ignored.

I think the rule here is simple: keep it fast and small. It runs before each answer, so a slow command delays every message, and the default timeout for this event is 30 seconds. Everything you add costs context on every turn, too. A date line is fine, a full git log is not. For things that only matter at the start, a SessionStart hook is the better fit.

Further reading: Hooks reference, Automate actions with hooks

More Claude tips /recap /focus /skill-doctor /autocompact 500k claude purge ~/work/my-repo --dry-run All Claude tips (83) All topics

Link