claude-code - 💡(How to fix) Fix Hook event: PreModelResponse (inject context before model plans)

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…

Claude Code's hook system currently has four events: SessionStart, PreToolUse, PostToolUse, and PreCompact. None of these fire between when a user submits a prompt and when the model begins generating its response. This gap prevents injecting pre-computed context that the model needs for its first planning step.

Root Cause

Claude Code's hook system currently has four events: SessionStart, PreToolUse, PostToolUse, and PreCompact. None of these fire between when a user submits a prompt and when the model begins generating its response. This gap prevents injecting pre-computed context that the model needs for its first planning step.

Code Example

{
  "hookSpecificOutput": {
    "hookEventName": "PreModelResponse",
    "additionalContext": "Injected system-level context the model sees before planning."
  }
}
RAW_BUFFERClick to expand / collapse

Summary

Claude Code's hook system currently has four events: SessionStart, PreToolUse, PostToolUse, and PreCompact. None of these fire between when a user submits a prompt and when the model begins generating its response. This gap prevents injecting pre-computed context that the model needs for its first planning step.

Use Case

I maintain a directive/knowledge-base system with gate hooks that enforce loading context-specific directives before code operations. The current flow is:

  1. User submits prompt ("fix the null pointer in FooService")
  2. Model plans and emits a tool call (e.g., Read FooService.java)
  3. PreToolUse hook fires, detects missing Java directives, blocks the call
  4. Model reads the block message, calls load_bundle to load directives
  5. Model retries the original tool call
  6. PreToolUse hook passes (markers now present)

This costs 2-6 extra turns per gate miss (activity directives, tool knowledge, repo context can cascade). Each round-trip burns tokens for the block message, the load call, the response, and the retry.

I've built a gate predictor that can determine what directives a prompt will need before the model sees it, using session history, project context, and keyword analysis. The predictor can call load_bundle and have the content ready. But there's no hook event where I can inject that content into the conversation before the model starts planning.

Proposed Event: PreModelResponse (or PrePromptProcess)

Fires after the user's prompt is submitted and before the model begins generating. The hook receives the prompt text and can return:

{
  "hookSpecificOutput": {
    "hookEventName": "PreModelResponse",
    "additionalContext": "Injected system-level context the model sees before planning."
  }
}

The additionalContext string would appear as a system message in the model's context, similar to how PreToolUse's additionalContext works but positioned before the model's first response to the prompt rather than alongside a tool call.

Why Existing Events Don't Work

  • PreToolUse: fires after the model has already planned and committed to a tool call. The model's initial reasoning doesn't benefit from the injected context. Also requires the model to make a tool call at all; pure-text responses never trigger it.
  • PostToolUse: fires after a tool completes; too late for pre-loading.
  • SessionStart: fires once at session start, not per-prompt.
  • PreCompact: fires before compaction, unrelated.

Impact

Without this event, the only way to pre-load context is the block-retry pattern, which:

  • Wastes 2-6 turns of model output per gate miss
  • Burns tokens on block messages, retry logic, and duplicate tool calls
  • Adds user-visible latency (each blocked call is a full model round-trip)
  • Cannot be optimized away by prediction since there's no injection point

With this event, a hook could pre-satisfy all gates in a single synchronous call before the model starts, eliminating the round-trip overhead entirely.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

claude-code - 💡(How to fix) Fix Hook event: PreModelResponse (inject context before model plans)