Kiro hooks: triggers, the JSON format, examples and fixes
Short answer: a Kiro hook is a small JSON file in .kiro/hooks/ that says "when this happens, do that". The trigger is an event like PostFileSave, PreToolUse or Stop. The action is either a shell command (runs in your project root, gets the event as JSON on stdin, costs no credits) or an agent prompt (sent to the agent, uses credits). A command that exits with code 2 on PreToolUse blocks the tool call. Hooks run in the Kiro IDE and Kiro CLI, not in Kiro Web sessions. The CLI V3 also loads global hooks from ~/.kiro/hooks/.
What Kiro hooks do
Kiro's docs say hooks "run shell commands or agent prompts automatically when specific events happen in your session". Steering tells Kiro how to work; a hook makes something happen every time, whether the agent remembers or not. The uses Kiro lists:
- Run linters, formatters or type checks after the agent changes a file.
- Block a dangerous tool call unless a check passes (
PreToolUse). - Create companion files, like tests or translations, when new source files appear.
- Check code quality before the agent finishes.
- Feed the agent extra instructions based on what it's doing.
| Feature (Kiro's table) | IDE | CLI | Web | Mobile |
|---|---|---|---|---|
| Event-driven hooks | Yes | Yes | No | No |
| Shell command actions | Yes | Yes | No | No |
| Agent prompt actions | Yes | Yes | No | No |
| Create hooks by asking the agent in chat | Yes | Yes | No | No |
Kiro Web can store your hooks under Agent Config > Cloud Config so new local IDE or CLI sessions can use them, but Web sessions themselves don't fire hooks.
Where Kiro hook files go
- Project:
.kiro/hooks/in your project root. Commit it and your whole team gets the same hooks. - Global (CLI V3):
~/.kiro/hooks/. These run in every workspace, alongside the project ones (both run; neither replaces the other). - Inside a custom agent (CLI V3): a
hooksarray in the agent's JSON profile. Those only run when you use that agent.
Any .json file name works (Kiro suggests kebab-case like lint-on-save.json), one file can hold several hooks, and hooks switch on by themselves when a session starts. There's nothing to register.
my-project/
└── .kiro/
└── hooks/
├── lint-on-save.json # one file can hold one hook...
├── guards.json # ...or several in the "hooks" array
└── scripts/
└── guard-env.js # scripts your hooks call (any folder works)
~/.kiro/hooks/ # global hooks (CLI V3), run in every workspace
Free rules starter pack for Cursor, Claude Code, Codex and Antigravity
One shared AGENTS.md that works across Cursor, Claude Code, Codex, Copilot and Antigravity, plus database and test rules in each tool's format. Unzip it into your project and fill in two sections. I'll email you the link.
Free. No spam. Unsubscribe anytime. See our privacy note.
The hook file format
Since Kiro IDE 1.0 and CLI V3, the IDE and CLI share one format. This is Kiro's own quick example, saved as .kiro/hooks/lint-on-save.json:
{
"version": "v1",
"hooks": [{
"name": "Lint on save",
"trigger": "PostFileSave",
"matcher": "\\.(ts|tsx)$",
"action": { "type": "command", "command": "npx eslint --fix" }
}]
}
| Field | Required? | What it does |
|---|---|---|
version | Yes | Always "v1" for now |
hooks | Yes | The list of hooks in this file |
name | Yes | A readable name for the hook |
description | No | Notes for people; Kiro doesn't act on it |
trigger | Yes | The event, in PascalCase (PreToolUse, not preToolUse) |
matcher | No | Which events count. For tool triggers, a tool name, tag or regex. For file triggers, a regex on the file path. Leave it out to match everything. |
action.type | Yes | "command" or "agent" |
action.command / action.prompt | One of them | The shell command, or the prompt for the agent |
timeout | No | Seconds a command may run (default 60, 0 = no limit) |
enabled | No | false turns a hook off without deleting it |
confirm | No | Ask before a Stop command hook runs |
The matcher is a regex inside a JSON string, so backslashes are doubled: "\\.(ts|tsx)$" in the file means \.(ts|tsx)$ to Kiro.
The confirm block is Kiro's way to ask first. This is their example:
{
"version": "v1",
"hooks": [
{
"name": "Submit session results",
"trigger": "Stop",
"action": { "type": "command", "command": "./submit.sh" },
"confirm": {
"question": "Submit this session's results?",
"options": [
{ "id": "submit", "label": "Yes, submit", "run": true },
{ "id": "dismiss", "label": "Not this time", "run": false }
]
}
}
]
}
You can also add confirmCommand, a script that runs first and decides whether to ask at all. If it fails or prints bad JSON, Kiro falls back to the fixed question.
Every Kiro hook trigger
| Trigger | Fires when | Matcher | Can block? | IDE / CLI |
|---|---|---|---|---|
SessionStart | A new chat session starts | None | No | IDE, CLI V3 |
UserPromptSubmit | You send a message | Prompt text | IDE only | Both |
PreToolUse | The agent is about to use a tool | Tool | Yes (exit 2) | Both |
PostToolUse | A tool has run | Tool | No | Both |
PostFileCreate | The agent creates a file | File path | No | IDE, CLI V3 |
PostFileSave | The agent saves or edits a file | File path | No | IDE, CLI V3 |
PostFileDelete | The agent deletes a file | File path | No | IDE, CLI V3 |
PreTaskExec | Before a spec task starts | None | Yes (exit 2) | IDE, CLI V3 |
PostTaskExec | After a spec task finishes | None | No | IDE, CLI V3 |
Stop | The agent finishes its turn | None | Can send it back to work | Both |
| Pre / Post Compaction | Before or after Kiro summarizes a long chat | None | Pre can cancel (exit 2) | IDE 1.4.121+, CLI 3.1.0+ (PreCompact / PostCompact) |
SessionEnd | A CLI session closes | None | No | CLI V3 only |
Two things trip people up. File triggers only fire on the agent's changes. If you save a file yourself in the editor, PostFileSave doesn't run. And there's no new manual hook in the IDE 1.0 format. Old IDE 0.x manual hooks still show with a "legacy" label and a play button; for anything new on demand, Kiro points you to a manually included steering file, which runs as a /name slash command (see Kiro steering files).
Matching tools in PreToolUse and PostToolUse
A tool matcher can be:
- A category:
read,write,shell,web,spec(all the built-in tools of that kind).shellcoversexecute_bashon Mac and Linux andexecute_pwshon Windows. - A source:
@builtin,@mcp,@powers,@postgres(one MCP server) or@postgres/query(one MCP tool). - A wildcard like
mcp_git_*(JSON files only; the IDE's hook editor rejects*). - A regex, used as soon as it has regex characters. Anchor it with
^...$if you mean an exact name.
In the event JSON, MCP tools show up as mcp_<server>_<tool>, lowercased, not as @server/tool. And plain names are matched loosely: read matches any tool ID that contains "read".
Command or agent action, and what exit codes do
Command actions run in your project root and get the event as JSON on stdin (fields like hook_event_name, cwd, session_id, and for tool hooks tool_name and tool_input). Kiro says they don't use credits and are faster. Agent actions send a prompt to the agent. They start a new agent loop, so they use credits, and they can't block anything. On UserPromptSubmit the hook's prompt is added to the end of yours.
- Exit 0: fine. On
SessionStartandUserPromptSubmit, what the command prints goes into the agent's context. - Exit 2: blocks
PreToolUseandPreTaskExec, and stderr goes to the agent as the reason. OnPreCompactit cancels the summary. - Any other non-zero: a warning. The tool still runs. This is the big gotcha for guard scripts: if your script crashes with exit 1, nothing is blocked.
Stopis different: exit 1 (or exit 0 with a"decision": "block"reply) starts another agent turn, so the agent keeps working.
For UserPromptSubmit, Kiro's Hook types page says the IDE can block a prompt but the CLI can't: in the CLI the prompt is always sent and the hook's output goes along with it. To stop something in the CLI, use PreToolUse.
How to create a hook
- IDE: click + in the Agent Hooks section of the Kiro panel, choose Ask Kiro to create a hook, and describe it ("run tests after every file save"). Kiro writes the JSON into
.kiro/hooks/. The eye icon next to a hook turns it on and off. - CLI: write the file in
.kiro/hooks/yourself (or ask the agent to). It's picked up when you start a session. - Custom agent (CLI V3): put the same hook objects in the agent profile's
hooksarray (the IDE ignores agents with this field; see Kiro custom agents). This is the shape from Kiro's configuration reference:
{
"hooks": [
{
"name": "git-status",
"trigger": "SessionStart",
"action": { "type": "command", "command": "git status" }
},
{
"name": "audit-shell",
"trigger": "PreToolUse",
"matcher": "^(execute_bash|execute_pwsh)$",
"action": {
"type": "command",
"command": "{ echo \"$(date) - Shell call:\"; cat; echo; } >> /tmp/shell_audit_log"
}
}
]
}
Kiro hook examples you can copy
Kiro's own examples page has four agent-prompt hooks: a security check on Stop, prompt logging on UserPromptSubmit, a translations helper on src/locales/en/*.json, and a test-coverage helper. The ones below are ours, written against the format above. Test them on a spare branch first.
Our example (VibeKits): format every file the agent saves, and show git status at the start
{
"version": "v1",
"hooks": [
{
"name": "format-on-save",
"description": "Prettier on every file the agent saves",
"trigger": "PostFileSave",
"matcher": "\\.(ts|tsx|js|jsx|css|md)$",
"action": { "type": "command", "command": "npx prettier --write {{filePath}}" },
"timeout": 30
},
{
"name": "git-status-at-start",
"trigger": "SessionStart",
"action": { "type": "command", "command": "git status --short" },
"timeout": 10
}
]
}
{{filePath}} is the file path variable Kiro's IDE 1.0 page uses for file triggers. Because git-status-at-start is a SessionStart command, its output lands in the agent's context, so the agent starts knowing what's uncommitted.
Our example (VibeKits): stop the agent writing to .env files
{
"version": "v1",
"hooks": [
{
"name": "block-env-writes",
"description": "Stop the agent writing to .env files",
"trigger": "PreToolUse",
"matcher": "write",
"action": { "type": "command", "command": "node .kiro/hooks/scripts/guard-env.js" },
"timeout": 10
}
]
}
// .kiro/hooks/scripts/guard-env.js
// Exit 2 = block the tool call and send the reason to the agent.
// Any other non-zero exit only WARNS and the write still happens,
// so every failure path below exits 2 on purpose (fail closed).
let raw = "";
process.stdin.on("data", (c) => (raw += c));
process.stdin.on("end", () => {
try {
const event = JSON.parse(raw);
const input = JSON.stringify(event.tool_input || {});
// Matches .env, .env.local, config/.env.production ... but not .envrc
if (/(^|[\\/"'\s])\.env(\.[\w-]+)?(["'\s]|$)/.test(input)) {
console.error("Blocked: don't write to .env files. Ask me to change secrets myself.");
process.exit(2);
}
process.exit(0);
} catch (err) {
console.error("guard-env.js could not read the hook event, so the write was blocked: " + err.message);
process.exit(2);
}
});
The write tag covers all of Kiro's built-in write tools, so you don't need their exact names. The script checks the whole tool input for a .env path, and if anything goes wrong it exits 2 rather than 1, because exit 1 would let the write through. It doesn't cover the agent writing .env through a shell command; add a second hook with "matcher": "shell" if you want that too.
Our example (VibeKits): run tests when the agent says it's done
{
"version": "v1",
"hooks": [
{
"name": "tests-on-stop",
"description": "Run tests when the agent finishes; send it back to fix failures (max 3 tries)",
"trigger": "Stop",
"action": { "type": "command", "command": "bash .kiro/hooks/scripts/tests-on-stop.sh" },
"timeout": 300
}
]
}
#!/bin/bash
# .kiro/hooks/scripts/tests-on-stop.sh
# Stop hook exit codes (Kiro docs): 0 = done, 1 = start another agent turn.
# A counter file stops it looping forever on a test it can't fix.
COUNT_FILE=".kiro/hooks/.stop-retries"
cat > /dev/null # we don't need the event JSON on stdin
if npm test --silent > /tmp/kiro-test-output.txt 2>&1; then
rm -f "$COUNT_FILE"
exit 0
fi
tries=$(cat "$COUNT_FILE" 2>/dev/null || echo 0)
if [ "$tries" -ge 3 ]; then
rm -f "$COUNT_FILE"
echo "Tests still failing after 3 tries; stopping so you can look." >&2
exit 0
fi
echo $((tries + 1)) > "$COUNT_FILE"
echo "Tests failed. Fix them before you finish. Last lines:"
tail -n 30 /tmp/kiro-test-output.txt
exit 1
Kiro's docs don't mention a built-in limit on how many times a Stop hook can send the agent back, so the counter file is there to stop a loop. Add .kiro/hooks/.stop-retries to .gitignore.
Our example (VibeKits): an agent-prompt hook for new components
{
"version": "v1",
"hooks": [
{
"name": "test-for-new-component",
"trigger": "PostFileCreate",
"matcher": "^src/components/.*\\.tsx$",
"action": {
"type": "agent",
"prompt": "A new component file was just created. If it has no matching .test.tsx file next to it, write one that renders the component and checks its main props. Don't change the component itself."
}
}
]
}
This one uses credits every time it fires, which is why the matcher only looks at src/components/. Kiro's best practices page says the same: start with narrow file patterns and widen them later.
Kiro hooks vs steering, skills and other tools' hooks
- Hooks vs steering: steering is instructions the agent reads (
.kiro/steering/*.md). A hook is automation that runs whether the agent follows instructions or not. "Use Prettier" is steering; "run Prettier after every save" is a hook. More in Kiro steering files. - Hooks vs skills: a skill is a step-by-step recipe the agent loads when a job calls for it (Kiro skills). Hooks don't wait for the agent to decide.
- Kiro vs Claude Code hooks: the names will look familiar. Kiro uses
PreToolUse,PostToolUse,UserPromptSubmit,StopandSessionStart, and exit 2 blocks, like Claude Code hooks. The file is different: Kiro wants standalone.kiro/hooks/*.jsonfiles withversion,triggerandaction, and its tool names (execute_bash,read_file) aren't Claude Code's. Kiro's docs don't describe importing Claude Code hooks, so copy the logic, not the file. - Kiro vs Cursor hooks: Cursor hooks live in one
.cursor/hooks.jsonwith camelCase events likebeforeShellExecutionandafterFileEdit. Same idea, different names.
Upgrading old hooks (IDE 0.x and CLI 2.x)
IDE 0.x used .kiro.hook files with when/then. Open the Agent Hooks panel and click the upgrade badge on each old hook. The names map like this: agentSpawn → SessionStart, agentStop → Stop, promptSubmit → UserPromptSubmit, fileCreated / fileEdited / fileDeleted → PostFileCreate / PostFileSave / PostFileDelete, preTaskExecution / postTaskExecution → PreTaskExec / PostTaskExec. Your regex patterns carry over.
CLI 2.x kept hooks in the agent profile as an object keyed by camelCase names. Run /upgrade-agent; it turns them into a hooks array and warns about anything it couldn't convert. Old tool names like fs_read and grep aren't V3 tool IDs, so switch those matchers to the category (read) or the V3 ID (read_file, grep_search).
Kiro hooks not working? 9 things to check
- Wrong folder. It has to be
.kiro/hooks/in the project root (or~/.kiro/hooks/for global hooks in CLI V3). Global hooks aren't in Kiro's IDE docs. - Old trigger names. The v1 format wants PascalCase:
PostFileSave, notfileEdited. - Missing
"version": "v1"or a typo in the JSON. Run the file through a JSON checker. - You edited the file yourself. File triggers only fire on the agent's changes.
- Single backslashes in the matcher. In JSON,
\.must be written\\.. - The hook is switched off. Check the eye icon in the Agent Hooks panel, or
"enabled": false. - The guard doesn't block. Only exit 2 blocks. Exit 1 is a warning and the tool runs anyway.
- The script won't run. Try the command in a terminal from the project root, make the script executable, and check its path. In the IDE, the Kiro output panel shows hook logs.
- You're on Kiro Web. Web sessions don't run hooks.
Where Kiro's own docs disagree
- Timeout field. The main Hooks page says
timeout, in seconds, default 60. The CLI tabs on the Hook actions, Management and Best practices pages still saytimeout_ms, default 30,000 ms, withcache_ttl_seconds. Those look like CLI 2.x leftovers; the CLI V3 migration page's own examples usetimeoutin seconds. - Blocking prompts. The Hook actions page says a non-zero exit blocks the prompt on
UserPromptSubmit. The Hook types page says CLI prompt hooks can't block at all, and the IDE 1.0 page says exit 2 blocks. fs_writematchers. The Best practices page suggests a matcher like"fs_write", while the Hook types page says CLI 2.x names aren't expanded in V3. Thewritetag avoids the question.- Hooks on Web. The Configuration scopes page says Kiro Web reads project hooks from
.kiro/hooks/; the Hooks page says Web sessions don't run them. Both can be true (Web stores and reads them for later local use), but no page says so outright.
Related setups
Hooks are one part of a Kiro setup. The others: steering files for your rules, skills for step-by-step jobs, and AGENTS.md, which Kiro reads too. In other tools: Claude Code hooks, Cursor hooks and Claude Code mods. Not sure what rules to give the agent in the first place? The free rules generator writes a starting AGENTS.md.
Best value
All 6 kits for $29 — or $20.30 with code WELCOME30 (through Nov 30)
Hooks keep Kiro's work tidy and safe. The kits tell it what to build, step by step: a small game, a Discord bot, an email-triage agent, an MVP, or a cleanup of messy code.
Get all 6 kits for $29 → See what's in each kit
$29 instead of $48 for the six kits bought separately; WELCOME30 takes 30% off that at checkout ($20.30). Instant download from Gumroad. 7-day refund, no questions asked.
Related
- Kiro steering files (.kiro/steering, inclusion modes, AGENTS.md, examples)
- Kiro skills (SKILL.md, .kiro/skills, skills vs steering vs powers)
- Kiro custom agents (.kiro/agents, permissions, subagents, examples)
- Kiro specs (requirements.md, design.md, tasks.md, Quick Spec, examples)
- Claude Code hooks examples (7 copy-paste hooks, plus Cursor)
- Cursor hooks (hooks.json, every event, examples, fixes)
- Antigravity hooks (hooks.json, six events, PreToolUse decisions, fixes)
- Claude Code mods (what a mod is, sample mods, your first mod)
- AGENTS.md examples (Kiro reads AGENTS.md as steering)
- Vibe coding security checklist (12 checks before you launch)
- Every rules file format
- Free rules file generator
Sources
- Kiro docs: Hooks
- Kiro docs: Hook types
- Kiro docs: Hook actions
- Kiro docs: Hook examples
- Kiro docs: Hook management
- Kiro docs: Hooks best practices
- Kiro docs: Troubleshooting hooks
- Kiro docs: What's new in IDE 1.0, Hooks
- Kiro docs: CLI V3 hooks migration
- Kiro docs: Agent configuration reference
- Kiro docs: Configuration scopes
Every trigger, field, exit code and folder here was checked Nov 11, 2026 on Kiro's own docs. Kiro changes often, so the Hooks pages in Kiro's docs win over this page. If something here no longer matches, tell us at [email protected].