●2.1.334 — Claude Code 2.1.334: /model now has a search box once the list passes 25 models, matching names and model IDs●FAST — Fast mode for Claude Opus 4.8 ended on 11/30. Drop speed: "fast" and Opus 4.8 keeps working●OPUS 4.5 — claude-opus-4-5 retires on 2027/1/22, 50 days from now. The replacement is claude-opus-5-5●IME — On the Windows desktop app, pressing Enter to confirm Japanese IME input can send the message early●NEW — New: Compare tool-use requests across three routes before moving to Bedrock●MCP — More setups now share one local MCP server across Claude Code, Cursor and Antigravity●2.1.334 — Claude Code 2.1.334: /model now has a search box once the list passes 25 models, matching names and model IDs●FAST — Fast mode for Claude Opus 4.8 ended on 11/30. Drop speed: "fast" and Opus 4.8 keeps working●OPUS 4.5 — claude-opus-4-5 retires on 2027/1/22, 50 days from now. The replacement is claude-opus-5-5●IME — On the Windows desktop app, pressing Enter to confirm Japanese IME input can send the message early●NEW — New: Compare tool-use requests across three routes before moving to Bedrock●MCP — More setups now share one local MCP server across Claude Code, Cursor and Antigravity
Two hooks that record every model switch and stop the ones you never agreed to
Build a PostModelSwitch hook that logs every model change and a PreModelSwitch hook that blocks unapproved switches during unattended runs. Complete scripts, measured timings, and the reason the two jobs must stay separate.
Last week my weekly usage on a set of unattended scheduled runs came in higher than I expected, so I went looking for the breakdown. My logs had the outcome of every run and the artifacts it produced. What they did not have, anywhere, was a record of when the session changed models.
As an indie developer running routine work on scheduled automation, splitting work across models is the obvious design. Formatting and search-and-replace go to something light; rewriting prose goes to something heavier. The problem was that this split existed as an intention, not as an observation. I had never once written down what actually happened.
Claude Code v2.1.251 added two hook events for exactly this: PreModelSwitch and PostModelSwitch. We are going to build a recorder and a guard — as two separate hooks, on two separate events. The separation is the interesting part, so let's start there.
A usage total cannot be decomposed after the fact
The Claude Code weekly limit is a shared pool across models. The status line JSON exposes rate_limits.seven_day alongside seven_day_opus and seven_day_sonnet, which tells you the pool carries per-model sub-buckets inside a single ceiling.
Practically, that means any run that used a heavier model than intended has already eaten into the room left for everything else. On September 14, 2026 the weekly limit settled permanently at +25% over the pre-promotion baseline. That is lower than the +50% the promotion had been giving, so re-measuring how the allocation actually behaves matters more now, not less.
Re-measuring needs data. Checking /usage afterwards gives you a total, not an attribution. Some information exists only at the instant the switch happens, so the recorder has to be in place first.
What the two events hand you
PreModelSwitch fires immediately before a switch. PostModelSwitch fires after the session's model has changed. Where other hooks receive things like tool_name, these two receive fields that describe the switch itself.
Field
Type
Meaning
from_model
string
Model in use before the switch
to_model
string
Model being switched to
requested_model
string | null
The explicitly requested model; null when the switch was automatic
source
"command" | "picker" | "sdk"
How the switch was triggered: the /model command, the interactive picker, or the SDK
context_tokens
number
Context size at that moment
prompt_cache_warm
boolean
Whether the prompt cache is currently warm
cache_ttl
"5m" | "1h"
Cache lifetime in effect
estimated_cache_write_usd
number
Estimated cost of the cache write, in USD
pricing
"configured" | "catalog" | "default"
Which price table produced that estimate
The usual session_id, transcript_path, and cwd come along too. PostModelSwitch carries the same fields, but source can take more values there than on the Pre side. Because that list may grow from release to release, I do not enumerate it here; I check the official hooks reference before writing any branch on it (as of November 2026).
That source field is what lets you tell a human choosing a model at a picker apart from a switch that happened inside an unattended run. That distinction becomes the hinge for the guard.
✦
Thank you for reading this far.
Continue Reading
What follows includes implementation code, benchmarks, and practical content we hope you'll find useful. This site runs without ads — server and development costs are supported entirely by members like you. If it's been helpful, we'd be truly grateful for your support.
WHAT YOU'LL LEARN
✦You will be able to explain, after the fact, which unattended run consumed which model instead of guessing from a single usage total
✦You will avoid the failure mode where a heavy step inside PreModelSwitch quietly stops model switching altogether
✦You will be able to fold the cache write cost reported by estimated_cache_write_usd into a switching decision you were making on per-token price alone
Secure payment via Stripe · Cancel anytime
✦
Unlock This Article
Get full access to the rest of this article. Buy once, read anytime. This site is ad-free — your support goes directly toward keeping it running.
Recording belongs to PostModelSwitch; only the decision belongs to PreModelSwitch
This is the design choice worth making before you write any code. My first attempt put both the logging and the policy check into PreModelSwitch, since standing right before the switch means you can see both sides of it.
That turns out to be the wrong place. When a PreModelSwitch hook is canceled at its timeout, the model switch is blocked. Most hook events fail open on a timeout; this one fails closed.
So a logging step that simply took too long stops the switch from happening at all. You wanted a log line, and instead your model routing silently stopped working — with a cause that is genuinely hard to see from the outside. In an unattended run, nobody is watching the screen when it happens.
Hence the split.
Event
Job
What may live there
PreModelSwitch
Decide whether to block, nothing else
Local file reads, string comparison, and a one-line append when blocking. No network calls, no aggregation
PostModelSwitch
Record, nothing else
File appends, outbound sends. Slowness here cannot affect a switch that already happened
We want to block switches to unapproved models during unattended runs only. Interactive switches have a human in front of them, so they stay out of scope.
Start with an allowlist. Put one model ID per line in .claude/model-allowlist.txt.
claude-haiku-4-5-20251001claude-sonnet-5
The hook reads JSON on stdin and exits with code 2 when the switch should not proceed. For most hook events, exit code 2 is the only exit code that blocks through the exit code alone.
#!/usr/bin/env bash# .claude/hooks/model-switch-guard.sh# PreModelSwitch: decision only. Nothing heavy goes in here.set -uo pipefailINPUT=$(cat)ALLOW_FILE="${CLAUDE_PROJECT_DIR:-$PWD}/.claude/model-allowlist.txt"# Let everything through unless this is an unattended run.# An interactive switch was somebody's deliberate choice.[[ "$INPUT" == *'"source":"sdk"'* || "$INPUT" == *'"source": "sdk"'* ]] || exit 0# Pull out to_model with parameter expansion — no subprocess.TO="${INPUT#*\"to_model\":}"TO="${TO#*\"}"TO="${TO%%\"*}"# No allowlist means no blocking. A missing config file# should never be the thing that halts your work.[ -f "$ALLOW_FILE" ] || exit 0grep -qxF "$TO" "$ALLOW_FILE" && exit 0echo "Unattended run attempted a switch to an unapproved model: ${TO}" >&2echo "Allowlist: ${ALLOW_FILE}" >&2exit 2
Feeding it three sample payloads:
$ echo '{"to_model":"claude-opus-5","source":"sdk"}' | ./model-switch-guard.shUnattended run attempted a switch to an unapproved model: claude-opus-5Allowlist: /tmp/hooktest/.claude/model-allowlist.txtexit=2$ echo '{"to_model":"claude-sonnet-5","source":"sdk"}' | ./model-switch-guard.shexit=0$ echo '{"to_model":"claude-opus-5","source":"picker"}' | ./model-switch-guard.shexit=0
The third case matters most. A guard that also blocks the picker turns into an obstacle the moment you want to try something by hand.
Why the JSON parsing avoids Python
My first version piped the payload through python3 to read to_model and source. That version is plainly easier to read. Running each variant twenty times on my machine gave these numbers:
Implementation
Per invocation
Parsing with Python
29 ms
Parameter expansion only
7 ms
Almost all of the difference is interpreter startup. Even 29 ms sits nowhere near a timeout, and on those numbers alone the readable version wins. I went with the fast one because of what PreModelSwitch does when it runs out of time: slow means blocked. Leaving headroom now keeps room for a heavier check later.
These are numbers from my environment, not a benchmark you should adopt. Measure your own. What transfers is the shape of the cost — interpreter startup as a fixed overhead on every invocation — not the milliseconds.
The first version let a single space through
About two months after publishing this, I added tests that varied the shape of the input. What caught me was a payload with a space before the colon.
claude-opus-5 is not on the allowlist, and the guard let it through anyway. The source check only recognized "source":"sdk" and "source": "sdk", so "source" : "sdk" read as "not unattended" and passed. For a guard, that is the worst direction to fail in.
I have not verified, release by release, exactly how Claude Code formats the JSON it hands to hooks. But a guard that silently stops guarding because whitespace moved is not worth keeping. So I swapped the extraction for one that ignores spacing and newlines, still without spawning anything: bash's =~ with BASH_REMATCH.
#!/usr/bin/env bash# .claude/hooks/model-switch-guard.sh (revised)# PreModelSwitch: decide only. Nothing heavy goes here.set -uo pipefailINPUT=$(cat)DIR="${CLAUDE_PROJECT_DIR:-$PWD}/.claude"ALLOW_FILE="$DIR/model-allowlist.txt"# Extract with bash regex so spacing and newlines in the payload do not matter.# No external processes.re_src='"source"[[:space:]]*:[[:space:]]*"([^"]*)"'re_to='"to_model"[[:space:]]*:[[:space:]]*"([^"]*)"'[[ $INPUT =~ $re_src ]] && SRC="${BASH_REMATCH[1]}" || SRC=""[[ $INPUT =~ $re_to ]] && TO="${BASH_REMATCH[1]}" || TO=""# Anything that is not an unattended run (source=sdk) passes through.[ "$SRC" = "sdk" ] || exit 0# No allowlist means no blocking, so a missing file never halts work.[ -f "$ALLOW_FILE" ] || exit 0if [ -z "$TO" ]; then echo "Could not read to_model; blocking because this is an unattended run" >&2 exit 2figrep -qxF -- "$TO" "$ALLOW_FILE" && exit 0# Record only the fact of a block (a single append; the decision stays fast).printf '%s\t%s\n' "$(date -u +%FT%TZ)" "$TO" >> "$DIR/model-switch-blocked.tsv" 2>/dev/nullecho "Switch to a model not allowed for unattended runs: ${TO}" >&2echo "Allowlist: ${ALLOW_FILE}" >&2exit 2
Keep the same filename and settings.json does not change. The one-line append to model-switch-blocked.tsv on a block is there for a later section; a single append barely moves the timing.
Five payloads, old and revised side by side:
Payload
Old exit code
Revised exit code
{"to_model":"claude-opus-5","source":"sdk"}
2 (block)
2 (block)
{"to_model" : "claude-opus-5", "source" : "sdk"}
0 (passes)
2 (block)
Pretty-printed JSON with newlines and indentation
2
2
{"source":"sdk"} (no to_model)
2 (error names the model "source")
2 (says it could not read to_model)
{"to_model":"claude-sonnet-5","source":"sdk"}
0
0
The fourth row is worth a look too. The old version did block, but its error named the model source — a string that only confused me when I read the log later. The revised version states the actual reason.
Twenty runs each in the same environment: 8 ms per call for the revised guard, 6 ms for the old one (Linux, bash 5.1). Two milliseconds to close a fail-open hole is an easy trade.
Decide fast and narrow, and when you cannot read the input, block. For unattended guards, that is the order I keep.
Building the recorder
The PostModelSwitch side appends one JSONL line per switch. Nothing here can affect the switch, so readability wins.
#!/usr/bin/env bash# .claude/hooks/model-switch-log.sh# PostModelSwitch: recording only.set -uo pipefailLOG_DIR="${CLAUDE_PROJECT_DIR:-$PWD}/.claude/logs"mkdir -p "$LOG_DIR"export LOG="${LOG_DIR}/model-switch-$(date -u +%Y-%m).jsonl"python3 -c 'import json, sys, os, datetimed = json.load(sys.stdin)row = { "at": datetime.datetime.now(datetime.timezone.utc).isoformat(timespec="seconds"), "session": d.get("session_id"), "from": d.get("from_model"), "to": d.get("to_model"), "requested": d.get("requested_model"), "source": d.get("source"), "context_tokens": d.get("context_tokens"), "cache_warm": d.get("prompt_cache_warm"), "cache_ttl": d.get("cache_ttl"), "cache_write_usd": d.get("estimated_cache_write_usd"), "pricing": d.get("pricing"),}with open(os.environ["LOG"], "a", encoding="utf-8") as f: f.write(json.dumps(row, ensure_ascii=False) + "\n")' 2>>"${LOG_DIR}/model-switch-error.log"# The switch is already done by now. Do not turn a logging# failure into something that looks like a switching failure.exit 0
That explicit exit 0 is deliberate. For SessionStart, SubagentStart, and PostModelSwitch, exit code 2 causes Claude Code to render the stderr in the transcript as a hook error. A failed append does not deserve that treatment.
Omitting matcher fires the hook on every switch. If you later want to narrow it, note that for PreModelSwitch the matcher is evaluated against the canonical name Claude Code derives from to_model. Recording everything first and narrowing afterwards is the safer order — you cannot pick a filter for behavior you have not observed yet.
The 5-second and 15-second timeouts are the earlier split written into configuration: short for the decision, generous for the record.
These are the ones I actually hit after wiring it up, plus the ones I could reproduce in tests, indexed by symptom.
Symptom
Likely cause
How to check
Fix
No switch is ever recorded
Hooks not loaded / script not executable / Claude Code older than v2.1.251
Both events listed in /hooks; ls -l .claude/hooks; claude --version
chmod +x, update Claude Code, keep settings.json in the project's .claude/
Log file exists but never grows
python3 not on PATH / exception while reading JSON
Read .claude/logs/model-switch-error.log
Use an absolute path to python3; fix the failing line
Every unattended switch is blocked
Allowlist entry does not match to_model (alias vs dated ID, trailing CRLF or spaces)
Look for ^M in cat -A .claude/model-allowlist.txt; compare with column 2 of model-switch-blocked.tsv
Write IDs exactly as to_model reports them, with LF line endings
An unapproved model is not blocked
source arrives as something other than sdk / the old guard cannot read spaced JSON
Check the source column in the log; run the five payloads above
Switch to the revised guard; match the condition to the source values you actually see
Switches occasionally stop with no error left behind
PreModelSwitch timeout (slow means blocked)
Time the hook with time; look for network calls or aggregation
Move heavy work to PostModelSwitch
A logging failure shows up as a hook error in the transcript
PostModelSwitch returning exit code 2
Read the end of the script and whether set -e is on
End with exit 0 and send errors to a file
The CRLF case in row three comes from writing the allowlist in a Windows editor. grep -qxF matches whole lines, so a trailing \r makes an identical-looking ID fail to match, and the revised guard — working correctly — blocks it.
Reading the log: counts, sources, and round trips
Once the JSONL has some volume, aggregate it. Counts alone are not what you are after — you want the breakdown by source, and the pairs that go back and forth.
#!/usr/bin/env python3"""Aggregate recorded switches by source, and surface round trips."""import json, sys, collectionsrows = []for path in sys.argv[1:]: with open(path, encoding="utf-8") as f: rows += [json.loads(l) for l in f if l.strip()]by_source = collections.Counter()cost_by_source = collections.defaultdict(float)pairs = collections.Counter()for r in rows: s = r.get("source") or "unknown" by_source[s] += 1 cost_by_source[s] += r.get("cache_write_usd") or 0.0 pairs[(r.get("from"), r.get("to"))] += 1total = sum(cost_by_source.values())print(f"{len(rows)} switches / estimated cache writes ${total:.4f}")for s, n in by_source.most_common(): print(f" source={s:<8} {n:>3}x ${cost_by_source[s]:.4f}")# A pair that appears in both directions is worth a second look.seen = set()for (a, b), n in pairs.items(): if (b, a) in pairs and (b, a) not in seen: seen.add((a, b)) print(f" round trip: {a} <-> {b} {n}+{pairs[(b,a)]}x")
Round trips get their own line on purpose. When a single session bounces between a light and a heavy model, either the work is sliced too finely or two switching conditions are fighting each other. Neither shape shows up in a raw count.
The cache write that rides along with every switch
There is a reason estimated_cache_write_usd is in the payload at all.
Changing models changes the prompt cache prefix. Whatever was warm before the switch is no longer usable, and the next turn starts with a cache write. The higher prompt_cache_warm was at the moment of the switch, the more you just threw away.
This is where the pricing change that came with Claude Fable 5.1 gets easy to misread (prices as of November 2026; check the official pricing page for current rates). With Claude Fable 5.1, the cut applies to cache reads only — down 75% from the equivalent of $1.00 to $0.25 per MTok. Input at $10 and output at $50 per MTok are unchanged, and cache writes were not reduced either.
Line item
Change
Effect on switch-heavy workloads
Input and output tokens
Unchanged
None
Cache reads
Down 75%
Largest benefit when a warm cache is reused for a long time
Cache writes
Unchanged
Incurred on every switch, eroding the benefit
Anthropic published an index from four weeks of August 2026 usage: roughly 25% lower for a typical workload, up to roughly 45% lower for context- and tool-heavy agentic work. Both figures assume cache reads make up a large share of the bill. A workload that switches often, discarding the cache each time, sits at the low end of that share.
Which is why "25% cheaper" needed checking against my own numbers before I believed it. In my case, having sliced the unattended runs into small stages translated directly into switch count. The switch count, it turns out, is a cost question — and I had no way to see that until the log existed.
Three common misreadings
"Any non-zero exit blocks." Only exit code 2 blocks. Other non-zero codes, such as 1, are treated as non-blocking errors (see the exit code section of the official hooks reference). A guard written with exit 1 prints a warning and lets the switch proceed.
"A timed-out hook just passes." For most events, yes. PreModelSwitch fails closed instead, and that single fact is why this design splits deciding from recording.
"Fable 5.1 made everything 25% cheaper." Only the cache read price dropped. The more often a workload rewrites its cache on a switch, the less of that benefit it sees.
The next thing that breaks: model retirements make the allowlist stale
As of November 2026, the official Model deprecations page lists claude-sonnet-4-5-20250929 as retiring on November 30, with claude-sonnet-5-5 as the recommended replacement. If you move an unattended run to the replacement and forget the allowlist, the revised guard will — correctly — block the switch to the new model.
A guard doing its job stops your work on migration day, and because the run is unattended, you find out the next morning. I head this off by reconciling the allowlist against the logs before the migration.
#!/usr/bin/env python3"""Compare the allowlist against what actually happened: switches that went through and switches that were blocked."""import collections, glob, json, os, sysbase = os.path.join(os.environ.get("CLAUDE_PROJECT_DIR", os.getcwd()), ".claude")with open(os.path.join(base, "model-allowlist.txt"), encoding="utf-8") as f: allow = {l.strip() for l in f if l.strip()}used = collections.Counter()for path in glob.glob(os.path.join(base, "logs", "model-switch-*.jsonl")): with open(path, encoding="utf-8") as f: for line in f: if line.strip(): r = json.loads(line) if r.get("source") == "sdk": used[r.get("to")] += 1blocked = collections.Counter()bpath = os.path.join(base, "model-switch-blocked.tsv")if os.path.exists(bpath): with open(bpath, encoding="utf-8") as f: for line in f: parts = line.rstrip("\n").split("\t") if len(parts) == 2: blocked[parts[1]] += 1print("Blocked switches (decide: add to the allowlist, or fix the caller)")for m, n in blocked.most_common(): print(f" {m:<32} {n}x")print("Allowlisted but never used in unattended runs (check these are not retired)")for m in sorted(allow - set(used)): print(f" {m}")# Exit 1 when anything was blocked, so a scheduled job notices.sys.exit(1 if blocked else 0)
With two blocked switches to claude-sonnet-5-5 and an allowlisted claude-opus-5 that no unattended run has used, it prints:
Blocked switches (decide: add to the allowlist, or fix the caller) claude-sonnet-5-5 2xAllowlisted but never used in unattended runs (check these are not retired) claude-opus-5exit=1
The first group asks you to either add the model or confirm the caller is not pointing at a new model by accident. The second group is permission without use — the place a retired ID hides. That includes the claude-haiku-4-5-20251001 in my example; check every listed ID against the deprecations page from time to time. For counting where an old ID is still being called from, I wrote up 43 Days Until Sonnet 4.5 Retires: I Counted Callers From the Usage CSV, Not From grep.
Where to start
Roll it out in four stages:
Drop in .claude/hooks/model-switch-log.sh and register it on PostModelSwitch only. Nothing gets blocked yet
Let it run for a week, then read the breakdown and round trips with switch-report.py
From the models that actually appear, write the ones you sanction for unattended work into .claude/model-allowlist.txt
Register the revised model-switch-guard.sh on PreModelSwitch, with a short timeout
When a model retirement is announced, run check-allowlist.py against the logs before you migrate
Do steps 1 and 2 first and leave the guard for later. What is worth blocking is not something you can decide before you have seen what actually happens.
I prefer this order for my own automation: drawing the line after observing holds up better than drawing it in advance. When I first wrote this, I planned to settle my allowlist after two weeks of data. These days I lean more on reconciling it against the logs whenever a retirement is announced. A line you drew once may simply be something you redraw each time the models change.
Share
Thank You for Reading
Claude Lab is ad-free, supported entirely by members like you. We publish practical guides daily with implementation code, benchmarks, and production-ready patterns. If you've found it useful, we'd love to have you on board.