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:
| Decision | When | Behavior |
|---|---|---|
Proceed | Default | Tool executes normally |
Block | Safety / policy | Short-circuits immediately — later hooks never run |
Modify | Input transformation | Passes modified input to the next hook in the chain |
Retry | AfterToolCall only | Re-executes the tool (e.g., after transient failure) |
Modules
| Module | What it does | Dependencies |
|---|---|---|
agent-hooks-core | Pure Java 17 API — events, decisions, registry | Zero (portable) |
agent-hooks-spring | Spring AI adapter — wraps ToolCallback with hook dispatch, auto-configures via Boot | Spring AI, Spring Boot |
Event Hierarchy
The event system is open (unsealed) — you can define custom events for your runtime:| Event | Interface | Decisions |
|---|---|---|
BeforeToolCall | ToolEvent | Proceed, Block, Modify |
AfterToolCall | ToolEvent | Proceed, Retry |
SessionStart | HookEvent | Observation only |
SessionEnd | HookEvent | Observation only |
Quick Start
AgentHookProvider and register it as a bean — auto-configuration handles the rest:
Documentation
Source Code
Core API and Spring AI 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