Prompt Caching in OpenAI vs Claude: How to Reduce LLM Costs

By Rajit Khosla··4 min read

Imagine building an AI customer support assistant. Every customer request sends the same system instructions, product documentation and support policies to the LLM. Only the customer's question changes.

Why should your application repeatedly pay to process the same information? Prompt caching helps solve this problem by allowing LLM providers to reuse previously processed prompt content. This can reduce input token costs and improve response speed.

Both OpenAI and Anthropic Claude support prompt caching, but their implementations differ.

Let's explore how they work and how to use them effectively.

1. Understanding cache reuse

Consider an AI assistant receiving hundreds of customer questions. Each request contains:

Prompt partSizeChanges per request?
System instructions1,000 tokensNo
Product documentation3,000 tokensNo
Customer question50 tokensYes

The first two remain unchanged, while the customer question changes.

For caching to work, providers look for matching prompt prefixes. This makes prompt structure important.

2. How does OpenAI prompt caching work?

OpenAI supports automatic prompt caching for eligible models. You generally don't need additional configuration.

For example, using the Responses API:

openai_caching.py
python
from openai import OpenAI

client = OpenAI()

response = client.responses.create(
  model="YOUR_SUPPORTED_MODEL",
  input=[
      {
          "role": "developer",
          "content": """
          You are a customer support assistant.
          Follow company support policies.

          [Reusable company documentation]
          """
      },
      {
          "role": "user",
          "content": "Can I return a product after 20 days?"
      }
  ]
)

print(response.output_text)

# Check cached tokens
print(response.usage.input_tokens_details.cached_tokens)

OpenAI automatically identifies eligible matching prefixes and reuses cached content when available.

For supported models, you can also use explicit cache breakpoints and prompt_cache_key for additional control. Cache eligibility and retention depend on the model. Supported newer models offer different cache retention options. Read the documentation for more detail.

3. How does Claude prompt caching work?

Anthropic supports both automatic caching and explicit cache configuration through its Messages API.

You can use cache_control to mark reusable content:

claude_caching.py
python
import anthropic

client = anthropic.Anthropic()

response = client.messages.create(
  model="YOUR_SUPPORTED_CLAUDE_MODEL",
  max_tokens=500,
  system=[
      {
          "type": "text",
          "text": """
          You are a customer support assistant.
          Follow company support policies.

          [Reusable company documentation]
          """,
          "cache_control": {
              "type": "ephemeral"
          }
      }
  ],
  messages=[
      {
          "role": "user",
          "content": "Can I return a product after 20 days?"
      }
  ]
)

print(response.content)

The cache_control setting marks the reusable content for caching.

Claude offers two cache lifetime options:

  • 5 minutes: Default cache lifetime.
  • 1 hour: Extended cache lifetime with a higher cache-write cost.

Minimum cacheable tokens depend on the model.

Explicit cache breakpoints are useful when you want more control over reusable content in applications involving large documents, RAG pipelines or AI agents.

Refer: Anthropic – Prompt Caching for more details

5. How to optimise prompt caching

Enabling caching is only the first step. Your application architecture also determines how effectively you reuse cached content.

Here are four practical recommendations:

  1. 1

    Keep reusable content first

    Place system instructions, shared documentation and tool definitions before dynamic user messages.

  2. 2

    Avoid unnecessary changes

    Avoid adding timestamps, random IDs or request-specific information at the beginning of prompts. Changes to the prefix can prevent cache reuse.

  3. 3

    Keep tool definitions stable

    For applications using function calling or AI agents, avoid unnecessarily changing tool descriptions and schemas between requests.

  4. 4

    Choose the right cache lifetime

    Consider how frequently your application reuses the same content. Longer cache lifetimes can help, but may increase cache-write costs.

6. Monitor caching in production

Don't assume caching is saving money just because it is enabled. Monitor the usage information returned by the APIs.

usage_openai.py
python
print(response.usage.input_tokens_details.cached_tokens)

Track three important metrics:

  • Cache hit rate: How many input tokens are being reused?
  • Cache writes: How many tokens are being added to the cache?
  • Cost and latency: Are you actually reducing API costs and response times?

Compare these metrics before and after enabling caching.

Conclusion

Prompt caching is a useful optimisation for production LLM applications that repeatedly send the same instructions, documents or tool definitions. OpenAI provides automatic caching with additional configuration options, while Claude offers automatic and explicit caching through cache_control.

Want to build practical AI applications?

Auragile's AI Engineering course teaches developers by building real-world projects, from LLM-powered applications to more advanced AI systems.

Explore the course →

Get new articles by email

Get new Auragile articles and course news by email. Unsubscribe anytime. See our Privacy Policy.