Skip to content

feat(server): cache prompts of Chat Completions and Responses requests on Messages targets - #163

Merged
deLiseLINO merged 1 commit into
deLiseLINO:mainfrom
erikfrish:feat/auto-prompt-cache
Dec 3, 2026
Merged

deLiseLINO merged 1 commit into
deLiseLINO:mainfrom
erikfrish:feat/auto-prompt-cache

Conversation

@erikfrish

@erikfrish erikfrish commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Why

A Claude model reached through /v1/chat/completions or /v1/responses gets no prompt caching, so every turn of an agent session pays for the whole prompt again. You reproduced it: repeated 36k-token prompts through these routes read 0 cached tokens, while /v1/messages with cache_control reads them all from the cache.

OpenAI's Chat Completions and Responses APIs carry prompt_cache_options (mode, ttl) and a per-block prompt_cache_breakpoint (prompt caching guide). Prism maps them, and in implicit mode adds marks of its own, as block-level cache_control on Messages targets. A request-level mark is not used, since some Anthropic-compatible upstreams reject it.

Scope

  • internal/promptcache:
    • Prepare runs once for a Chat Completions or Responses request. It gives the client's explicit marks without a ttl the request's lifetime, fits them to Anthropic's limits, and in implicit mode records that a target may add marks (canon.PromptCacheOptions.Implicit).
    • Place runs in the Anthropic runner for a target that allows automatic caching. On a copy of the request it marks the last tool, the end of the system prompt or instructions, the latest message or tool result, and the user message before it, within four marks counting the client's. It then drops message marks first beyond four and shortens a 1h mark that follows a shorter one in wire order, as Anthropic requires. Because it works on a copy, a failover to another target starts from the client's marks again.
  • internal/ingress/openaicache reads prompt_cache_options and prompt_cache_breakpoint for both ingresses, on text and image blocks. ttl "30m" maps to 5 minutes and "12h" to 1 hour, the closest Anthropic lifetimes. An unsupported mode or ttl is a 400, as OpenAI answers. mode: "explicit" keeps only the client's marks.
  • autoPromptCache on a provider decides for that provider, else the top-level autoPromptCache; both are on when absent. The planner resolves it into provider.Target.AutoPromptCache. hostsync.Edits carries the top-level value as the host's effective setting, so "not edited" (nil) is told apart from "reset to default" (true), and a host's own value survives a push that does not take it. The provider field travels with the provider in host sync.
  • Server.turn reads the config once and passes the vision sidecar settings to visionSidecar. Prepare runs after the sidecar replaces images.
  • promptCacheKeyNote no longer says there is no cache_control synthesis.
  • Removes intPtr in internal/config/config_test.go and decodeJSON in internal/providers/anthropic/request.go, which staticcheck reports as unused.
  • verify/features/daemon-api.md describes the behavior.

Blast Radius

A Chat Completions or Responses request to a Messages target with automatic caching on now carries up to four block-level cache_control marks. A prompt not repeated within the lifetime pays the cache write once. /v1/messages is unchanged. A client sending an unsupported prompt_cache_options or prompt_cache_breakpoint value now gets a 400 instead of having the field ignored. With autoPromptCache: false on a provider, its requests carry only the client's explicit marks.

Tradeoffs

The client's explicit marks are mapped to cache_control, not dropped in favour of Prism's own. Dropping them would leave a client in explicit mode with no caching at all. Prism also does not add a request-level mark for first-party api.anthropic.com: block-level marks work there too, and one path is simpler to keep correct.

Verification

  • go build ./..., go vet ./... and go test -race ./... pass; staticcheck on the changed packages reports nothing.

  • internal/promptcache tests: the four implicit marks on a tool-using turn and no change to the shared request, a marked latest tool result, a 12h lifetime, explicit mode keeping only the client's two marks, an unprepared request left alone, the cap of four and the 1h ordering. TestBuildRequestPlacesImplicitCacheMarksPerTarget: block marks on the wire when the target allows caching, none when it does not, never a request-level mark. TestPlannerResolvesAutoPromptCachePerProvider: a provider's own value wins over the top-level one both ways. TestPromptCachePreparedForOpenAIProtocols and TestPromptCacheOptionsRejectUnsupportedValues cover the server and the 400s. TestHostEditsSurviveAPushThatDoesNotTakeThem now also turns autoPromptCache off on the host.

  • A daemon from this branch on my home install, a three-turn conversation with a tool call, about 14k tokens per turn:

    Route and target Turn 1 cached Turn 2 cached Turn 3 cached
    /v1/chat/completions, Dia Sonnet 0 of 14,074 14,070 of 14,212 14,210 of 14,237
    /v1/chat/completions, second Messages upstream, Haiku 0 of 13,800 13,796 of 13,861 13,859 of 13,886
    /v1/responses, explicit mode, one breakpoint on the developer message 0 of 13,443 13,428 of 13,443 13,428 of 13,443

    In explicit mode only the marked developer prefix is read from the cache, as asked; no request failed.

Model: claude-opus-5-5

@deLiseLINO

Copy link
Copy Markdown
Owner

Thanks, reproduced on our side: on a Messages-wire upstream, repeated 36k-token prompts through /v1/chat/completions and /v1/responses get 0 cached tokens, while /v1/messages with cache_control reads them all from the cache. The fix is needed. A few changes before merge:

  1. Use block-level breakpoints instead of request-level cache_control. Not every Anthropic-compatible upstream accepts the top-level field (e.g. LiteLLM → Bedrock returns 400: fix(bedrock/messages): convert top-level cache_control to per-block caching when routing /v1/messages to Bedrock BerriAI/litellm#26320), and Prism treats that 400 as a request error, so no fallback runs. Block-level breakpoints are accepted everywhere: mark the last tool, the last system block, and the last one or two user messages (max 4 in total, fewer if the client already placed its own). Request-level cache_control could stay only for first-party api.anthropic.com.
  2. Respect OpenAI's cache options. With prompt_cache_options.mode: "explicit" the client asks for no caching without explicit breakpoints, so Prism should not add any. Explicit prompt_cache_breakpoint markers could map to cache_control on the same blocks.
  3. Host sync. autoPromptCache is missing from hostsync.Edits / Diff / Keep / Apply, so autoPromptCache: false set on a host is silently reset on the next sync. It needs to tell "not edited" apart from "reset to default".
  4. Per-provider switch. A single global autoPromptCache means one upstream that rejects caching forces turning it off for every provider. Please make it configurable per provider, with the global value as the default.
  5. Minor: promptCacheKeyNote in internal/providers/anthropic/anthropic.go still says "no cache_control synthesis". Also, turn copies the whole config just to read this flag, and visionSidecar reads it again; one snapshot would do.

@erikfrish

erikfrish commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks for the review. All five points are addressed; the branch is now one commit, 55902ef.

  1. Block-level breakpoints. No request-level cache_control is sent any more. internal/promptcache marks the last tool, the end of the system prompt, the latest message or tool result, and the user message before it, within four marks counting the client's. Beyond four it drops message marks first, and it shortens a 1h mark that follows a shorter one, as Anthropic requires. There is no request-level mark for first-party api.anthropic.com either, since block-level marks work there too.
  2. OpenAI cache options. Both ingresses read prompt_cache_options (mode, ttl) and prompt_cache_breakpoint on text and image blocks. mode: "explicit" adds nothing; explicit breakpoints map to cache_control on the same blocks ("30m" → 5m, "12h" → 1h). Unsupported values return 400.
  3. Host sync. hostsync.Edits carries autoPromptCache as the host's effective value through Diff / Keep / Apply, so nil means "not edited" and true means "reset to default". TestHostEditsSurviveAPushThatDoesNotTakeThem turns it off on the host and checks it survives.
  4. Per-provider switch. autoPromptCache on a provider overrides the top-level one. The planner resolves it into Target.AutoPromptCache, and the Anthropic runner adds the implicit marks per target, on a copy, so a combo that fails over to a provider with caching off sends only the client's marks.
  5. Minor. promptCacheKeyNote is fixed; turn reads one config snapshot and passes the sidecar settings to visionSidecar.

go test -race ./... and go vet ./... are clean, and staticcheck finds nothing in the changed packages. On a live daemon, a three-turn tool conversation through /v1/chat/completions reads 99% of the prompt from the cache from turn 2 on two different Messages upstreams, and /v1/responses in explicit mode reads only the marked prefix. Details are in the updated description.

@erikfrish erikfrish closed this Dec 3, 2026
@erikfrish
erikfrish force-pushed the feat/auto-prompt-cache branch from 25cc9c4 to d54190c Compare December 3, 2026 19:01
…s on Messages targets

- Messages targets add block-level cache_control marks to Chat Completions and Responses requests: the last tool, the system prompt, the latest message and the user message before it, within four marks.
- Read prompt_cache_options and prompt_cache_breakpoint; explicit mode keeps only the client marks.
- autoPromptCache per provider, falling back to the top-level setting, which a host keeps across host sync.
@erikfrish erikfrish reopened this Dec 3, 2026
@deLiseLINO
deLiseLINO merged commit f3d00f5 into deLiseLINO:main Dec 3, 2026
3 checks passed
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.

2 participants