Zubnet AIAprenderWiki › Prompt Caching
Using AI

Prompt Caching

Context Caching, Prefix Caching
Uma técnica que salva e reutiliza a versão processada de um prefixo de prompt através de múltiplas chamadas API, evitando computação redundante. Se você envia o mesmo system prompt e contexto de documento com cada requisição (o que é comum), prompt caching o processa uma vez e reutiliza a computação em cache para requisições subsequentes. Isso reduz tanto a latência quanto o custo.

Por que importa

A maioria das aplicações IA envia o mesmo system prompt, exemplos few-shot, ou documentos de referência com cada requisição. Sem caching, o provedor processa esse prefixo idêntico toda vez. Prompt caching pode cortar custos de tokens de entrada em 50–90% e reduzir significativamente o time-to-first-token. Para aplicações de alto volume, isso se traduz em milhares de dólares economizados por mês.

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.

Conceitos relacionados

← Todos os termos
← Prompt Prompt Engineering →