# Prompt Caching & Cost Optimization — API Store Hub

> VI: Chiến lược kết hợp `cache_control: ephemeral`, định tuyến phân tầng Token Lane vs Request Lane và giới hạn max_tokens giúp cắt giảm 50–80% hóa đơn ST hàng tháng.
> EN: Combine ephemeral prompt caching, token-vs-request lane selection, and tiered routing on API Store Hub to cut monthly ST spend by 50–80%.

- Canonical VI: https://apistorehub.com/vi/tich-hop/prompt-caching-batch-cost
- Canonical EN: https://apistorehub.com/en/tich-hop/prompt-caching-batch-cost
- Language: Python
- Runtime: Python 3.10+ / TypeScript, OpenAI & Anthropic SDK
- Protocol: Anthropic (/v1/messages) & OpenAI (/v1/chat/completions)
- Install: `pip install -U anthropic openai`
- Auth Env Var: `API_KEY` (prefix `sk-ash-`)

## Step-by-Step Setup
1. Keep static system instructions and reference docs at the very beginning of prompts with no dynamic timestamps.
2. Mark reusable Anthropic prompt blocks over 1,024 tokens with `cache_control: {"type": "ephemeral"}`.
3. Compare Token Lane vs Fixed Request Lane for long-context workloads.
4. Split traffic with a two-tier router: 80% simple queries to `deepseek-v4-flash`, 20% deep tasks to `claude-sonnet-5-5`.

## Production Code Sample
```
import os
from openai import OpenAI

client = OpenAI(base_url="https://apistorehub.com/v1", api_key=os.environ["API_KEY"])

def route_task(prompt: str, complex_reasoning: bool) -> str:
    model = "claude-sonnet-5-5" if complex_reasoning else "deepseek-v4-flash"
    resp = client.chat.completions.create(
        model=model,
        max_tokens=1024 if complex_reasoning else 320,
        messages=[
            {"role": "system", "content": "Keep system prefix static for high cache hit rate."},
            {"role": "user", "content": prompt},
        ],
    )
    return resp.choices[0].message.content or ""
```

## Quick cURL Check
```bash
curl https://apistorehub.com/v1/messages \
  -H "x-api-key: sk-ash-your_key" \
  -H "anthropic-version: 2023-06-01" \
  -H "Content-Type: application/json" \
  -d '{"model":"claude-sonnet-5-5","max_tokens":256,"system":[{"type":"text","text":"Static policy prefix...","cache_control":{"type":"ephemeral"}}],"messages":[{"role":"user","content":"Check rule 1"}]}'
```

## Recommended Models (Live ST Pricing)
| Model | Live Price | Context | Datasheet |
|---|---|---|---|
| `claude-sonnet-5-5` | 7,700 ST / 1M tokens | 1M | https://apistorehub.com/llms/models/claude-sonnet-5-5.md |
| `deepseek-v4-flash` | 1,500 ST / 1M tokens | 1M | https://apistorehub.com/llms/models/deepseek-v4-flash.md |
| `deepseek-v4-pro` | 3,600 ST / 1M tokens | 1M | https://apistorehub.com/llms/models/deepseek-v4-pro.md |

## FAQ
- **When is Request Lane cheaper than Token Lane on API Store Hub?**: When each call carries large context (30K–150K tokens) where flat per-request billing beats per-million-token pricing.
- **What is the minimum prompt size for Anthropic prompt caching?**: Typically 1,024+ tokens (2,048+ on Haiku) reused within the active ephemeral TTL window.
- **Can I monitor spend per API key and model?**: Yes. The Dashboard Usage & FinOps analytics break down every call, token count, and ST charge in real time.
