guía del comprador
AI Coding Agent Hooks
How lifecycle hooks, matchers, and customization surfaces separate real coding agent platforms. A buyer guide to Cursor hooks.json, Claude Code hooks, and GitHub Copilot custom instructions and preview hooks.
guía del comprador
How lifecycle hooks, matchers, and customization surfaces separate real coding agent platforms. A buyer guide to Cursor hooks.json, Claude Code hooks, and GitHub Copilot custom instructions and preview hooks.
Hooks are the part of a coding agent that decides what it is allowed to do, when it must stop, and what context it must load before it acts. The three leaders expose the idea very differently:
hooks.json to register lifecycle callbacks around agent events, tabs, and terminal/app boundaries.PreToolUse, PostToolUse, beforeShellExecution, SessionStart) to gate tool calls and shell access. See the Claude Code hooks guide and the full hooks reference.Buyer's shortcut: pick Cursor if you want IDE-native event interception; pick Claude Code if you want terminal-first tool governance; pick Copilot if you want prompt-level guardrails inside a GitHub-native workflow. Whichever you choose, deploy the hooks in test repos first and version them like production code. Related: best coding AI agents 2026, coding agent permissions guide, MCP config locations for coding agents.

The word is overloaded. In this article, a hook is a programmable point where a coding agent's default behavior can be inspected, blocked, redirected, or augmented. Hooks answer three questions:
Without hooks, a coding agent is just a chatbot with file access. With hooks, it becomes a governed teammate that can operate inside your repo safely enough to leave running while you grab coffee. That distinction matters because the same model that writes beautiful code can also delete a database, commit secrets, or drift across microservices until the fix costs more than the original task. The OWASP LLM Top 10 maps these risks—prompt injection, insecure output handling, and excessive agency—to the controls hooks provide.

Most engineering leaders spend their evaluation time comparing Claude 4 Sonnet, GPT-4.1, o3, Gemini 2.5 Pro, or whatever shipped last Tuesday. Model quality affects output, but execution control determines whether that output ever reaches production safely. Two teams using the same model can have wildly different risk profiles because one configured hooks and the other did not.
The real buying criteria are:
If a vendor cannot answer those five questions, you are buying autocomplete with ambition, not an agent platform.
hooks.json lifecycle hooksCursor's hook system is the most IDE-native of the three. According to the Cursor Hooks documentation, hooks are defined in hooks.json files at the project level (.cursor/hooks.json) or user level (~/.cursor/hooks.json), and they run as spawned processes that communicate over stdio using JSON. They fire before or after defined stages of the agent loop and can observe, block, or modify behavior.

The Cursor docs group hooks into three categories:
sessionStart, sessionEnd, preToolUse, postToolUse, beforeShellExecution, afterShellExecution, beforeReadFile, afterFileEdit, beforeSubmitPrompt, stop, and others.beforeTabFileRead, afterTabFileEdit.workspaceOpen fires when a workspace opens.A minimal project-level hooks.json can block destructive shell commands and audit every tool call:
{
"version": 1,
"hooks": {
"beforeShellExecution": [
{
"command": ".cursor/hooks/block-destructive.sh",
"matcher": "rm|kubectl delete|drop table"
}
],
"preToolUse": [
{
"command": ".cursor/hooks/audit.sh",
"matcher": "Shell|Read|Write"
}
],
"afterFileEdit": [
{
"command": ".cursor/hooks/format.sh"
}
]
}
}
Because Cursor owns the editor surface, hooks can also inspect UI-level events: tab switches, file opens, or context panel changes. That lets you enforce rules like "any edit to payments/ must load PAYMENTS_README.md y SECURITY_REVIEW.md." Cloud agents also pick up project hooks from .cursor/hooks.json, although some events such as sessionStart y workspaceOpen do not fire in the cloud environment.
hooks.json across projects.Claude Code's hook approach is built for the terminal. The Claude Code hooks guide explains that hooks are user-defined shell commands (or HTTP endpoints, MCP tool calls, prompts, or agent-based verifiers) that run at specific points in the session lifecycle. The full hooks reference documents the JSON input/output schemas and every supported event.

PreToolUse fires before a tool is invoked and can inspect the tool name and arguments. It is the right place to block file writes outside allowed directories or require a confirmation before an edit touches generated code. PostToolUse lets you verify results: did the test pass, did the diff actually apply, does the file still lint? beforeShellExecution is the critical safety gate for any command that hits the shell, which is where most production incidents start. SessionStart is useful for loading required context, pinning model behavior, or printing a safety reminder.
The reference documents many more events, including UserPromptSubmit, PermissionRequest, ConfigChange, CwdChanged, FileChanged, SubagentStart, SubagentStop, PreCompact, PostCompact, y SessionEnd.
A project-level .claude/settings.json can block edits to protected files and auto-format after writes:
{
"hooks": {
"PreToolUse": [
{
"matcher": "Edit|Write",
"hooks": [
{
"type": "command",
"command": "${CLAUDE_PROJECT_DIR}/.claude/hooks/protect-files.sh"
}
]
}
],
"PostToolUse": [
{
"matcher": "Edit|Write",
"hooks": [
{
"type": "command",
"command": "jq -r '.tool_input.file_path' | xargs npx prettier --write"
}
]
}
]
}
}
The protect-files.sh script reads JSON from stdin, checks the file path, and exits with code 2 to block edits to sensitive paths:
#!/bin/bash
INPUT=$(cat)
FILE_PATH=$(echo "$INPUT" | jq -r '.tool_input.file_path // empty')
if [[ "$FILE_PATH" == *".env"* || "$FILE_PATH" == *".git/"* ]]; then
echo "Blocked: $FILE_PATH is protected by project hook" >&2
exit 2
fi
exit 0
if filters.command, http, mcp_tool, prompt, y agent.GitHub Copilot does not expose the same depth of lifecycle interception as Cursor or Claude Code by default. Instead, it gives you customization surfaces that act as persistent guardrails. The Copilot customization cheat sheet compares the options and where each file lives.

.github/copilot-instructions.md (repo-wide), .github/instructions/*.instructions.md (path-specific), AGENTS.md o CLAUDE.md (agent instructions), or personal/org settings via the GitHub UI..github/prompts/*.prompt.md..github/agents/AGENT-NAME.md..github/skills/NAME/SKILL.md..github/hooks/*.json files.Custom instructions are the closest Copilot comes to a pre-action hook for most users. You can use them to say "never edit files in infra/ without approval," "always run pytest after changing Python files," or "load ARCHITECTURE.md before proposing refactors." Custom agents can restrict which tools are available to a given agent persona, which approximates a permission gate. The preview hooks feature adds deterministic shell commands at lifecycle events, but support is narrower than Cursor or Claude Code today.
| Capability | Cursor hooks.json | Claude Code hooks | GitHub Copilot |
|---|---|---|---|
| Lifecycle event interception | Yes — agent, tab, terminal/app events | Yes — tool, shell, session, prompt, compaction events | Partial — preview hooks in limited surfaces |
| Pre-action permission gate | Yes, via hooks | Yes, via PreToolUse y beforeShellExecution | Approximated by custom instructions; preview hooks add deterministic gates |
| Post-action verification | Yes, via after-event hooks | Yes, via PostToolUse y PostToolUseFailure | Limited; relies on model self-check or Actions |
| Repo-level config | .cursor/hooks.json | .claude/settings.json | .github/copilot-instructions.md, prompt files, agents, skills, preview hooks |
| Editor surface | IDE-native | Terminal-first | IDE + GitHub web/PR + CLI |
| Mejor ajuste | Teams that live in Cursor and want fine-grained control | Teams running terminal agents under review | GitHub-native teams needing prompt-level guardrails |
This table is a starting point, not a final architecture. The best choice often depends on which surface your team already uses eight hours a day.
Do not try to write a perfect policy on day one. Start with five hooks or instructions that prevent the most common failures:
rm -rf, drop table, kubectl delete, and any credential rotation unless explicitly allowed.packages/api/, the agent must read ARCHITECTURE.md y API_CONVENTIONS.md.These five rules will catch more incidents than the most expensive model upgrade.
A hook that fails silently is worse than no hook at all. Treat hook development like test-driven infrastructure:
Even the best hook system fails when humans undermine it:
npm run * also permits npm run deploy:production.Use this decision flow before committing budget and training time:
hooks.json. Treat it as experimental and pin Cursor versions during critical sprints.Most mature organizations will end up using more than one surface. The hooks strategy should match the surface strategy, not the other way around.
No. Hooks reduce the probability of a bad action, but they do not replace human review of agent-generated diffs. Think of hooks as guardrails, not approvers.
No. Hooks can block known dangerous patterns, require context, and enforce testing. They cannot anticipate every creative way a model or a prompt can go wrong.
For basic gates, no. A senior engineer can write the first version in an afternoon. For complex cross-repo policies or audit integrations, platform or security engineering help is wise.
Repo-level config is usually better for teams because it travels with the code and can be reviewed in PRs. User-level config is fine for personal experimentation.
Pick one high-risk action — for example, editing payment files or running deploy commands — and write a hook that blocks it without approval. Demo the block, then demo the approved path. Nothing convinces leadership faster than seeing a disaster not happen.
For the surrounding buyer context, read our full best coding AI agents 2026 guide and the coding agent permissions guide. If you are wiring agents to external tools, see MCP config locations for coding agents.