Prompt caching - Anthropic
Prompt caching is a powerful feature that optimizes your API usage by allowing resuming from specific prefixes in your prompts. This approach significantly reduces processing time and costs for repetitive tasks or prompts with consistent elements. Here’s an example of how to implement prompt caching with the Messages API using a cache_control block: In this example, the entire text of “Pride and Prejudice” is cached using the cache_control parameter. This enables reuse of this large text across multiple API calls without reprocessing it each time. Changing only the user message allows you to ask various questions about the book while utilizing the cached content, leading to faster responses and improved efficiency. When you send a request with prompt caching enabled: This is especially useful for: By default, the cache has a 5-minute lifetime. The cache is refreshed for no additional cost each time the cached content is used. Prompt caching caches the full prefix Prompt caching referen
Cache prompt prefixes with cache_control to cut costs and latency, using automatic caching or explicit breakpoints with 5-minute or 1-hour TTLs. Prompt caching optimizes your API usage by allowing resuming from specific prefixes in your prompts. This significantly reduces processing time and costs for repetitive tasks or prompts with consistent elements. There are two ways to enable prompt caching: Automatic caching: Add a single cache_control field at the top level of your request. The system automatically applies the cache breakpoint to the last cacheable block and moves it forward as…
related reading
- Perplexityperplexity.ai
- Prompting best practicesdocs.anthropic.com
- OpenAI | Research & Deploymentopenai.com
- GitHub - f/prompts.chat: f.k.a. Awesome ChatGPT Prompts. Share, discover, and collect prompts from the community. Free and open source — self-host for your organization with complete privacy.github.com
- The Shape of AI | UX Patterns for Artificial Intelligence Designshapeof.ai
- Rewind.ai - Every AI Tool, Completely Freerewind.ai
- ChatGPTchatgpt.com
- PromptLayer — Prompt Management, Evals & Observabilitypromptlayer.com
- AI Learning Resources & Guides from Anthropic \ Anthropicanthropic.com
- API Overview | OpenAI API Referenceplatform.openai.com
- Cookbookcookbook.openai.com
- GitHub - brexhq/prompt-engineering: Tips and tricks for working with Large Language Models like OpenAI's GPT-4.github.com