Why Hooks
LLMs are probabilistic β prompt-based instructions drift under token pressure. Agents skip steps, forget constraints, and ignore guardrails. Hooks solve this by moving critical logic out of the prompt and into deterministic code that intercepts every tool call, the same way servlet filters intercept HTTP requests:- Safety β Block dangerous operations before they execute. A
Blockdecision short-circuits immediately and cannot be overridden by later hooks. - Observability β Log every tool call, capture timing data, and feed traces into Agent Journal for behavioral analysis.
- Steering β Modify tool inputs in flight. Subsequent hooks see the modified input, so transformations chain cleanly.
How It Works
Hooks intercept at two points in the tool-call lifecycle:Decision Model
HookDecision is a sealed type with four variants:
Multiple hooks execute in priority order (default: 100, lower = earlier).
AfterToolCall hooks fire in reverse priority order for proper cleanup semantics.
Modules
Event Hierarchy
The event system is open (unsealed) β you can define custom events for your runtime:Quick Start
Write Once, Run Anywhere
The sameAgentHookProvider works on all three runtimes β this is the core value proposition.
@Component bean, auto-configuration handles the rest:
HookRegistry:
HookOutput.
Tool call duration is tracked via wall-clock timing across the pre/post hook boundary.
Each Claude session gets its own HookContext for isolated state and history.
Gemini CLI β stateless subprocess dispatcher reads JSON from stdin:
HookEvent records.
HookContext is fresh per invocation β stateless hooks (security gates, audit logging) work out of the box.
Note: Gemini BeforeTool can only allow or block β Modify is downgraded to allow with a warning.
Documentation
Source Code
Core API, Spring AI adapter, Claude adapter, and Gemini adapter
Design Notes
Architecture decisions, event hierarchy, dispatch semantics
Used By
- Agent Workflow β hooks apply automatically to any workflow step that invokes tools
- Agent Journal β hook provider that logs tool-call events to a journal Run