Claude Code hooks

Claude Code PostToolUse hook: check a tool result and tell Claude

A PostToolUse hook runs right after a tool call has succeeded. The tool has already done its work. The hook can read the result and tell Claude what to fix.

This page covers the input, the feedback fields, a script you can copy, and the traps. Every statement about Claude Code comes from the official hooks reference, linked at the end.

When the hook runs

PostToolUse runs immediately after a tool completes successfully. It matches on tool name, with the same values as PreToolUse. A call that fails fires PostToolUseFailure instead. The reference adds one exception: in the main conversation,PostToolUse hooks from managed settings also run after some failed calls, so write those to handle both.

To run a hook after any tool, leave out the matcher or use "*". The hook can then find out what changed by itself, for example with git status --porcelain, which also lists untracked files that git diff misses.

A hook that matches Edit|Write does not run when a Bash command or a process outside Claude Code rewrites the same file. For changes on disk from any source, the reference points to the FileChanged event.

What arrives on stdin

The input has the common fields plus tool_name, tool_input (the arguments sent to the tool), tool_response (the result it returned) and tool_use_id. The exact shape of both depends on the tool. This is the example for a Write call:

stdin
{
  "session_id": "abc123",
  "cwd": "/Users/me/my-project",
  "permission_mode": "default",
  "hook_event_name": "PostToolUse",
  "tool_name": "Write",
  "tool_input": {
    "file_path": "/path/to/file.txt",
    "content": "file content"
  },
  "tool_response": {
    "filePath": "/path/to/file.txt",
    "type": "create"
  },
  "tool_use_id": "toolu_01ABC123...",
  "duration_ms": 12
}

duration_ms is optional. It is the tool's run time and leaves out the time spent in permission prompts and PreToolUse hooks. For an MCP tool the input also carries the mcp_server object described on the PreToolUse page.

When a Bash command changes files in a Git repository, Claude Code can record the change in tool_response.bashEditDiff. The reference calls this list best effort and in public beta, and says its shape may change. It needs Claude Code v2.1.269 or later and depends on the bashEditDiffEnabled setting.

Feedback and changed output

Exit 0 and print one JSON object. These fields are specific to the event:

decision
"block" adds the reason next to the tool result. Claude still sees the original output.
reason
The explanation Claude reads when decision is "block".
hookSpecificOutput.additionalContext
A string added to Claude's context next to the tool result.
hookSpecificOutput.updatedToolOutput
Replaces the tool's output before it goes to Claude. The value must match the tool's own output shape.
hookSpecificOutput.updatedMCPToolOutput
Replaces the output for MCP tools only. The reference prefers updatedToolOutput, which also works for built-in tools.
hookSpecificOutput.classifierContext
A short note for the auto mode classifier, not for Claude. Needs Claude Code v2.1.236 or later.

A hook that exits 2 does not stop anything, because the tool already ran. Claude Code shows your stderr to Claude instead. That is the shortest way to warn Claude from this event. With exit 0, Claude never sees stderr.

Claude Code places additionalContext next to the tool result. Keep it to facts about the state of things, such as "This file is generated. Edit src/schema.ts and run bun generate instead." (the reference's own example).

Example: check JSON after an edit

The script below reads the file Claude just wrote or edited. When it ends in .json and no longer parses, the hook tells Claude. It runs as it stands: a test in our repository executes this exact code.

1. Add the hook

.claude/hooks/check-json.mjs
import { readFileSync } from "node:fs";

const input = JSON.parse(readFileSync(0, "utf8"));
const file = String(input.tool_input?.file_path ?? "");
if (!file.endsWith(".json")) process.exit(0);

let text;
try {
  text = readFileSync(file, "utf8");
} catch {
  process.exit(0);
}

try {
  JSON.parse(text);
} catch (error) {
  console.log(
    JSON.stringify({
      decision: "block",
      reason: file + " is not valid JSON after this edit: " + error.message,
    }),
  );
}

A file that is not .json, or that cannot be read, ends the hook without output and with exit 0. The call goes on unchanged.

2. Register it

.claude/settings.json
{
  "hooks": {
    "PostToolUse": [
      {
        "matcher": "Write|Edit",
        "hooks": [
          {
            "type": "command",
            "command": "node",
            "args": [
              "${CLAUDE_PROJECT_DIR}/.claude/hooks/check-json.mjs"
            ],
            "timeout": 10
          }
        ]
      }
    ]
  }
}

Claude then sees the reason next to the tool result and can repair the file in its next step. Test the script without Claude by piping a payload into it, as in the stdin example above.

Traps

  1. The tool has already run. Files are written, commands are executed, requests are sent. decision: "block" adds a note, and updatedToolOutput changes only what Claude sees. To stop a call, use a PreToolUse hook. Telemetry also records the original output before your hook runs.
  2. A replacement must match the tool's shape. Built-in tools return objects. The Bash tool returns stdout, stderr, interrupted and isImage. A plain string in its place can be ignored, and Claude then gets what the tool returned. For an MCP tool, return a string or an array of content blocks.
  3. Esc cancels the hook. If you interrupt a turn while a tool runs, Esc cancels the hook at once. With Ctrl+Enter the hook runs and has 10 seconds, whatever its own timeout. A message queued with Enter does not interrupt the hook.
  4. Do not copy untrusted text into classifierContext. The classifier reads it as information from the application that hosts the session. The reference caps the notes for one call at 2,000 characters.
  5. Slow work belongs in the background. A command hook with async: true runs in the background. It cannot block or rewrite the result.

Sources

Checked against the official docs on .

More: All Claude Code hook events · PreToolUse hook · Stop hook · All docs