← All news

News / Learn / Working with model APIs

What is prompt caching and how much does it save?

You'll be able to explain how prompt caching works and structure your prompts to get the savings.

Prompt caching lets a model provider reuse work it already did on a prefix of your prompt. If many of your requests start with the same long text, such as a system prompt or a large document, the provider can skip re-processing that shared part. You pay less for it, and the response often starts faster.

Many AI products send the same long instructions or reference material with every request. Without caching, you pay to process that text again each time. With caching, repeated prefixes cost less and respond sooner, which matters a lot once you have real traffic.

Models read a prompt as a sequence of tokens and build internal state for each one. Processing that state is the expensive part of a long prompt. A cache stores that state for a prefix, keyed on the exact text from the start of the prompt up to some point.

Caching only works on a prefix. The provider matches your new request against cached prefixes from the beginning. If the first token differs, nothing is reused. If the first 5,000 tokens match a stored entry, those tokens can be reused and only the remainder is processed fresh.

This means order matters. Put stable content first: system instructions, tool definitions, and reference documents. Put variable content last: the user's question, today's data, and the latest turn of a conversation. Small changes early in the prompt, such as a timestamp in the system prompt, break the match for everything after it.

Cache entries expire after a period of inactivity, and providers have their own rules on minimum prefix length and how caching is requested. Check your provider's documentation for the exact rules, since they change over time.

The savings depend on your hit rate: how often a request reuses a cached prefix, and how long that prefix is. A long shared prefix sent many times in a short window gives the biggest gains. A unique prompt per user gives almost none.

A stable prefix first, variable input last. This is the shape that lets caching work.

SYSTEM_PROMPT = open("policy_guide.txt").read()  # long, rarely changes

def build_messages(user_question: str):
    return [
        {"role": "system", "content": SYSTEM_PROMPT},   # stable prefix first
        {"role": "user", "content": user_question},     # variable part last
    ]

# Avoid putting per-request values such as timestamps
# or request IDs inside SYSTEM_PROMPT, because they break the prefix match.

A short sketch to show the idea. Check the official docs of the tool you use.

  • Putting variable data at the top Move timestamps, user names, and request IDs to the end of the prompt. Keep the stable instructions and documents at the start.
  • Assuming every request gets cached Caching depends on an exact prefix match and on timing. Measure your hit rate in the provider's usage data instead of assuming savings.
  • Reordering tools or examples between calls Serialize tool definitions and few-shot examples in a fixed order. A different order produces a different prefix and a cache miss.
  • Forgetting that expiry exists Cached entries expire. If your traffic is sparse, expect fewer hits and plan costs with that in mind.

Learn how your provider reports cached tokens in usage data, then build a small test that logs cache hits across a sample of requests. Read about batch APIs and context window limits, since both affect cost and design.

Does prompt caching change the answer I get?

It should not. Caching reuses computed state for the same input text. The output still comes from the same prompt, although sampling settings like temperature still introduce normal variation.

Can I cache the middle of a prompt?

Generally no. Matching starts at the beginning of the prompt and runs forward, so only a shared prefix can be reused.

Is caching the same as storing my responses?

No. Response caching returns a saved answer for a repeated question. Prompt caching reuses internal processing of the input, and the model still generates a new response.

How do I know how much I saved?

Look at the cached token counts in your provider's usage report and compare them to total input tokens. Your provider's pricing page gives the discount for cached input.

Written with AI by CapyLaunch. Spotted a mistake? Email hello@capylaunch.com.