Retrieval-augmented chat with 90% cache savings.
Embed your docs once. Put the stable system prompt in the right place so provider cache fires on every query. Pay 10% on the cached prefix, full price only on the small user message.
Why this pattern — Cache the prefix, not the question
A naive RAG chain puts everything in the user message — instructions, retrieved chunks, and the question. The system prompt is empty. This works but the entire prompt is uncached on every call.
A smarter chain puts the stable instructions in the system prompt (rare changes) and the variable retrieval + question in the user message. Anoman auto-injects cache markers when the system prompt is long enough; subsequent calls hit the provider prompt cache and pay 10% on the cached prefix.
See /docs/concepts/caching for the cache mechanics in detail.
1. Index — Embed once, batch-style
Embeddings are cheap (~$0.02 per million tokens for text-embedding-3-small). Batch 100 chunks per call to amortize HTTP overhead.
Index docs with embeddings
2. Query with cacheable system prompt — The pattern
Query — embed + retrieve + chat
Build the chat call with cacheable system prompt
Cache anti-pattern: if you put retrieved chunks into the system prompt, every query has a different system prompt and the cache never fires. Keep system stable; put context in the user message.
3. Verify the cache fired — Read cached_tokens
Verify the cache is firing
For Anthropic models the system prompt must be ≥ 2,048 tokens to be cacheable. For OpenAI it's automatic for any length. For Google it's 32,768+ tokens (via explicit CachedContent API). See the per-provider table at /docs/concepts/caching.
4. Stream for UX — Streaming + cache stack
Set stream: true. Provider cache still applies — you save 90% on the prefix and stream the small variable part token-by-token.
Stream retrieval + streaming response
Production tips
- Cache lifetime is ~5 minutes. Idle traffic doesn't benefit. For sparse-traffic apps, send a synthetic warming request every 4 minutes to keep the cache hot.
- System prompt versioning — any change invalidates the cache. Use a content-hash version comment at the top of the system prompt so you can detect accidental drift in your code.
- Don't cache user-PII in the system prompt — Anoman's PII guardrails apply, but you shouldn't persist sensitive customer data in a multi-tenant cache layer.
- Response cache on top — for FAQ-style apps where the same question repeats at
temperature: 0, turn on Anoman's response cache for the API key (opt-in, exact match, rolling out). Exact repeats can then be served from Anoman's cache; they are billed at the normal rate. - Reranking after retrieval — top-k by cosine is rough. For higher quality, pass the candidates through a cheap rerank model (
qwen-2.5-7b) before stuffing into the user message.
Inspect cache savings live.
Usage page shows cache_savings_usd as a separate column.