Skip to content

post-tool-use Hook

Captures observations from every tool call - file changes, bugs, decisions, and failed attempts


post-tool-use Hook

The post-tool-use hook fires after every tool call in Claude Code. It classifies each tool call into an observation type (file change, bugfix, feature, decision, failed attempt, etc.) and stores it in the memory database. This is the primary mechanism by which Massu AI learns from your sessions.

Trigger Event

Fires after every tool call: Read, Write, Edit, Bash, Grep, Glob, and all MCP tools.

What It Does

  1. Receives tool call details (tool name, input, response)
  2. Classifies the tool call into an observation type using pattern matching:

- Write/Edit calls become file_change observations - Bash commands with error output become failed_attempt observations - Bash commands with test results become vr_check observations - Tool calls referencing decisions become decision observations

  1. Deduplicates failed attempts (increments recurrence count instead of creating duplicates)
  2. Detects plan progress from tool responses and updates the session summary
  3. Stores the observation in the memory database

Performance

This hook must complete in under 500ms to avoid slowing down the AI workflow. It achieves this by:

  • Using lightweight pattern matching (no AI calls)
  • Deduplicating Read calls within a session (in-memory set)
  • Batch-writing to SQLite in a single transaction

Example Input

json
{
  "session_id": "abc123-def456",
  "transcript_path": "/path/to/transcript.jsonl",
  "cwd": "/Users/dev/my-app",
  "hook_event_name": "post_tool_use",
  "tool_name": "Edit",
  "tool_input": {
    "file_path": "/Users/dev/my-app/src/server/routers/orders.ts",
    "old_string": "publicProcedure.mutation",
    "new_string": "protectedProcedure.mutation"
  },
  "tool_response": "File edited successfully"
}

Observation Types

TypeWhen CreatedImportance
file_changeWrite or Edit tool called3
bugfixEdit fixes a known error pattern7
featureNew file created for a feature6
refactorRename or restructure patterns5
decisionDecision language detected8
failed_attemptBash command fails or error detected9
vr_checkTest or build command results6
discoveryRead/Grep reveals new information5

Configuration

No explicit configuration is needed. The hook uses:

  • Memory database for storage (getMemoryDb())
  • Observation extractor for classification (classifyRealTimeToolCall())
  • Plan progress detector for tracking completion

Tips

  • The hook filters out duplicate Read calls within a session to reduce noise
  • Failed attempts are the most valuable observations -- they prevent repeating mistakes
  • Plan progress is auto-detected from tool responses containing plan item references