Inference requests for large models often contain overlapping input, such as in a multi-turn conversation or a series of questions about the same book. Context Cache reduces redundant computation by caching the common prefix of these requests. This improves response speed and lowers usage costs without affecting response quality.
- Explicit cache: A mode that you enable manually. You create a cache for specific content to ensure a deterministic hit within its 5-minute validity period. Tokens used to create the cache are typically billed at 125% of the standard input token price, while subsequent cache hits are typically billed at only 10% of that price. For specific pricing, see Billing.
- Implicit cache: This automatic mode requires no extra configuration and cannot be disabled, ideal for scenarios that prioritize convenience. The system automatically identifies and caches the common prefix of requests, but the hit probability is not guaranteed. The portion of the input served from the cache is typically billed at 20% of the standard input token price. For specific pricing, see Billing.
Item | Explicit cache | Implicit cache |
|---|---|---|
Impact on response quality | No | No |
Billing for cache creation tokens | Typically 125% of the standard input token price | 100% of the standard input token price |
Billing for cached input tokens | Typically 10% of the standard input token price (see Billing) | Typically 20% of the standard input token price (see Billing) |
Minimum tokens for caching | 1024 | 1024 |
Cache validity period | 5 minutes (resets on hit) | Indeterminate. The system periodically clears old, unused cache data. |
Explicit cache
Unlike an implicit cache, an explicit cache requires explicit creation and incurs overhead, but delivers a higher cache hit ratio and lower access latency.
How it works
Add a"cache_control": {"type": "ephemeral"} marker to the messages array. The system then searches backward from eachcache_control marker and examines up to 20 precedingcontent blocks to find a cache hit.
A single request supports up to four cache markers.
-
Cache miss
If a cache miss occurs, the system creates a new cache block from the content between the start of the messages array and the
cache_controlmarker. The new cache block has a validity period of 5 minutes.The system creates the cache after the model generates a response. Wait for the creation request to complete before trying to hit that cache.
A cache block contains at least 1,024 tokens.
- Cache hit If a cache hit occurs, the system selects the longest matching prefix and resets the validity period of the corresponding cache block to 5 minutes.
- Send the first request: Send a system message containing text A (more than 1,024 tokens), and add a cache marker:
- If there are 20 or fewer "Other messages," the request hits cache block A, resetting its validity period to 5 minutes. The system also creates a new cache block based on A, the other messages, and B.
- If there are more than 20 "Other messages," the request misses cache block A. The system still creates a new cache block based on the full context (A, the other messages, and B).
Supported models
- Singapore
- China (Beijing)
- Germany (Frankfurt)
- Hong Kong (China)
- Japan (Tokyo)
- US (Virginia)
The following models are available in the International deployment scope.Qwen Max: qwen3.8-max, qwen3.7-max, qwen3.7-max-2026-05-20, qwen3.7-max-2026-06-08, qwen3.6-max-preview, qwen3-maxQwen Open-source: qwen3.8-2.4t-a95b, qwen3.8-27bQwen Plus: qwen3.7-plus, qwen3.7-plus-2026-05-26, qwen3.6-plus, qwen3.5-plus, qwen3.5-plus-2026-04-20, qwen-plusQwen Flash: qwen3.8-flash, qwen3.7-flash, qwen3.7-flash-2026-07-15, qwen3.6-flash, qwen3.5-flash, qwen-flashQwen Coder: qwen3-coder-plus, qwen3-coder-flashQwen VL: qwen3-vl-plus, qwen3-vl-flashDeepSeek: deepseek-v3.2
Quick start
The following examples demonstrate the cache block creation and cache hit mechanisms for OpenAI compatible, DashScope, and Anthropic compatible protocols.
- OpenAI compatible
- DashScope
- Anthropic compatible
cache_control marker enables explicit cache for the mock code repository content. For subsequent requests that query this content, the system reuses the cache block, eliminating recomputation. This makes requests that hit the cache faster and cheaper than the initial cache-creation request.
Fine-grained control with multiple cache markers
In complex scenarios, a prompt often consists of multiple parts with different reuse frequencies. You can use multiple cache markers to achieve fine-grained control.
For example, the prompt for an intelligent customer service agent typically includes:
- System persona: Highly stable and rarely changes.
- External knowledge: This is obtained from the knowledge base or through tool queries and may not change during a single conversation.
- Conversation history: Grows dynamically.
- Current question: Different for each request.
Billing
Explicit cache only affects how input tokens are billed. The rules are as follows:
-
Cache creation: Content used to create a new cache is billed at 125% of the standard input token price. If the content for a new cache includes an existing cache as a prefix, only the incremental portion is billed for cache creation (i.e., the number of new cache tokens minus the number of existing cache tokens).
For example, if you have an existing 1,200-token cache (Cache A) and you use a new request to cache 1,500 tokens of content (Content AB), the first 1,200 tokens are billed as a cache hit at 10% of the standard price. The new 300 tokens are billed for cache creation at 125% of the standard price.
The
cache_creation_input_tokensparameter specifies the number of tokens used for cache creation. -
Cache hit: Billed at 10% of the standard input token price.
The
cached_tokensparameter specifies the number of cached tokens. - Other tokens: Tokens that are neither a cache hit nor used for cache creation are billed at the standard input token price.
- Exception: The explicit cache hit price for qwen3.8-max, qwen3.8-flash, and qwen3.8-2.4t-a95b is not 10% of the standard input token price. For specific pricing, see the Model Studio console. (The cache creation price remains 125% of the standard price.)
Cacheable content
Only the following message types in the messages array support adding cache markers:
-
System message
For function calling, if a request includes the
toolsparameter, the tool definition is included in the system message for cache calculation. Tool definitions cannot be cached independently. Cache markers added to tool definitions are ignored, as they can only be added to the content of a message. -
User message
When creating a cache with the
qwen3-vl-plusmodel, you can place thecache_controlmarker after multimodal content or text. Its position does not affect how the entire user message is cached. - Assistant message
- Tool message (the result of tool execution)
content field to an array and add the cache_control field:
messages array.
Cache limitations
- The minimum cacheable prompt length is 1,024 tokens.
-
The cache uses a backward prefix matching strategy. A cache miss occurs if the matching content and the message with the
cache_controlmarker are separated by more than 20 content blocks. -
The
typecan only be set toephemeral, which creates a cache with a 5-minute validity period. -
A single request supports up to four cache markers.
If more than four cache markers are provided, only the last four take effect.
Function Calling cache optimization
A tool definition is serialized into a JSON string for caching. To prevent cache invalidation, this definition must be identical across all requests. Note the following:
- Consistent tool order: The order of tools in the
toolsarray must be consistent across all requests. - Consistent field order: The order of JSON fields within the same tool must be consistent across all requests.
- Consistent field structure: Do not omit or add fields, even if they are empty or optional.
Optimizing message structure for parallel tool calls
When you use parallel tool calls, the model returns multiple tool_calls in a single response. If you send each tool result as a separate tool message, the number of content blocks in the messages array grows quickly. When more than 20 content blocks separate the cache_control marker from earlier content, the backward lookback window cannot reach those earlier blocks, causing a cache miss.
To resolve this, merge consecutive same-role tool messages into a single tool message with multiple content blocks before sending the next request. This reduces the total content block count and keeps the content you want to cache within the 20-block lookback window.
Before optimization (separate tool messages — lower cache hit rate):
cache_control markers at stable positions in the messages array (for example, on the system message or on other infrequently changing content). A single request supports up to four cache markers.
Usage examples
Querying a long text
Querying a long text
To ensure model performance, the system appends a few internal tokens. These tokens are billed at the standard input price. For more information, see the FAQ.
Caching tools for function calling
Caching tools for function calling
tools parameter is cached as part of the system message. Ensure that the tool definition is identical for every request (including the tool order, field order, and field structure), and add a cache_control flag to the last content in messages.The following shows the complete flow: the first request creates the cache, and the second request hits the cache.Continuous multi-turn conversation
Continuous multi-turn conversation
Implicit cache
Supported models
- China (Beijing)
- Singapore
- US (Virginia)
- Germany (Frankfurt)
- China (Hong Kong)
- Japan (Tokyo)
-
Text generation models
- Qwen Max: qwen3.8-max, qwen3.7-max, qwen3.7-max-2026-05-20, qwen3.7-max-2026-06-08, qwen3-max, qwen3-max-preview, qwen-max
- Qwen Plus: qwen3.7-plus, qwen3.7-plus-2026-05-26, qwen-plus
- Qwen Flash: qwen3.8-flash, qwen3.7-flash, qwen3.7-flash-2026-07-15, qwen-flash
- Qwen Turbo: qwen-turbo
- Qwen Coder: qwen3-coder-plus, qwen3-coder-flash
- Qwen Character: qwen-plus-character, qwen-flash-character
- Qwen Open-source: qwen3.8-2.4t-a95b, qwen3.8-27b
- DeepSeek: deepseek-v4-pro, deepseek-v4-flash, deepseek-v3.2, deepseek-v3.1, deepseek-v3, deepseek-r1
- Kimi: kimi-k3, kimi-k2.7-code, kimi-k2.6, kimi-k2.5, kimi-k2-thinking, Moonshot-Kimi-K2-Instruct
- GLM: glm-5.2, glm-5.2-fast-preview, glm-5.1, glm-5, glm-4.7, glm-4.6
- MiniMax: MiniMax-M2.5
-
Visual understanding models
- Qwen VL: qwen3-vl-plus, qwen3-vl-flash, qwen-vl-max, qwen-vl-plus
How it works
The implicit cache feature is automatically enabled when a request is sent to a supported model. The system works as follows:
-
Search: After receiving a request, the system uses prefix matching to check the cache for a common prefix of the content in the request's
messagesarray. -
Decision:
- If a cache hit occurs, the system uses the cached result for the rest of the inference.
- If a cache miss occurs, the system processes the request normally and stores the prefix of the prompt in the cache for future requests.
The system periodically clears cached data that has not been used for a long time. The Context Cache hit probability is not 100%. A cache miss may occur even if the request context is identical. The system determines the specific hit probability.
Increase the cache hit probability
An implicit cache hit occurs when the prefixes of different requests have duplicate content. To increase the hit probability, place duplicate content at the beginning of a prompt and unique content at the end.
- Text model: For example, assume the system has cached "ABCD". A request for "ABE" may hit the "AB" part, but a request for "BCD" will not.
-
Visual understanding model:
- To ask multiple questions about the same image or video, place the image or video before the text.
- To ask the same question about different images or videos, place the text before the image or video.
Billing
No additional fees are charged for enabling the implicit cache mode.
When a request hits the cache, the input tokens from the cache hit are billed as cached_token. The discount rate for these tokens varies by model. Input tokens that do not hit the cache are billed as standard input_token. Output tokens are billed at the original price.
-
For models other than deepseek-v4-pro, qwen3.8-max, qwen3.8-flash, and qwen3.8-2.4t-a95b: The unit price of
cached_tokenis 20% of theinput_tokenunit price. -
deepseek-v4-pro: The unit price of
cached_tokenis not 20% of theinput_tokenunit price. For specific pricing, see the Model Studio console. -
qwen3.8-max, qwen3.8-flash, and qwen3.8-2.4t-a95b: The unit price of
cached_tokenis not 20% of theinput_tokenunit price. For specific pricing, see the Model Studio console. - GLM (deployed on Alibaba Cloud Model Studio): 25% for glm-5.2 and glm-5.2-fast-preview, and 20% for all other GLM series models.
- Non-cache-hit tokens (5,000): Billed at 100% of the unit price.
- Cache-hit tokens (5,000): Billed at 20% of the unit price.

cached_tokens attribute in the response.
Calls made using the OpenAI-compatible - Batch (file input) method are not eligible for cache discounts.
Cache hit examples
- Text generation models
- Visual understanding models
- OpenAI-compatible
- DashScope
- Anthropic-compatible
usage.prompt_tokens_details.cached_tokens field. This value is part of usage.prompt_tokens.Use cases
If your requests share a common prefix, the context cache can significantly improve inference speed, lower inference cost, and reduce first-packet latency. This feature is particularly useful in the following use cases:
- Long-text question answering Use this pattern when you send multiple requests about the same long text, such as a novel, textbook, or legal document. First request messages
video before text increases the probability of a cache hit. If you ask the same question about different videos, placing text before video increases the probability of a cache hit. The following example shows two requests for the same video:
FAQ
Q: How long is the context cache retained (validity period)?
A: The validity period of the context cache depends on the cache type:
- Explicit cache: The validity period is 5 minutes, and each cache hit resets it to another 5 minutes. If the cache block is not hit within 5 minutes, the system automatically clears it.
- Implicit cache: Managed automatically by the system with no fixed validity period. The system periodically clears cache data that has not been used for a long time.
Q: How do I disable implicit cache?
A: You cannot disable it. The implicit cache is enabled for all applicable model requests because it does not affect response quality. When a cache hit occurs, it reduces costs and improves response speed.
Q: Why did my explicit cache miss?
A: A cache miss can occur for the following reasons:
- The system clears the cache block if it is not hit within its 5-minute validity period.
- If the interval between the last
contentand an existing cache block is greater than 20contentblocks, a cache hit will not occur. We recommend that you create a new cache block.
Q: Does a cache hit reset its validity?
A: Yes. Each hit resets the cache block's validity period to 5 minutes.
Q: Is explicit cache shared between accounts?
A: No. Both implicit cache and explicit cache data is isolated at the account level.
Q:Is explicit cache shared across models?
A: No. Cache data is isolated between models.
Q: Why doesn't theinput_tokensinusageequal the sum ofcache_creation_input_tokensandcached_tokens?
A: To ensure model output quality, the backend service appends a small number of tokens (typically 10 or fewer) to your prompt. These tokens are placed after the cache_control marker, so they are not counted for cache creation or reading, but are included in the total input_tokens.