feat(llm): enable Bedrock/Anthropic prompt caching for Claude models
Summary
Enables Anthropic/Bedrock prompt caching for Claude models by passing LiteLLM's cache_control_injection_points through ModelSettings.extra_args in make_model_settings.
A Strix scan is a long, multi-turn agentic loop that re-sends a large, stable prefix every turn — the system prompt plus the tool schemas — while only the conversation tail changes. Without a caching breakpoint that whole prefix is re-tokenised and billed at the full input rate on every turn. On Bedrock Claude this is the single biggest lever on scan cost.
Measured on a real scan: cache-read went 0% → 57% with these injection points set — roughly halving input-token cost, and the ratio climbs on longer scans where the stable prefix dominates more turns.
Why this shape (not a ModelSettings flag)
This is deliberately kept at the LiteLLM-config layer rather than a general caching flag on ModelSettings. That's the direction the Agents SDK maintainer explicitly prescribed when declining a native cache_system_prompt field:
"For LiteLLM, the preferred direction is to keep this at the LiteLLM configuration layer. LiteLLM already supports
cache_control_injection_points, andLitellmModelpassesModelSettings.extra_argsthrough tolitellm.acompletion()." — openai/openai-agents-python#3008 (PR #3009 closed on this basis; earlier #905, #1257wontfix)
The SDK owner's rationale: caching is LiteLLM/provider-specific, and a ModelSettings flag would let strict OpenAI-compatible paths emit non-standard cache_control parts (not a guaranteed no-op). Applying it here, in the Strix SDK-consumer layer, is exactly that prescribed pattern — and it's where the provider is already known.
How it works
LiteLLM implements this end to end: when cache_control_injection_points is present in the call kwargs, its AnthropicCacheControlHook fires and emits the provider-appropriate breakpoint (Anthropic cache_control; Bedrock Converse cachePoint), honouring Anthropic's 4-breakpoint cap. LitellmModel forwards ModelSettings.extra_args straight into litellm.acompletion(), so passing the points there is all that's required.
We mark the two big stable segments (2 of the 4 allowed breakpoints, headroom left):
- system prompt (
role: system) — the largest repeated span - tool schemas (
tool_config) — sizeable and identical every turn
Safety / scope
- Gated on Claude (
_is_claude_model) → a strict no-op for every other provider: no injection points means the hook never fires, so nothing non-standard reaches OpenAI / AnyLLM / non-Claude endpoints. - Only Claude-family routes (Anthropic native, Bedrock, Vertex, OpenRouter → Claude) honour the marker.
- Conversation-tail turns left uncached (they change every turn — a breakpoint there would never hit).
Tests
tests/test_inputs.py — parametrised, non-vacuous:
- Claude routes (bedrock / anthropic-native / openrouter) get the two injection points.
- Non-Claude (gpt-5 / gemini / o3) get
extra_args=None.
ruff clean; no new dependencies (LiteLLM support is already present).
Greptile Summary
This PR enables prompt caching for Claude models across supported LiteLLM routes. The main changes are:
- Adds system and rolling transcript cache points.
- Adds Bedrock-specific tool caching with capability checks.
- Preserves existing model arguments when adding cache settings.
- Adds an environment-based cache kill switch.
- Expands route, opt-out, and transcript caching tests.
Confidence Score: 5/5
The updated code looks safe to merge.
- Existing model arguments are preserved when cache settings are added.
- Bedrock-specific cache points are guarded by route capability checks.
- No remaining blocking issue met the scope for this follow-up review.
Important Files Changed
| Filename | Overview |
|---|---|
| strix/config/models.py | Adds Claude and Bedrock route detection with a prompt-caching capability check. |
| strix/config/settings.py | Adds the default-enabled STRIX_PROMPT_CACHE setting. |
| strix/core/inputs.py | Adds route-specific cache injection points while preserving existing extra arguments. |
| strix/core/runner.py | Passes the configured prompt-cache value into scan model settings. |
| tests/test_inputs.py | Adds coverage for cache routes, opt-out behavior, model capability checks, and rolling transcript points. |
| tests/test_runner_rate_limit.py | Updates runner test scaffolding for the prompt-cache setting. |
| tests/test_runner_root_prompt.py | Updates root-prompt test scaffolding for the prompt-cache setting. |
Reviews (3): Last reviewed commit: "chore(llm): trim prompt-caching docstrin..." | Re-trigger Greptile
Thanks @greptile-apps — addressed both in af77695, with a note on where each held up vs. not:
Issue 1 (extra_args replaced): ModelSettings.resolve() actually merges extra_args (there's explicit dict-merge logic in model_settings.py, and unrelated keys like num_retries/timeout survive a resolve()), and make_model_settings builds ModelSettings from scratch so the base extra_args is None here regardless — so nothing was being dropped. That said, the suggestion is a good readability call: I now build {**existing, **cache} explicitly so preservation is obvious at the call site rather than relying on resolve()'s non-obvious merge semantics. No behaviour change.
Issue 2 (tool_config ignored): on current LiteLLM the Bedrock Converse transform does handle location: "tool_config" (converse_transformation.py appends a cachePoint to the tools; verified on litellm 1.90.1), so it isn't silently dropped. The real nuance is graceful degradation: an unrecognised injection-point location on an older pin is simply not injected — no error — so a stale LiteLLM still gets the system-prompt breakpoint (the dominant win). Documented that in the docstring rather than changing behaviour.
Verified live before filing: cache-read 0% → 57% on a real multi-turn scan with these points set.
Both issues are cleanly addressed. The explicit {**existing, **cache} merge is the right call — it's self-documenting and future-proof regardless of what resolve() does under the hood. The graceful-degradation note in the docstring correctly captures the real behavior: unrecognised locations are silently skipped, so an older LiteLLM pin still benefits from the system-prompt breakpoint.
A few small observations on the final state:
-
_is_claude_modelcheck —"claude" in model_name.lower()will also match hypothetical non-Anthropic model names containing "claude" (e.g."some-provider/claude-knockoff"). Almost certainly fine in practice, but worth being aware of. -
location: "message"vs"system"— the injection point is{"location": "message", "role": "system"}. Just confirming you've verified this is the correct key LiteLLM'sAnthropicCacheControlHookexpects for the system prompt slot — the 57% cache-read number confirms it's working, so this is just a doc note. -
Test for
extra_argspreservation — the comment in the implementation calls out "unrelated LiteLLM options are preserved" as a maintenance invariant, but there's no test exercising it (e.g. passing in aModelSettingswith a pre-existingextra_argskey and asserting it survives the merge). Not a blocker, but given the comment specifically highlights this concern, a test would lock it in.
Otherwise this looks solid. The gating, the merge pattern, the docstring, and the parametrised tests are all in good shape. LGTM.
Pushed a follow-up (cc2b335) hardening this against a hard crash I hit running it live.
Problem: the injection is gated on the model name containing "claude", but LiteLLM's AnthropicCacheControlHook only consumes cache_control_injection_points for models it recognises as cache-capable via its (statically bundled) model map. On a Bedrock route whose model isn't in that map, the marker passes straight through to the Converse API, which rejects unknown fields outright:
ValidationException: cache_control_injection_points: Extra inputs are not permitted
That fails the whole run at the first LLM call. It bites any Bedrock Claude model LiteLLM hasn't mapped yet (a just-released model), and is worse when LiteLLM can't refresh its remote model map — e.g. behind a TLS-intercepting corporate proxy — and falls back to a stale local copy. I hit it on bedrock/global.anthropic.claude-sonnet-5.
Fix: withhold the marker only for a Bedrock route LiteLLM can't confirm supports prompt caching (_bedrock_route_without_cache_support). The scope is deliberately narrow — Anthropic-native, Vertex, and OpenRouter Claude tolerate/ignore the marker (or LiteLLM keys them under names we don't resolve), so gating those on confirmed support would disable caching for capable models, the opposite of this PR's intent. Only Bedrock hard-rejects, so only Bedrock is guarded. Unmapped Bedrock model → runs uncached instead of crashing.
Added two tests covering both directions (unmapped Bedrock → no marker; unmapped non-Bedrock → keeps marker).
Pushed efb698a: added a third cache breakpoint on the conversation tail ({"location": "message", "index": -1}), alongside the existing system-prompt and tool_config points.
Why: the two prefix breakpoints only cache the fixed prefix. A scan transcript is append-only, so the growing conversation body was re-sent at full input price every turn and cache-read decayed as the transcript grew — a denominator effect, not the prefix missing. Measured on a 29-turn Bedrock Opus scan: the cached prefix stayed pinned at ~56k tokens while per-turn input climbed to ~256k and cache-read fell 90% → 22%. Because prior turns are immutable, a rolling index: -1 breakpoint re-caches the whole prefix-so-far each turn and hits on the next — modelled cache-read ~96%, full-price input ~16× less on that scan.
Still 3 of the 4 allowed breakpoints, degrades gracefully on older LiteLLM (unrecognised location simply not injected), and the new end-to-end test drives LiteLLM 1.90.1's _apply_message_injections to confirm the point tracks the tail across a growing transcript.
@greptile