Docs

Prompt caching

Cached prompt tokens cost $0.05 per 1M instead of $0.20. There is nothing to turn on: it happens on its own whenever the start of your prompt matches a recent one. This page is about keeping it that way.

How it works

The GPU keeps recently read prompts in memory. When a new request begins with exactly the same tokens as one it holds, it reuses them instead of reading them again. The match is a prefix: it runs from the first token until the first difference, and everything after that difference is read fresh.

Each response tells you how much matched, in usage.prompt_tokens_details.cached_tokens. That is the number we bill at the cached price.

Cached prompts live in memory for a few minutes and can be pushed out by other traffic, so the first message after a long break usually reads in full. That's expected.

Why it matters so much for long conversations

A chat frontend resends the whole conversation every turn: system prompt, character card, then every message so far, plus the new one. Turn 40 is turn 39 with two messages added at the end. If nothing at the top changes, almost the whole prompt is a cache hit.

12,000-token prompt, 300-token replyCost
Stable prompt, 11,500 tokens cached$0.0009
A timestamp at the top, 40 tokens cached$0.0026

Rules of thumb

  1. Put what never changes first. System prompt, then the character brief, then examples. Same text, same order, every turn.
  2. Append, don't rewrite. Add new messages at the end of the history. Editing an old message, or re-summarizing the history every turn, changes everything after the edit.
  3. No changing values near the top. The current time or date, a random seed phrase, a "messages so far" counter or a mood that updates each turn all belong at the end of the prompt, after the history, if anywhere.
  4. Inject late. Retrieved notes, memory snippets and per-turn instructions that come and go should be inserted near the end (a few messages from the bottom), not above the brief.
  5. Trim in chunks. When the history outgrows the context, dropping the oldest message every turn shifts the whole history each time. Summarize a block of old messages once, then keep that summary fixed for a while.
  6. Keep whitespace and formatting stable. A different line ending or an extra space at the top is a different prefix.

Check your own hit rate

The console's Usage page shows prompt and cached tokens per request. A steady chat should show most of each prompt cached after the first turn. If every request shows close to zero, something near the top of your prompt changes each turn: look for a timestamp, a random value or a retrieved note inserted before the brief.