Skip to content

fix(anthropic): automatic prompt caching and a fallback for refused thinking "disabled" - #14

Open
jedrzejsieracki wants to merge 3 commits into
BaaRai:mainfrom
jedrzejsieracki:fix/anthropic-prompt-cache
Open

jedrzejsieracki wants to merge 3 commits into
BaaRai:mainfrom
jedrzejsieracki:fix/anthropic-prompt-cache

Conversation

@jedrzejsieracki

@jedrzejsieracki jedrzejsieracki commented Nov 30, 2026 •

Copy link
Copy Markdown

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:

  • History is never cached. Anthropic requests only get a breakpoint on the system prompt, and only when the caller sets DynamicBoundary. Every turn of an agent loop pays full input price for the whole history again.
  • Turning thinking off fails on current models. 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)

  1. fix(anthropic): turn on automatic prompt caching
    • Every Anthropic request now carries the top-level "cache_control": {"type": "ephemeral"}. The API places the breakpoint on the last cacheable block and moves it forward as the conversation grows.
    • It uses the 5-minute TTL, like the system breakpoint, so a request has at most 2 breakpoints.
    • Config.DisableAutoCache turns it off for endpoints that reject the field, such as the legacy Bedrock InvokeModel integration or strict gateways.
  2. feat(llm): let one request opt out of automatic caching
    • Adds CompletionRequest.NoAutoCache. A one-shot request whose prompt is never read back would otherwise pay the 1.25x write premium on all of it.
    • The compaction summarizer sets it.
  3. fix(anthropic): retry once when the model refuses thinking "disabled"
    • A request that sent "disabled" (per request or from Config) and gets a 400 mentioning thinking is sent 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.
    • Replayed thinking blocks stay stripped, as with "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:
    • the field is sent by default;
    • it is omitted with DisableAutoCache and with NoAutoCache;
    • it never appears in the OpenAI format.
  • compaction/spec3_test.go: the summarizer sets NoAutoCache.
  • anthropic_thinking_fallback_test.go, covering both Stream and Complete:
    • the Sonnet 5.5 error text leads to a retry with between_tools;
    • another thinking rejection leads to a retry with adaptive;
    • a Config-level disabled falls back the same way;
    • there is no retry for an unrelated 400, a non-400 status, or another thinking mode.

Mutation checks on the fallback: dropping the retry, the between_tools branch, 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 unmodified main:
  • ARTEX's full test suite passes against this branch (via a local replace).

Not tested:

  • No live API calls were made.
  • The Sonnet 5.5 rejection text is the one ARTEX already matches in production code. The Opus 5.5 wording wasn't available, so the fallback keys on the 400 plus "thinking" rather than on exact text.

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

Jędrzej Sieracki and others added 3 commits November 30, 2026 10:42
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant