What this project supports
Prompt caching lets supporting upstream models reuse input computation; each call still requests a new generation. It does not cache the final answer or replace conversation history. Recognized fields and conversion boundaries are:
The current Chat DTO does not declare top-level
prompt_cache_options. Upstream support in a new version does not establish support through ordinary Chat forwarding here. Content-block extensions and native Claude cache markers are not universally lossless across protocols either. Global or channel passthrough is deployment configuration, not a default assumption for all callers.
The gateway has configurable channel-affinity rules; for example, Responses can extract an affinity key from prompt_cache_key. Rules depend on paths, models, grants, and session policy. Affinity does not guarantee cache hits. Do not import OpenRouter’s default sticky-routing, TTL, or session_id semantics. No general X-OpenRouter-Cache response-cache contract is established by this documentation’s verified scope.
Prepare reusable prefixes
Place stable instructions, tool definitions, and reference material first, with changing questions at the end. Keep effective content and ordering consistent; do not pad requests with repetition just to reach a length threshold. Trimming, summaries, channel changes, and model-parameter changes can affect reuse. Minimum input lengths, lifetimes, cache-write prices, and cache-read prices depend on the model and deployment. Use cache keys scoped by tenant and content version without personal information or secrets; a cache key is not an access-control boundary.store: false does not guarantee that all caching and logging are disabled.
Runnable: observe cache counts across two requests
Save UTF-8 reference material approved for transmission to the deployment asreference.txt. This example reads one regular file up to 64 KiB, keeps its prefix stable, and changes the final question. It makes two real generation calls without automatic retries. Use the REST runtime, keeping examples/prompt-cache.mjs and http-client.mjs together.
cached_tokens: null means the field was absent; 0 means zero hits were reported. A zero on the second request is not an example failure. Both answers must complete; either failure stops the example without printing the final comparison. Confirm that the model accepts prompt_cache_key and that the reference length suits it. Local fixtures do not establish a real cache-hit rate.
Interpret and troubleshoot counts
Common fields areusage.prompt_tokens_details.cached_tokens for Chat and usage.input_tokens_details.cached_tokens for Responses; native Claude uses cache_read_input_tokens and cache_creation_input_tokens. Fields may be absent, and adapters can normalize usage. Do not mechanically add counters from different protocols or derive a final invoice from hit counts.
For zero hits, check the identical effective prefix and its length, model support, fields retained during conversion, upstream switching, expiry, and returned usage metadata. Correlate logs using observability without logging prompt bodies.
Sources: revision 30a6d88a4, relaykit/dto/openai_request.go, relaykit/dto/claude.go, Chat / Responses converters, and setting/operation_setting/channel_affinity_setting.go. Topic organization references OpenRouter’s caching guide; see OpenAI caching documentation for upstream policy.
Chat API · Responses API · Context management
阅读中文