Hook
Attach automation to lifecycle points in your agentic workflow so that checks, formatting, and bookkeeping happen without anyone remembering to do them.
Understand This First
- Harness (Agentic) – the harness provides the lifecycle points where hooks attach.
Context
At the agentic level, a hook is automation that fires at a specific lifecycle point in an agentic workflow. Hooks let you attach behavior to events (before a file is saved, after a commit is created, when a conversation starts, before a tool is invoked) without modifying the core logic of the agent or harness.
The idea is old. Git hooks, React lifecycle hooks, CI/CD webhooks all work this way: inject custom behavior at defined points without coupling it to the main process. Agentic harnesses adopt the same mechanism.
Problem
How do you enforce conventions, run checks, or trigger side effects at specific points in an agentic workflow without manually intervening every time?
Some tasks should happen automatically: formatting code before a commit, running linters after a file is saved, updating a progress log at the end of a session, or notifying a team channel when an agent completes a major task. Without hooks, these tasks rely on human discipline (remembering to do them) or on the agent’s instructions (hoping it does them). Both are unreliable.
Forces
- Consistency requires that some actions happen every time, without exception.
- Human attention is limited. Remembering to run a formatter or update a log after every change is error-prone.
- Agent instructions are soft constraints. The model may skip steps, especially in long sessions.
- Workflow flexibility: different projects need different automation at different lifecycle points.
Solution
Configure hooks at the appropriate lifecycle points in your agentic harness. Common hook points include:
Pre-commit hooks run before a commit is finalized. They can enforce code formatting, run linters, or check for secrets in the diff. If the hook fails, the commit is blocked.
Post-save hooks run after a file is modified. They can trigger type checking, auto-formatting, or incremental test runs.
Session hooks run when a conversation starts or ends. A start hook might load project context or check the git status. An end hook might update a progress log or summarize what was accomplished.
Tool hooks run before or after a specific tool invocation. A pre-tool hook might validate parameters or check approval policies. A post-tool hook might log the result.
Hooks should be fast, focused, and non-interactive. A hook that takes thirty seconds or asks a human for input has become something else. If the check requires judgment, it belongs in a verification loop, not a hook.
Start with a small set of high-value hooks: a pre-commit linter and a post-session progress log are a good foundation. Add more hooks only when you identify a recurring manual step that should be automated.
How It Plays Out
A team configures a pre-commit hook that runs their linter and type checker. An agent completes a feature, attempts to commit, and the hook catches a type error the agent introduced in its last edit. The agent sees the hook failure, fixes the type error, and commits successfully. The hook caught an error that the agent missed and the human hadn’t yet reviewed.
A developer configures a session-start hook that automatically loads the latest git log and test results into the agent’s context. Every conversation begins with the agent knowing what was last changed and whether the tests are passing, without the developer remembering to provide this information.
“Set up a pre-commit hook that runs the linter and type checker. If either fails, block the commit and show me the errors.”
Consequences
The main benefit is consistency without vigilance. A well-configured hook catches errors early and handles bookkeeping that neither the human nor the agent would reliably remember. The cognitive load drops because routine checks stop being tasks you track and become infrastructure you trust.
The cost is real: configuration, maintenance, and debugging when hooks break. A flaky hook that intermittently blocks commits erodes more trust than it builds. Confusing error messages from a failed hook can send an agent into a Ralph Wiggum Loop, retrying the same broken step without understanding why. Keep hooks fast, reliable, and few. Each one adds friction that compounds.
Related Patterns
- Depends on: Harness (Agentic) – the harness provides the lifecycle points where hooks attach.
- Enables: Verification Loop – hooks automate parts of the verification process.
- Uses: Approval Policy – hooks can enforce approval requirements automatically.
- Enables: Progress Log – hooks can automate log updates.
- Contrasts with: Ralph Wiggum Loop – when a hook fails opaquely, the agent may loop instead of recovering.
Sources
The hook/callback pattern originates in event-driven programming and the observer pattern cataloged by the Gang of Four in Design Patterns (1994). Git hooks brought the concept into version control workflows; Junio Hamano and the Git community formalized the pre-commit/post-commit lifecycle that most developers encounter first. React popularized “lifecycle hooks” in frontend development, extending the idea from infrastructure events to component state transitions. In the agentic context, Claude Code’s hook system (2025) applies the same mechanism to agent lifecycle points: pre-tool, post-tool, session start, and session end.