Hooks as Token Filters and Subagents as Context Isolators
- Write a PreToolUse hook that preprocesses command output before it enters the context window
- Delegate verbose operations to subagents so their output stays in an isolated context
- Install a code intelligence plugin and explain how it replaces expensive grep-plus-file-read sequences
Two Advanced Levers for High-Volume Contexts
Session commands, model selection, and prompt habits cover most of what you can do to manage tokens in a typical session. For power users and teams running Claude Code at scale, two more levers go deeper: hooks that filter data before it reaches Claude, and subagents that isolate expensive operations in their own context windows.
Hooks as Token Filters
A PreToolUse hook runs before a tool call executes. One of its capabilities is rewriting the tool input — which means you can intercept a command before it runs, modify it to produce less output, and return the filtered version for Claude to execute.
The canonical example: filtering test output to show only failures. A test suite that prints 500 lines of output costs thousands of tokens. Most of those lines are passing tests — information Claude does not need. A hook that rewrites the test command to pipe through a failure filter reduces that output to dozens of lines.
Here is how the approach works in settings.json:
{
"hooks": {
"PreToolUse": [
{
"matcher": "Bash",
"hooks": [
{
"type": "command",
"command": "~/.claude/hooks/filter-test-output.sh"
}
]
}
]
}
}
The hook script receives the tool input as JSON, checks if the command is a test runner, and rewrites it to pipe output through a grep filter. The rewritten command produces a fraction of the original output — the same information Claude needs, without the noise.
Beyond test output, this pattern applies to: truncating large log files before Claude reads them, filtering API responses to relevant fields, preprocessing search results to remove boilerplate. Any operation that tends to produce large output is a candidate for a filter hook.
Code Intelligence Plugins
Without a language server, Claude finds a function definition by grepping for its name across candidate files and then reading several of them to locate the right one. With a code intelligence plugin installed, "go to definition" is a single call that returns the exact location. The difference is typically one precise call versus a grep plus two or three file reads.
Language servers also report type errors automatically after edits, so Claude catches type mistakes without running a compiler — eliminating another round of output to process.
Check /plugins for available code intelligence plugins for your language stack. For large typed codebases, this is one of the most cost-effective setup investments.
Subagents as Context Isolators
A subagent is a separate Claude instance with its own context window. When you delegate a verbose operation to a subagent, its output stays in the subagent's context — only a summary returns to your main conversation.
Operations worth delegating:
- Documentation fetching: reading a large documentation page to extract one answer. The full page content stays in the subagent; your main conversation gets the answer.
- Log analysis: processing a large log file to find relevant entries. The log content stays in the subagent; your main conversation gets the filtered result.
- Exploratory research: scanning multiple files to understand a pattern. The exploration stays in the subagent; your main conversation gets the synthesis.
- Test runs with full output: running a test suite and summarizing results. The full output stays in the subagent.
Each subagent has its own full context window, so the isolation is complete. Your main conversation is not burdened by the verbose work — it only receives the result.
For tasks that fire repeatedly or handle routine operations, use Haiku as the subagent model. Set it explicitly in the subagent configuration: model: haiku. For research-heavy or synthesis tasks, Sonnet is appropriate.
- A PreToolUse hook that filters test output to failures only can reduce context consumption from thousands of tokens to dozens — any high-volume command output is a candidate for a filter hook.
- Code intelligence plugins replace grep-plus-file-read sequences with single precise calls; for large typed codebases, installing a language server is one of the highest-leverage setup investments.
- Subagents isolate expensive operations — documentation fetches, log analysis, codebase exploration — in their own context windows; only the summary reaches your main conversation.
- Use Haiku as the model for subagents doing routine or repetitive work; the capability gap is acceptable for lookups and summaries and the cost difference is significant.
- The combination of filter hooks (reduce per-operation output) and subagents (isolate expensive operations) addresses token consumption at the source — before it enters the main context — rather than managing it after the fact.