Skip to main content

Prompt Cache

Prompt Caching is a powerful performance optimization feature that allows you to reuse specific prefix content in your requests. This approach can significantly reduce processing time and call costs, particularly suitable for scenarios containing large amounts of static content.
  • Cost Reduction: When cache hits occur, cached content typically costs only 10% of the original input cost
  • Speed Enhancement: Reduces processing time for repeated content, accelerating response speed
  • Use Cases: Long system prompts, numerous examples, RAG documents, long conversation history, etc.

Caching Types Overview

Models supported by ShuYou offer two types of prompt caching mechanisms:

Type 1: Implicit Caching

The following model series provide implicit automatic prompt caching functionality, requiring no special parameters in requests—the model automatically detects and caches reusable content.
For each model’s cache read pricing (Cache Read), visit the corresponding model detail page, for example:GPT-5 Model Detail PageGPT-5 Model Detail PageTypically, cache read fees are about 10%-25% of the original input cost, saving up to 90% of input costs.
To maximize cache hit rate, follow these best practices:
  1. Static-to-Dynamic Ordering: Place stable, reusable content (such as system instructions, few-shot examples, document context) at the beginning of the messages array
  2. Variable Content at End: Place variable, request-specific content (such as current user question, dynamic data) at the end of the array
  3. Maintain Prefix Consistency: Ensure cached content remains completely consistent across multiple requests (including spaces and punctuation)

Type 2: Explicit Caching

Anthropic Claude and Qwen series models can explicitly specify caching strategies through specific parameters. This approach provides the finest control but requires you to actively manage caching strategies.

Caching Working Principle

When you send a request with cache_control markers:
  1. The system checks if a reusable cache prefix exists
  2. If a matching cache is found, cached content is used (reducing cost)
  3. If no match is found, the complete prompt is processed and a new cache entry is created
Cached content includes the complete prefix in the request: toolssystemmessages (in this order), up to where cache_control is marked.
You only need to add a cache breakpoint at the end of static content, and the system will automatically check approximately the preceding 20 content blocks for reusable cache boundaries. If the prompt contains more than 20 content blocks, consider adding additional cache_control breakpoints to ensure all content can be cached.

Caching Limitations

Minimum Cache Length

Minimum cacheable token count for different models:
Prompts shorter than the minimum token count will not be cached even if marked with cache_control. Requests will be processed normally but no cache will be created.

Cache Validity Period

  • Default TTL: 5 minutes
  • Extended TTL: 1 hour (requires additional fee)
Cache automatically refreshes with each use at no additional cost.

Cache Breakpoint Count

Each request can define a maximum of 4 cache breakpoints.

Usage Methods

Basic Usage: Caching System Prompts

OpenAI Python SDK
Anthropic Python SDK

Advanced Usage: Caching Tool Definitions

When your application uses many tools, caching tool definitions can significantly reduce costs:
OpenAI PythonSDK
Anthropic Python SDK
By adding a cache_control marker on the last tool definition, the system will automatically cache all tool definitions as a complete prefix.

Advanced Usage: Caching Conversation History

In long conversation scenarios, you can cache the entire conversation history:
OpenAI Python SDK
Anthropic Python SDK
By adding cache_control to the last message of each conversation round, the system will automatically find and use the longest matching prefix from previously cached content. Even if content was previously marked with cache_control, as long as it’s used within 5 minutes, it will automatically hit the cache and refresh the validity period.

Advanced Usage: Multi-Breakpoint Combination

When you have multiple content segments with different update frequencies, you can use multiple cache breakpoints:
OpenAI Python SDK
Anthropic Python SDK
Using multiple cache breakpoints allows content with different update frequencies to be cached independently:
  • Breakpoint 1: Tool definitions (almost never change)
  • Breakpoint 2: System instructions (rarely change)
  • Breakpoint 3: RAG documents (may update daily)
  • Breakpoint 4: Conversation history (changes every round)
When only the conversation history is updated, the cache for the first three breakpoints remains valid, maximizing cost savings.

What Invalidates Cache

The following operations will invalidate part or all of the cache:

Best Practices

Maximizing Cache Hit Rate

  • Maintain Prefix Consistency: Place static content at the beginning of prompts, variable content at the end
  • Use Breakpoints Wisely: Set different cache breakpoints based on content update frequency
  • Avoid Minor Changes: Ensure cached content remains completely consistent across multiple requests
  • Control Cache Time Window: Initiate subsequent requests within 5 minutes to hit cache

Extending Cache Time (1-hour TTL)

If your request intervals may exceed 5 minutes, consider using 1-hour cache:
The write cost for 1-hour cache is 2x the base fee (compared to 1.25x for 5-minute cache), only worthwhile in low-frequency but regular call scenarios.

Avoiding Common Pitfalls

  1. Cached Content Too Short: Ensure cached content meets minimum token requirements
  2. Content Inconsistency: Changes in JSON object key order will invalidate cache (certain languages like Go, Swift)
  3. Mixed Format Usage: Using different formatting approaches for the same content
  4. Ignoring Cache Validity Period: Cache becomes invalid after 5 minutes

FAQ

Do Implicit Caching Models Require Configuration?

No. Implicit caching models like OpenAI, DeepSeek, Grok, Gemini, Qwen automatically manage caching without requiring any special parameters in requests.
These two model series support both implicit and explicit modes. Implicit mode triggers automatically, while explicit mode requires proactive control through API parameters (such as cache_control).

How to View Cache Data?

You can view the Prompt Token details of requests in the ShuYou Logs interface, as shown below: View Cache Data
Note that you need to enable the switch in the Strategy-API Call Logging interface for related data to be logged.Log Switch