Context: Studying Claude Code source code (backup branch)
How Hook additionalContext Reaches the Model
Data flow
- Hook outputs JSON with
hookSpecificOutput.additionalContext(string). Parsed inhooks.tsper event type (~line 622 for PreToolUse, 644 for PostToolUse, 625 for UserPromptSubmit, etc.) - Aggregated into
additionalContexts[]onAggregatedHookResult(hooks.ts:2787) - Wrapped as attachment message
type: 'hook_additional_context'intoolHooks.ts(PreToolUse: 566-578, PostToolUse: 133-142, PostToolUseFailure: 270-279) - Pushed into
resultingMessagesintoolExecution.ts(PreToolUse at line 846 before tool runs, PostToolUse viahookResultsat line 1585 after tool result) - Converted to API message in
messages.ts:4117-4128: becomes acreateUserMessagewithisMeta: true, content wrapped in<system-reminder>tags
What the model sees
<system-reminder>
PreToolUse:Bash hook additional context: [hook's string output]
</system-reminder>
User-role message, hidden from user UI (isMeta: true), fully visible to the API. Same structural privilege as skills.
Message ordering
[assistant message with tool_use block]
[PreToolUse hook additional context] <-- before tool runs
[tool_result block] <-- actual tool output
[PostToolUse hook additional context] <-- after tool result
[tool's newMessages (e.g. skill content)]
[hook_stopped_continuation, if any]
Both pre and post contexts are recent user-role messages wrapped in <system-reminder>, so the model treats them as authoritative injected instructions.