Repository navigation
feat(server): cache prompts of Chat Completions and Responses requests on Messages targets - #163
Merged
Conversation
Owner
|
Thanks, reproduced on our side: on a Messages-wire upstream, repeated 36k-token prompts through
|
Contributor
Author
|
Thanks for the review. All five points are addressed; the branch is now one commit,
|
erikfrish
force-pushed
the
feat/auto-prompt-cache
branch
from
December 3, 2026 19:01
25cc9c4 to
d54190c
Compare
…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.
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.
Why
A Claude model reached through
/v1/chat/completionsor/v1/responsesgets 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/messageswithcache_controlreads them all from the cache.OpenAI's Chat Completions and Responses APIs carry
prompt_cache_options(mode,ttl) and a per-blockprompt_cache_breakpoint(prompt caching guide). Prism maps them, and in implicit mode adds marks of its own, as block-levelcache_controlon Messages targets. A request-level mark is not used, since some Anthropic-compatible upstreams reject it.Scope
internal/promptcache:Prepareruns once for a Chat Completions or Responses request. It gives the client's explicit marks without attlthe request's lifetime, fits them to Anthropic's limits, and in implicit mode records that a target may add marks (canon.PromptCacheOptions.Implicit).Placeruns 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 orinstructions, 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/openaicachereadsprompt_cache_optionsandprompt_cache_breakpointfor both ingresses, on text and image blocks.ttl"30m"maps to 5 minutes and"12h"to 1 hour, the closest Anthropic lifetimes. An unsupportedmodeorttlis a 400, as OpenAI answers.mode: "explicit"keeps only the client's marks.autoPromptCacheon a provider decides for that provider, else the top-levelautoPromptCache; both are on when absent. The planner resolves it intoprovider.Target.AutoPromptCache.hostsync.Editscarries 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.turnreads the config once and passes the vision sidecar settings tovisionSidecar.Prepareruns after the sidecar replaces images.promptCacheKeyNoteno longer says there is no cache_control synthesis.intPtrininternal/config/config_test.goanddecodeJSONininternal/providers/anthropic/request.go, which staticcheck reports as unused.verify/features/daemon-api.mddescribes 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_controlmarks. A prompt not repeated within the lifetime pays the cache write once./v1/messagesis unchanged. A client sending an unsupportedprompt_cache_optionsorprompt_cache_breakpointvalue now gets a 400 instead of having the field ignored. WithautoPromptCache: falseon 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-partyapi.anthropic.com: block-level marks work there too, and one path is simpler to keep correct.Verification
go build ./...,go vet ./...andgo test -race ./...pass;staticcheckon the changed packages reports nothing.internal/promptcachetests: the four implicit marks on a tool-using turn and no change to the shared request, a marked latest tool result, a12hlifetime, 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.TestPromptCachePreparedForOpenAIProtocolsandTestPromptCacheOptionsRejectUnsupportedValuescover the server and the 400s.TestHostEditsSurviveAPushThatDoesNotTakeThemnow also turnsautoPromptCacheoff 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:
/v1/chat/completions, Dia Sonnet/v1/chat/completions, second Messages upstream, Haiku/v1/responses, explicit mode, one breakpoint on the developer messageIn explicit mode only the marked developer prefix is read from the cache, as asked; no request failed.
Model: claude-opus-5-5