Zubnet AIApprendreWiki › Prompt Caching
Using AI

Prompt Caching

Context Caching, Prefix Caching
Une technique qui sauvegarde et réutilise la version traitée d'un préfixe de prompt à travers plusieurs appels API, évitant du calcul redondant. Si tu envoies le même system prompt et contexte de document avec chaque requête (ce qui est commun), le prompt caching le traite une fois et réutilise le calcul en cache pour les requêtes suivantes. Ça réduit à la fois la latence et le coût.

Pourquoi c'est important

La plupart des applications IA envoient le même system prompt, les mêmes exemples few-shot, ou les mêmes documents de référence avec chaque requête. Sans caching, le fournisseur traite ce préfixe identique chaque fois. Le prompt caching peut couper les coûts de tokens d'entrée de 50–90 % et réduire significativement le time-to-first-token. Pour les applications à haut volume, ça se traduit en milliers de dollars sauvés par mois.

Deep Dive

The technical mechanism: during the "prefill" phase of LLM inference, the model processes all input tokens and computes their KV cache entries. Prompt caching stores this KV cache so that subsequent requests with the same prefix skip the prefill for the cached portion. Only new tokens (the user's actual message) need processing. Anthropic, OpenAI, and Google all offer some form of prompt caching.

How to Use It

Most implementations work by detecting matching prefixes automatically or by letting you mark cache breakpoints. The key constraint: only exact prefix matches count. If your system prompt changes by even one token, the cache misses. This means structuring your prompts with the stable parts first (system prompt, documents) and variable parts last (user message) is important for cache hit rates.

When It Matters Most

Prompt caching delivers the biggest savings when: (1) you have a long, stable prefix (large system prompts, RAG context), (2) you send many requests with that same prefix (chatbots, agents), and (3) input tokens are a significant portion of your costs. For applications with short, unique prompts, caching provides little benefit. For applications that stuff the context window with documents, it's transformative.

Concepts liés

← Tous les termes
← Prompt Prompt Engineering →