A/B Testing Claude Code Hooks - Optimizing Token Usage

Fazm Team··2 min read

A/B Testing Claude Code Hooks - Optimizing Token Usage

Claude Code hooks let you run scripts before and after agent actions. When configured well, they front-load context so the agent spends fewer tokens figuring out what it already should know. The cache read jumps in token usage confirm this - hooks that inject relevant context increase cache hits significantly.

What Hooks Do to Token Flow

Without hooks, the agent discovers context through exploration. It reads files, runs commands, and gradually builds understanding. Each discovery step costs tokens.

With hooks, you inject context before the agent starts working. A pre-tool hook can provide file contents, project state, or relevant documentation. The agent starts with more information and explores less.

How to A/B Test

Run the same task twice - once with hooks enabled, once without. Compare:

  • Total tokens consumed - hooks should reduce this by 15-30%
  • Cache read ratio - hooks should increase cache hits because the same context gets reused across requests
  • Task completion time - fewer exploration steps means faster completion
  • Error rate - better initial context means fewer wrong-direction attempts

The Cache Read Signal

When you see cache reads jump after adding a hook, it means the injected context is being reused across multiple agent turns. This is the clearest signal that your hook is working. The agent is not re-discovering information it already has.

Hooks That Actually Help

The highest-impact hooks inject context that the agent would otherwise spend 2-3 turns discovering:

  • Project structure summaries - saves the agent from running tree commands
  • Recent git changes - tells the agent what was modified recently
  • Test results - gives immediate feedback without running the test suite
  • Build errors - front-loads the problem so the agent can start fixing immediately

Hooks That Waste Tokens

Injecting too much context is counterproductive. If your hook dumps the entire codebase into context, you are spending more tokens on the hook output than you save on exploration. Keep hook output focused and relevant to the task type.

Fazm is an open source macOS AI agent. Open source on GitHub.

More on This Topic

Related Posts