> ## Documentation Index
> Fetch the complete documentation index at: https://docs.anyapi.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Prompt caching

# Stop Paying Twice for the Same Thing

*Turn repetitive prompts into serious savings*

Got a chunky system prompt you use over and over? Large context windows eating your budget alive? Prompt caching is your new best friend—store frequently used content once, then reference it at a fraction of the cost.

## 💰 The Economics of Caching

*When the math actually makes sense*

**The problem:** You're paying full price every time you send that 5,000-token system prompt—even when it's identical to the last 100 requests.

**The solution:** Cache it once, reuse it cheap. Here's what you'll save:

| Provider             | Cache Write Cost                              | Cache Read Cost      | Your Savings    |
| -------------------- | --------------------------------------------- | -------------------- | --------------- |
| **OpenAI**           | Free (automatic)                              | 0.1x–0.5x original   | 50–90% off      |
| **Anthropic Claude** | 1.25x original (5 min) / 2x original (1 hour) | 0.1x original        | 90% off reads\* |
| **Grok (xAI)**       | Free (automatic)                              | 0.1x original        | 90% off         |
| **DeepSeek**         | Free (automatic)                              | 0.1x original        | 90% off         |
| **Google Gemini**    | Auto (implicit) / standard input (explicit)   | 0.1x original (2.5+) | 90% off         |

*After the initial write cost, you're looking at massive savings on repeated use*

## 🎯 When Caching Makes Bank

**Perfect for:**

* 📚 Large system prompts (1000+ tokens)
* 🔄 Repeated context in conversations
* 📖 Reference documents sent with every request
* 🤖 Multi-turn conversations with consistent setup

**Skip it for:**

* ⚡ One-off requests
* 🎲 Constantly changing prompts
* 📏 Tiny system messages under the provider's minimum token threshold

## 🛠️ Implementation That Actually Works

### The Basic Setup

*Cache your hefty system prompt once, reference it forever*

```json theme={"system"}
{
  "model": "anthropic/claude-sonnet-4",
  "messages": [
    {
      "role": "system",
      "content": [
        {
          "type": "text",
          "text": "You are a senior software architect with 15 years of experience in distributed systems, microservices, and cloud architecture. When reviewing code, you focus on scalability, maintainability, security best practices, and performance optimization. You provide detailed technical feedback with specific examples and always suggest concrete improvements...",
          "cache_control": {
            "type": "ephemeral"
          }
        }
      ]
    },
    {
      "role": "user",
      "content": "Review this API design for our new user service"
    }
  ]
}
```

### Python Implementation

*Copy, paste, profit*

```python theme={"system"}
import requests

def create_cached_request(user_message):
    url = "https://api.anyapi.ai/v1/chat/completions"
    headers = {
        "Authorization": f"Bearer {API_KEY}",
        "Content-Type": "application/json"
    }

    # Your expensive system prompt (cache this baby!)
    system_prompt = """
    You are an expert financial advisor with CFA certification and 20 years
    of experience in portfolio management, risk assessment, and investment
    strategy. You provide detailed analysis based on current market conditions,
    regulatory requirements, and best practices in wealth management...
    """ # Imagine this is 3000+ tokens

    payload = {
        "model": "anthropic/claude-sonnet-4",
        "messages": [
            {
                "role": "system",
                "content": [
                    {
                        "type": "text",
                        "text": system_prompt,
                        "cache_control": {"type": "ephemeral"}
                    }
                ]
            },
            {
                "role": "user",
                "content": user_message
            }
        ]
    }

    return requests.post(url, headers=headers, json=payload)

# First call: Pay cache write cost
response1 = create_cached_request("Analyze my portfolio allocation")

# Subsequent calls: Pay only 10% for cached system prompt!
response2 = create_cached_request("What's your take on crypto exposure?")
response3 = create_cached_request("Should I rebalance before year-end?")
```

## 📊 Provider Deep Dive

*Know your options, optimize your spend*

### 🟢 **OpenAI: Automatic & Generous**

* **Write cost:** FREE (automatic, no setup needed)
* **Read cost:** 50% off for GPT-4o, 90% off for GPT-5 / GPT-5 Mini / GPT-5.4
* **Minimum:** 1,024 tokens to qualify
* **Expiration:** Typically 5–10 minutes of inactivity, always cleared within 1 hour
* **How it works:** Fully automatic — caches the longest matching prefix starting at 1,024 tokens

### 🔵 **Anthropic Claude: The Precision Player**

* **Write cost:** 25% premium for 5-minute TTL, 100% premium for 1-hour TTL
* **Read cost:** 90% discount (0.1x base input price)
* **Limitation:** Max 4 cache breakpoints per request
* **Expiration:** 5 minutes (default) or 1 hour (extended)
* **Minimum tokens:** 1,024–4,096 depending on model
* **Best for:** High-frequency workflows with explicit cache control

### 🟡 **Grok (xAI): Automatic & Simple**

* **Write cost:** FREE (automatic)
* **Read cost:** 90% off original
* **How it works:** Automatic — all requests benefit from caching with no configuration

### 🟣 **Google Gemini: Dual Approach**

* **Implicit caching:** Automatic, enabled by default for most Gemini models
* **Explicit caching:** Manual, with configurable TTL (defaults to 1 hour) + storage costs
* **Cost:** 90% off cached tokens for Gemini 2.5+ models, 75% off for Gemini 2.0
* **Minimum tokens:** 1,024–4,096 depending on model

### 🔴 **DeepSeek: Automatic Savings**

* **Write cost:** FREE (automatic KV cache on disk)
* **Read cost:** 90% off
* **How it works:** Automatic — repeated prefixes are cached and billed at the lower rate

## 🎯 Pro Optimization Strategies

### The Smart Workflow

```python theme={"system"}
# Structure your prompts for maximum cache hits
base_system = {
    "type": "text",
    "text": "Your massive, reusable system prompt...",
    "cache_control": {"type": "ephemeral"}
}

# Reuse the exact same cached content across requests
def make_request(user_input):
    return {
        "model": "anthropic/claude-sonnet-4",
        "messages": [
            {"role": "system", "content": [base_system]},
            {"role": "user", "content": user_input}
        ]
    }
```

### Cache Hit Maximization

**✅ Do this:**

* Keep cached content **identical** byte-for-byte
* Bundle reusable context into cache-friendly chunks
* Monitor cache hit rates in your analytics

**❌ Avoid this:**

* Modifying cached content (instant cache miss)
* Caching tiny prompts (not worth the complexity)
* Ignoring expiration times (hello, surprise costs)

### Cost Monitoring Magic

```python theme={"system"}
# Track your cache performance
def analyze_cache_savings(requests_made, avg_system_tokens, hit_rate):
    original_cost = requests_made * avg_system_tokens * INPUT_PRICE

    # Anthropic example: 1.25x write + 0.1x reads
    cache_cost = (avg_system_tokens * INPUT_PRICE * 1.25) + \
                 (requests_made * hit_rate * avg_system_tokens * INPUT_PRICE * 0.1)

    savings = original_cost - cache_cost
    print(f"You saved: ${savings:.2f} ({savings/original_cost*100:.1f}%)")
```

## ⚠️ Cache Reality Check

**Remember:**

* 🎯 **Exact matching required** → One character different = cache miss
* ⏰ **Expiration is real** → Plan for cache warmup in workflows
* 📏 **Token minimums apply** → Don't cache tiny prompts
* 🔍 **Monitor hit rates** → Low hit rates = wasted write costs

**When caching backfires:**

* Constantly changing system prompts
* Infrequent API usage
* Very short conversations
* Prompts under provider minimums

## 💡 Quick Wins

1. **Audit your system prompts** → Find the chunky, reusable ones
2. **Implement caching** → Start with your highest-traffic endpoints
3. **Monitor performance** → Track hit rates and actual savings
4. **Optimize expiration** → Align cache duration with usage patterns

***

*Ready to cut your prompt costs? Start caching the smart way with AnyAPI.*
