Repository navigation
fix(anthropic): automatic prompt caching and a fallback for refused thinking "disabled" - #14
Open
jedrzejsieracki wants to merge 3 commits into
Open
jedrzejsieracki wants to merge 3 commits into
jedrzejsieracki wants to merge 3 commits into
Conversation
Anthropic requests only carried a cache breakpoint on the system prompt,
and only when the caller set DynamicBoundary. The conversation history
was never cached, so each turn of an agent loop paid full input price
for it again; with no boundary set nothing was cached at all, tools and
system included, and cache_read_input_tokens stayed at zero.
Send the top-level "cache_control":{"type":"ephemeral"} field on every
Anthropic request. The API places the breakpoint on the last cacheable
block and moves it forward as the conversation grows, so the next turn
reads everything up to the previous one from the cache. It uses the
default 5-minute TTL, like the system breakpoint, and takes one of the
four breakpoint slots.
Config.DisableAutoCache omits the field for endpoints that reject it,
such as the legacy Bedrock InvokeModel integration or gateways that
refuse unknown top-level keys.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
With automatic caching on, the API writes the whole prompt of every Anthropic request to the cache at the write premium (1.25x). For a request whose prompt is never sent again nothing reads that entry back, so the write is pure surcharge. The compaction summary is such a request: its own system prompt plus up to the whole history, sent once. CompletionRequest.NoAutoCache omits the top-level cache_control from that request only; Config.DisableAutoCache still turns it off for the whole provider. The compaction summarizer sets it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Current models reject thinking {"type":"disabled"} with a 400: Claude
Sonnet 5.5 turns thinking off only with "between_tools", Claude Opus 5.5
cannot turn it off at all, and Claude Haiku 5.5 not above high effort.
Callers that set "disabled" to keep one request cheap, such as the
compaction summarizer, failed outright on those models, so compaction
never ran.
When a request that sent "disabled" (per request or from Config) gets a
400 that mentions thinking, send it once more with "between_tools" if
the error names it, otherwise with "adaptive". Any other error, status
or thinking mode is returned unchanged without a retry. Stream and
Complete share the new send step. Replayed thinking blocks stay stripped
in both fallback modes.
The Sonnet 5.5 error text is the one ARTEX already matches in its own
compaction fallback; the Opus 5.5 text was not available, so the
fallback keys on the 400 plus "thinking" rather than on exact wording.
Verified by mutation: dropping the retry, the "between_tools" branch,
the mode check, the status check, the "thinking" check or the Config
mode each fail a test.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
An ARTEX deployment using Claude Sonnet 5.5 through the Anthropic API records 0 cache reads and 0 cache writes on every call (49 calls, about 1.2M input tokens). On the Norma side, two things contribute:
DynamicBoundary. Every turn of an agent loop pays full input price for the whole history again.thinking: {"type": "disabled"}returns a 400 on Claude Sonnet 5.5 (which turns thinking off only with"between_tools"), on Claude Opus 5.5 (where thinking can't be turned off) and on Claude Haiku 5.5 above high effort. The compaction summarizer sets"disabled"on every request, so on those models compaction never succeeds.Changes (3 commits)
fix(anthropic): turn on automatic prompt caching"cache_control": {"type": "ephemeral"}. The API places the breakpoint on the last cacheable block and moves it forward as the conversation grows.Config.DisableAutoCacheturns it off for endpoints that reject the field, such as the legacy Bedrock InvokeModel integration or strict gateways.feat(llm): let one request opt out of automatic cachingCompletionRequest.NoAutoCache. A one-shot request whose prompt is never read back would otherwise pay the 1.25x write premium on all of it.fix(anthropic): retry once when the model refuses thinking "disabled""disabled"(per request or fromConfig) and gets a 400 mentioning thinking is sent once more:"between_tools"if the error names it;"adaptive"."disabled".Thinking-block replay is deliberately unchanged. Replaying blocks when thinking is unset would not help caching, because stripping them consistently keeps the prefix byte-identical. It would also turn client-side compaction (microCompact, keep-tail AutoCompact) into 400s on accounts enforced by the history-editing check.
Testing
New tests:
anthropic_cache_test.go:DisableAutoCacheand withNoAutoCache;compaction/spec3_test.go: the summarizer setsNoAutoCache.anthropic_thinking_fallback_test.go, covering bothStreamandComplete:between_tools;adaptive;disabledfalls back the same way;Mutation checks on the fallback: dropping the retry, the
between_toolsbranch, the mode check, the status check, the "thinking" check or the Config mode each makes a test fail.Test runs:
go test ./...: everything passes except three tests that also fail on unmodifiedmain:llmTestDoStreamHonorsConfiguredRetriespanics; fix(llm): doStream falls back to http.DefaultClient on a nil HTTPClient #12 looks like the fix.noaTestAdaptiveGrowthTracksTheWindowandTestSuppressionReleaseFitsInsideTheWindow.replace).Not tested:
Related
The ARTEX side is BaaRai/ARTEX#706. It works with the current Norma pin and gets history caching once ARTEX bumps to a Norma release with this change.
🤖 Generated with Claude Code