$ recombobulate _
home / tips / filter-test-output-with-a-pretooluse-hook-to-cut-token-costs
169

Filter Test Output with a PreToolUse Hook to Cut Token Costs

recombobulate @recombobulate · Mar 26, 2026 · Performance
filter-test-output-with-a-pretooluse-hook-to-cut-token-costs

Running pytest or npm test can dump thousands of lines into Claude's context. Most of that output is passing tests you don't care about. A PreToolUse hook can intercept test commands and pipe them through a filter before Claude ever sees the results.

{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Bash",
        "hooks": [
          {
            "type": "command",
            "command": "~/.claude/hooks/filter-test-output.sh"
          }
        ]
      }
    ]
  }
}

The hook script checks if the command is a test runner and rewrites it to only show failures:

#!/bin/bash
input=$(cat)
cmd=$(echo "$input" | jq -r '.tool_input.command')

if [[ "$cmd" =~ ^(npm test|pytest|go test) ]]; then
  filtered_cmd="$cmd 2>&1 | grep -A 5 -E '(FAIL|ERROR|error:)' | head -100"
  echo "{\"hookSpecificOutput\":{\"hookEventName\":\"PreToolUse\",\"permissionDecision\":\"allow\",\"updatedInput\":{\"command\":\"$filtered_cmd\"}}}"
else
  echo "{}"
fi

This can reduce context from tens of thousands of tokens to a few hundred. Claude still sees all the failures it needs to debug, just none of the noise.

Less noise in, fewer tokens burned, same debugging power.


via Claude Code Docs — Costs

~/recombobulate $ tip --comments --count=0

Log in to leave a comment.

~/recombobulate $ tip --related --limit=3
116
Use /clear to Start Fresh When Your Session Context Gets Confused or Stale

When Claude starts referencing files you've since deleted, remembering old code you've already changed, or getting confused by contradictory instructions from a long session — type /clear to wipe the slate clean. Unlike /compact which preserves context, /clear gives you a true fresh start without restarting the CLI.

recombobulate @recombobulate · 1 month ago
64
Toggle /fast for Quicker Responses on Simple Tasks

Not every task needs deep reasoning. Type /fast to switch Claude Code into fast mode — same model, faster output — for quick edits, simple questions, and routine changes. Toggle it off when you need Claude to think harder on complex problems.

recombobulate @recombobulate · 1 month ago
112
Use /compact to Free Up Context Space When Your Session Gets Long

Long sessions eat through your context window as conversation history piles up. Type /compact to summarize the conversation so far and reclaim space — keeping Claude's understanding of what you're working on while freeing up room for more work.

recombobulate @recombobulate · 1 month ago