Home › Rules › Kiro hooks

Kiro hooks: triggers, the JSON format, examples and fixes

From Kiro's own docs (Hooks, Hook types, Hook actions, Hook examples, Best practices, Troubleshooting, IDE 1.0 and CLI V3 migration pages), checked Nov 11, 2026. Sources below.

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:

Feature (Kiro's table)IDECLIWebMobile
Event-driven hooksYesYesNoNo
Shell command actionsYesYesNoNo
Agent prompt actionsYesYesNoNo
Create hooks by asking the agent in chatYesYesNoNo

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

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.

Where hook files live
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

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:

Kiro's example: ESLint when the agent saves a TypeScript file
{
  "version": "v1",
  "hooks": [{
    "name": "Lint on save",
    "trigger": "PostFileSave",
    "matcher": "\\.(ts|tsx)$",
    "action": { "type": "command", "command": "npx eslint --fix" }
  }]
}
FieldRequired?What it does
versionYesAlways "v1" for now
hooksYesThe list of hooks in this file
nameYesA readable name for the hook
descriptionNoNotes for people; Kiro doesn't act on it
triggerYesThe event, in PascalCase (PreToolUse, not preToolUse)
matcherNoWhich 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.typeYes"command" or "agent"
action.command / action.promptOne of themThe shell command, or the prompt for the agent
timeoutNoSeconds a command may run (default 60, 0 = no limit)
enabledNofalse turns a hook off without deleting it
confirmNoAsk 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:

Kiro's example: ask before a Stop hook runs
{
  "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

TriggerFires whenMatcherCan block?IDE / CLI
SessionStartA new chat session startsNoneNoIDE, CLI V3
UserPromptSubmitYou send a messagePrompt textIDE onlyBoth
PreToolUseThe agent is about to use a toolToolYes (exit 2)Both
PostToolUseA tool has runToolNoBoth
PostFileCreateThe agent creates a fileFile pathNoIDE, CLI V3
PostFileSaveThe agent saves or edits a fileFile pathNoIDE, CLI V3
PostFileDeleteThe agent deletes a fileFile pathNoIDE, CLI V3
PreTaskExecBefore a spec task startsNoneYes (exit 2)IDE, CLI V3
PostTaskExecAfter a spec task finishesNoneNoIDE, CLI V3
StopThe agent finishes its turnNoneCan send it back to workBoth
Pre / Post CompactionBefore or after Kiro summarizes a long chatNonePre can cancel (exit 2)IDE 1.4.121+, CLI 3.1.0+ (PreCompact / PostCompact)
SessionEndA CLI session closesNoneNoCLI 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:

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.

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

Kiro's example: hooks inside a CLI V3 agent profile
{
  "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

Our example (VibeKits): Prettier on save + git status on session 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

Our example (VibeKits): PreToolUse guard on every write tool
{
  "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
    }
  ]
}
Our example (VibeKits): the guard script (fails closed)
// .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

Our example (VibeKits): Stop hook that sends the agent back on failing tests
{
  "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
    }
  ]
}
Our example (VibeKits): the test script, with a 3-try limit
#!/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

Our example (VibeKits): PostFileCreate with an agent action
{
  "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

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

  1. 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.
  2. Old trigger names. The v1 format wants PascalCase: PostFileSave, not fileEdited.
  3. Missing "version": "v1" or a typo in the JSON. Run the file through a JSON checker.
  4. You edited the file yourself. File triggers only fire on the agent's changes.
  5. Single backslashes in the matcher. In JSON, \. must be written \\..
  6. The hook is switched off. Check the eye icon in the Agent Hooks panel, or "enabled": false.
  7. The guard doesn't block. Only exit 2 blocks. Exit 1 is a warning and the tool runs anyway.
  8. 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.
  9. You're on Kiro Web. Web sessions don't run hooks.

Where Kiro's own docs disagree

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

Sources

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].

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.

After that, an occasional email when I publish a new guide or rules file.

Free. No spam. Unsubscribe anytime. See our privacy note.