What are hooks in an AI coding agent?
Hooks are commands the agent runs automatically at fixed points in its lifecycle. The agent passes the event (which session, which tool, which file) to your command as JSON, and reads JSON back. Depending on the event, your hook can add context for the model, block an action, or simply record what happened.
They're the difference between asking an agent to do something and making sure it happens every time. A line in a rules file asking the agent to run the formatter is a suggestion. A hook that runs the formatter after every edit is a guarantee.
What lifecycle events do hooks fire on?
The names differ by tool, but the moments are the same five.
- Session start. Before your first prompt. The place to load context the agent should always have.
- Prompt submitted. When you send a message. Used to add context or catch things that shouldn't be sent, like secrets.
- Before a tool call. Before a shell command, file read or external call. The hook can allow or block it.
- After a tool call. After a file edit or command. Used to format, lint, run tests or log.
- Turn or session ends. When the agent stops. Used to capture what happened, notify you, or ask for a follow-up.
Which AI coding tools support hooks?
Most of the major ones, as of October 2026. Hook support is moving fast, so treat this as a map and check each tool's docs for exact event names.
| Tool | Where hooks live | Session start | Turn end | Around tools |
|---|---|---|---|---|
| Claude Code | settings.json | SessionStart | Stop | PreToolUse, PostToolUse |
| Cursor | .cursor/hooks.json | sessionStart | stop, afterAgentResponse | Before shell and MCP calls, after file edits |
| Gemini CLI | .gemini/settings.json | SessionStart | AfterAgent | Tool events |
| GitHub Copilot CLI | .github/hooks/*.json | sessionStart | sessionEnd | Prompt and tool events |
| Cline | .clinerules/hooks/ | TaskStart, TaskResume | TaskComplete | PreToolUse, PostToolUse |
| OpenCode | Plugins | session.created | session.idle | Before and after tool runs |
| Codex CLI | config.toml | SessionStart (feature flag) | Stop (feature flag); notify on turn end | In progress |
| Windsurf | .windsurf/hooks.json | Documented at system, user and workspace level; see its docs for events | ||
Checked against each tool's documentation and release notes in October 2026. Cursor's session-start hook doesn't run in its cloud agents; Cline's hooks don't run on Windows; Codex hooks need a recent version with the hooks feature enabled. Tools built on OpenCode can usually use its plugin events too.
What should you use hooks for?
Anything that should happen every time, without relying on the model to remember.
Context at start
Load project conventions, the current branch's goal, or relevant memories before the first prompt.
Guardrails
Block destructive shell commands, writes outside the project, or prompts that contain secrets.
Format and lint
Run the formatter or linter after every edit, so style never depends on the model.
Tests
Run the relevant tests after changes and feed failures back to the agent.
Notifications
Ping you when a long task finishes or the agent is waiting for approval.
Memory
Capture what was decided and learned at the end of each turn, so the next session doesn't start from zero.
How do you give an agent long-term memory with hooks?
Two hooks: one writes, one reads.
- Capture at the end of a turn. When the agent stops, record what happened: decisions, fixes, preferences you stated. Store it outside the session.
- Inject at session start. Before the first prompt, retrieve what's relevant to this project and task, and hand it to the model as context.
- Keep it short and ranked. Send the few memories that matter, not the whole history. Some tools cut off long hook output, and every token is paid for on every call.
- Share it across tools. If you use more than one agent, point their hooks at the same memory, so a decision made in one is known in the others.
Why ranked and short matters: in our tests, a small, well-chosen set of memories reached 97% of the accuracy of giving the model the entire history, at about a fifth of the cost. See long context vs RAG vs memory for the numbers.
How do you write hooks that never break a session?
Treat every hook as code that runs in the critical path of someone's work.
- Never fail the session. Exit cleanly on every path. A crashed hook can surface as an error before the user has typed anything.
- Never block for long. Put a hard time limit on anything synchronous, and move slow work (syncing, indexing) to the background.
- Use absolute paths. Hooks often run with a minimal environment that's missing your shell's PATH. A bare
nodeorpythoncan silently do nothing. - Never prompt. No password or credential prompts in a hook; nobody is there to answer them, and the process hangs.
- Stay inside output limits. Keep injected context well under the tool's limit, and put the most important part first.
- Guard against loops. A turn-end hook that starts a new turn can trigger itself forever. Add a stop condition.
- Don't leak. Hook input can contain prompts, code and file paths. Don't send it anywhere you wouldn't send the code itself.
What about tools without hooks?
Read what they write to disk. Most agents keep a local record of each session. Watching those files picks up new sessions shortly after they happen, and reading them once imports everything you did before you set anything up. It's slower than a hook, but it works with any version, and it's also how you bring in past history even for tools that do have hooks.
Frequently asked questions
What are hooks in AI coding agents?
Scripts the agent runs automatically at fixed lifecycle points: session start, prompt submitted, before and after tool calls, and when a turn ends. They receive event data as JSON and can add context, block an action or record what happened.
Which AI coding tools support hooks?
As of October 2026: Claude Code, Cursor, Gemini CLI, GitHub Copilot CLI, Cline and Windsurf have documented hooks; OpenCode offers plugin events; Codex CLI has a hooks engine behind a feature flag in recent versions.
What is a SessionStart hook used for?
Injecting context the agent should always have before your first prompt: conventions, recent decisions, relevant long-term memories.
Can a hook block what the agent does?
In most tools, yes. Hooks before a tool call or prompt can return a decision that blocks it, which is how teams stop dangerous commands or keep secrets out of prompts.
How do I give an AI coding agent memory with hooks?
Capture at the end of each turn, inject the relevant part at session start, keep it short and ranked, and make both hooks fail silently.
Memory, wired into your agents' hooks.
Open Agentic Brain and Memory is an open-source, self-hosted memory layer for AI coding tools. It captures what each session learned and brings the relevant part back at the next session start, across tools, on your machine and under your key.
curl -fsSL https://raw.githubusercontent.com/rohithhn/Open-Agentic-Brain-and-Memory/main/install.sh | bash