Repository Public
1<!-- source: https://docs.x.ai/developers/advanced-api-usage/prompt-caching/how-it-works (from https://docs.x.ai/llms-full.txt, retrieved 2026-09-19T05:28:55Z) -->2#### Prompt Caching34# How It Works56The cache works from the **start of your messages array**. When a request arrives, the system checks how many messages at the beginning match a previous request exactly — that matching portion is the "prefix" and gets served from cache:781. **First request** — The full prompt is processed and cached92. **Subsequent requests** — If the prompt prefix matches, the cached portion is reused (a cache *hit*)103. **Billing** — Cached tokens are billed at a reduced rate1112> [!WARNING]13>14> Prompt caching is not 100% guaranteed. Cache entries can be evicted due to memory pressure, and requests may be routed to different servers. Use `x-grok-conv-id` to maximize cache hit rates.1516## Example1718**Request 1:**1920```text21[system] "You are a helpful assistant."22[user] "What is the capital of France?"23[assistant] "The capital of France is Paris."24```2526**Request 2:**2728```text29[system] "You are a helpful assistant." ← cached30[user] "What is the capital of France?" ← cached31[assistant] "The capital of France is Paris." ← cached32[user] "What about Germany?" ← new33```3435The first 3 messages match Request 1 exactly, so they're served from cache. Only the new message is computed.3637## Next3839* [Maximizing Cache Hits](/developers/advanced-api-usage/prompt-caching/maximizing-cache-hits)40* [What Breaks Caching](/developers/advanced-api-usage/prompt-caching/multi-turn)41* [Usage & Pricing](/developers/advanced-api-usage/prompt-caching/usage-and-pricing)42* [Best Practices & FAQ](/developers/advanced-api-usage/prompt-caching/best-practices)43