OpenAI Automatic Cached Input: Maximizing the 'Magical' Automatic Cache
OpenAI cached input gives 50-87% discount (model-dependent) but auto-triggers — limited control. This lesson covers trigger conditions, maximization strategies, cache hit detection, and Anthropic comparison.
Şükrü Yusuf KAYA
16 min read
Intermediate🪄 "Sihir gibi" otomatik cache
OpenAI'ın yaklaşımı: "Cache kontrolünü sana vermiyorum, ben otomatik halledeceğim." 2024 sonu eklendi. Default olarak çalışıyor. Ama maksimize etmek için bilmen gerekenler var.
OpenAI Cached Input Fiyatları (Mayıs 2026)#
| Model | Standart input | Cached input | İndirim |
|---|---|---|---|
| GPT-5 | $10/M | $1.25/M | -87% ⭐ |
| GPT-5-mini | $0.40/M | $0.10/M | -75% |
| GPT-5-nano | $0.10/M | $0.025/M | -75% |
| GPT-4.1 (legacy) | $2.50/M | $0.625/M | -75% |
| o3 | $2.00/M | $0.50/M | -75% |
| o3-mini | $1.10/M | $0.275/M | -75% |
GPT-5 dramatik indirim#
GPT-5'in cached input fiyatı standart'tan 87% düşük — diğer modellerden agresif. Bu, OpenAI'ın stratejik kararı: cache kullanan ekiplere büyük ödül.
Sonnet 4.6 cache read'i 0.10× (yani %90 indirim) → benzer agresiflik.
Cache Hit Tetikleme Koşulları#
OpenAI cache'i aktif olması için:
Koşul 1 — Min 1024 token prefix#
Prompt'un başında en az 1024 token sabit kalmalı. Sistem prompt + few-shot examples + statik veri kombinasyonu genelde bunu sağlar.
Koşul 2 — Birebir aynı prefix#
Bir önceki istekle byte-for-byte aynı başlangıç. Tek karakter farkı = miss.
Koşul 3 — Tools list aynı sırada#
Tools sırası değişirse cache invalid. Alfabetik sıralı tut.
Koşul 4 — 5-10 dakikalık TTL#
Belirsiz dokümante edildi. Genelde 5dk içinde tekrarlandığında hit.
Koşul 5 — Aynı model versiyonu#
gpt-5gpt-5-2026-02-01Cache Hit Tespit Etme#
OpenAI response'unda field'ından okunur:
cached_tokensresponse = openai.chat.completions.create( model="gpt-5-2026-02-01", messages=[ {"role": "system", "content": LARGE_SYSTEM_PROMPT}, # 2K+ token {"role": "user", "content": user_query}, ], ) usage = response.usage total_input = usage.prompt_tokens cached_input = usage.prompt_tokens_details.cached_tokens or 0 cache_hit_ratio = cached_input / total_input if total_input > 0 else 0 print(f"Cache hit: {cached_input}/{total_input} = {cache_hit_ratio:.1%}")
Tipik observation#
İlk istek: cached_tokens = 0 (miss, normal — cache write)
İkinci istek (5dk içinde): cached_tokens = 1500 (hit)
3.+ istek: cached_tokens = 1500 (hit)
Cache Hit'i Maksimize Stratejileri#
Strateji 1 — Deterministic Prompt Build#
Bu cümleler aynı görünür ama farklı bytes:
"You are a customer support assistant.\n" "You are a customer support assistant. " ← trailing space "You are a customer support assistant.\r\n" ← CRLF
Cache miss! Her zaman tek bir kurala uy.
Strateji 2 — System Prompt'u Stabil Tut#
Her deploy'da system prompt'u değiştirmek = her seferinde cache miss. Stable Versioning:
SYSTEM_PROMPT_V12 = """...""" # bu hash deterministic # Yeni version eklerken eskini bir hafta paralel tut, gradual rollout
Strateji 3 — Tools Listesini Alfabetik Sırala#
tools = sorted(get_user_tools(user), key=lambda t: t["function"]["name"]) response = openai.chat.completions.create( model="gpt-5", messages=[...], tools=tools, # ← her zaman aynı sırada )
Strateji 4 — Statik Bilgiyi Başa, Dinamik Sona#
# YANLIŞ — kullanıcı sorusu ortada messages = [ {"role": "system", "content": INTRO}, {"role": "user", "content": user_query}, # ← dinamik, breaks cache {"role": "assistant", "content": EXAMPLE_RESPONSE}, {"role": "user", "content": ANOTHER_STATIC_PART}, ] # DOĞRU — dinamik kısım sona messages = [ {"role": "system", "content": INTRO + STATIC_CONTEXT + EXAMPLES}, # 4K cacheable {"role": "user", "content": user_query}, # dinamik, sona ]
Anthropic vs OpenAI Cache — Detaylı Karşılaştırma#
| Özellik | Anthropic | OpenAI |
|---|---|---|
| Mode | Manuel breakpoint | Otomatik |
| Read indirim | %90 (0.10×) | %50-87 (model'e göre) |
| Write zam | %25 | None |
| Kontrolün var | ✅ 4 breakpoint | ❌ |
| TTL | 5dk / 1h | ~5-10dk (belirsiz) |
| Min cache size | 1024 / 2048 | 1024 |
| Cache invalidation | Explicit (yeni prompt) | Otomatik (5dk pass) |
| Multi-layer cache | ✅ (4 breakpoint) | ❌ |
| Debug | Kolay (cache_creation, cache_read alanları) | Sınırlı (cached_tokens only) |
Hangi durumda hangisi daha iyi?#
| Senaryo | Tercih |
|---|---|
| Sık değişen tenant config | Anthropic (multi-layer) |
| Statik sistem prompt, basit | OpenAI (zero-effort) |
| Production'da KVKK | Anthropic Bedrock + cache (preview) |
| Cost-optimization en yüksek öncelik | Anthropic %90 > OpenAI %50-87 |
| Latency en kritik | İkisi de aynı |
Pratik Örnek — Aynı Pipeline, İki Sağlayıcı#
def run_with_caching(provider: str, query: str): if provider == "anthropic": return anthropic.Anthropic().messages.create( model="claude-sonnet-4-6", system=[ {"type": "text", "text": SYSTEM_PROMPT, "cache_control": {"type": "ephemeral"}}, {"type": "text", "text": FAQ + CATALOG, "cache_control": {"type": "ephemeral"}}, ], messages=[{"role": "user", "content": query}], max_tokens=500, ) elif provider == "openai": return openai.chat.completions.create( model="gpt-5-2026-02-01", # pinned messages=[ {"role": "system", "content": SYSTEM_PROMPT + "\n\n" + FAQ + CATALOG}, {"role": "user", "content": query}, ], max_tokens=500, )
100 istekten sonra metric'ler:
| Metric | Anthropic | OpenAI |
|---|---|---|
| Avg cache hit % | 87% | 84% |
| Avg cost/req | $0.0024 | $0.0028 |
| Yıllık (100K/ay × 12) | $2.880 | $3.360 |
Anthropic biraz daha ucuz (cache %90 vs %87 indirim), ama OpenAI hiç ek kod olmadan benzer sonuç verdi.
▶️ Sıradaki ders
7.3 — Gemini Context Caching. Anthropic ve OpenAI'dan farklı bir model: cache storage fee + read fee kombosu. Avantaj/dezavantaj analizi ve hangi senaryoda Gemini cache daha ekonomik.
Frequently Asked Questions
It works, but cache invalidates when OpenAI upgrades the model. Pin the snapshot in production. Standard for all cost optimization strategies.
Yorumlar & Soru-Cevap
(0)Yorum yazmak için giriş yap.
Yorumlar yükleniyor...
Related Content
Module 0: Why Cost, Why Now?
The AI Cost Explosion: Why Token Prices Fell 96% from 2022 to 2026 — Yet Bills Grew 40×
Start LearningModule 0: Why Cost, Why Now?
Unit Economics Vocabulary: COGS, Gross Margin, $/User, Contribution Margin — 9 Financial Concepts Every AI Engineer Must Know
Start LearningModule 0: Why Cost, Why Now?