Lessons from Spotify's Claude Code setup: stop paying frontier prices to read files
Tung Bui Thanh (MikeDaDev)8 min read

On this page
Open the log of any Claude Code session and count: how much of it is real thinking, and how much is reading a whole 1,500-line file to find three lines to change, or copying an existing test file and renaming the class? For most of us the answer is not flattering. And every one of those lines is billed at the price of the most expensive model.
This week a post on X from @undefinedKi spread quickly, describing a Claude Code setup published by Spotify engineers that cut about 90% of tokens. I read the original on Spotify's engineering blog (by Dimitri Mazmanov, published 3 September 2026). The 90% figure is real, but it measures something much narrower than the headline. This post covers the mechanism, how it was measured, the limits Spotify itself admits, and how to rebuild it for your own project.
# the starting observation: most agent work is not thinking
The original opens with something many people feel but rarely say: most of what a coding agent does is not thinking. It is I/O: reading files to find a few lines, generating boilerplate from an existing example, updating docs. None of that needs the reasoning of the strongest model, yet by default it all goes through it.
Spotify's idea is therefore simple: separate the two kinds of work, hand bulk reading and repetitive writing to a cheaper model, and let the main model receive only a short summary, or not see the result at all.
# the three layers of "shunt"
Spotify packaged the approach as a Claude Code plugin called shunt, which works through the CLI of Portal, their internal developer portal built on Backstage. It has three layers.
Layer 1: hooks that block large reads. Shunt registers two PreToolUse hooks. The first runs before every Read: if the file is longer than the threshold, it blocks the read and tells Claude to use the /bulk-reader skill. Targeted reads with offset and limit pass through. The second catches cat, head, tail, less and more on large files, while piped commands such as cat file | grep still run. The default threshold is 350 lines, configurable with the SHUNT_MIN_LINES environment variable.
Layer 2: two wrapper scripts. bulk-read takes a question and a list of files, sends them to the cheap model and returns structured bullet points. code-write takes a spec and a required reference file, generates code following that example and can write it straight to disk, so Claude never has to read the generated code.
Layer 3: two skills. Two Markdown files tell Claude when and how to call the scripts.
The model doing the heavy lifting is Gemini 2.5 Flash, configured as two "modes" in Portal's AiKA assistant at temperature 0.2, with very short instructions: one mode may only return structured bullets, the other may only return code with no explanation.
# why hooks, and not just a rule in CLAUDE.md
This is the most valuable lesson in the whole article. Before using hooks, the author tried writing the routing rules in CLAUDE.md. The model often ignored them. The author calls those rules "advisory, not enforced".
Hooks are different. They run before the tool is called, and if they block, the call does not happen, whatever the model intended. Even when Claude ignores the skill, the hook still blocks. This applies to every important rule for agents, not just tokens: anything that must always hold belongs where a machine checks it, not where the model "should read" it. It is the same spirit as All Code Is Slop: every time you have to remind an agent, turn the reminder into a deterministic check.
# what the 90% actually measures
The original explains its measurement, and read carefully the number is narrower than the headline:
- It was measured on one Java monorepo, across four scenarios that are not named individually.
- What was counted is the tokens Claude reads: reading files directly versus reading the
bulk-readersummary. The mean saving was around 90%. code-writehas no number, because the author says it is hard to measure in tokens.- The figure does not include the cheap model's cost, and is not expressed in money.
So "90% fewer tokens" here means 90% fewer tokens on the file reads that were delegated, not a 90% smaller bill. Still a good result, since file reading is usually the bulk of a session's tokens, but do not promise that number to your manager.
# what Spotify admits it cannot do
The limitations section of the original is refreshingly direct:
- Editing cannot be delegated. The cheap model's summaries do not carry reliable line numbers, so to edit, Claude still has to read the exact section itself. Those short, targeted reads are still billed in full.
- Reasoning cannot be delegated. In testing, the cheap model missed a subtle thread-safety bug that the main model found in seconds. So debugging, architecture decisions and safety-critical code are not routed to the cheap model.
- It is slower. Each delegation takes about 10 to 30 seconds, and Portal caps a single call at 30 seconds. For small files the delegation overhead outweighs the savings, which is why the 350-line threshold exists.
The missed thread-safety bug is the detail I want to stress. A wrong summary does not break anything immediately; it makes the main model confidently decide based on incomplete information. Keep summaries for lookup questions such as "which functions does this file have and what do they call", and let the main model read for itself when the question is "is this safe to run concurrently".
# rebuilding it with a hook and a subagent
Spotify's plugin needs Portal. Without it you can still build something similar with Claude Code's built-in features: a hook that blocks large reads, and a subagent on a cheaper model that does the reading instead. That is also the direction of the open-source project claude-code-flash, which describes itself as a self-hosted take inspired by shunt.
I ran the hook below against four cases: reading a whole 1,200-line file is blocked, reading a range with offset and limit passes, a 40-line file passes, and other tools are unaffected.
#!/usr/bin/env python3
"""Claude Code PreToolUse hook: stop whole-file reads of large files."""
import json
import os
import sys
MIN_LINES = int(os.environ.get("BIG_READ_MIN_LINES", "350"))
event = json.load(sys.stdin)
if event.get("tool_name") != "Read":
sys.exit(0)
args = event.get("tool_input", {})
if args.get("offset") or args.get("limit"):
sys.exit(0) # targeted reads (a line range) pass through
path = args.get("file_path", "")
try:
with open(path, "rb") as f:
lines = sum(1 for _ in f)
except OSError:
sys.exit(0) # let Claude Code report missing files itself
if lines > MIN_LINES:
print(
f"{path} has {lines} lines. Do not read it whole. Ask the bulk-reader "
"subagent a specific question about it, or read a range with offset/limit.",
file=sys.stderr,
)
sys.exit(2) # exit 2 blocks the tool call and shows stderr to ClaudeRegister the hook in the project's .claude/settings.json:
{
"hooks": {
"PreToolUse": [
{
"matcher": "Read",
"hooks": [{ "type": "command", "command": "python3 .claude/hooks/block-big-reads.py" }]
}
]
}
}And a reading subagent on a lighter model, with read-only tools, in .claude/agents/bulk-reader.md:
---
name: bulk-reader
description: Reads large files and answers a specific question about them in short bullets. Use before reading any file over 350 lines.
tools: Read, Grep, Glob
model: haiku
---
Answer only the question you were given, in short structured bullets.
Include file paths and the function or class names involved.
Do not propose edits. If the answer needs exact line numbers, say so instead of guessing.The last line of the subagent's instructions is how I handle the exact limit Spotify ran into: instead of letting the cheap model guess line numbers, make it say that a direct read is needed, so the main model reads that section itself.
# how to apply it
What I take from this, in the order I would do it:
- Measure before optimising. Look at where your tokens went over a week. If most of it is reading large files, this is worth doing; if your repo is all small files, the gain is minimal.
- Start with the hook, without a cheap model. Just blocking whole-file reads and forcing the agent to read ranges or use
grepalready cuts a lot, with no risk of a wrong summary. - Only route lookup questions to the cheap model. Leave debugging, security review and concurrency to the main model.
- Measure total cost, not just the main model's tokens, including the extra 10 to 30 seconds of waiting per delegation.
The biggest lesson has nothing to do with tokens: a rule written in an instructions file is advice, a rule enforced by a hook is law. Whatever you truly need an agent to follow, put it in the second place.
References:
- Spotify Engineering: Portal by Spotify cut my Claude Code token usage by 90% (September 2026)
- @undefinedKi on X: a summary of Spotify's setup
- claude-code-flash: a self-hosted take inspired by shunt
Just personal notes, shared in the hope they are useful. If you found this helpful, feel free to pass it on to your friends and colleagues!
Happy coding ๐ ๐๐ป ๐ ๐ฅ.