2 2
3# Prompt Caching3# Prompt Caching
4 4
5We recommend the [Responses API](/developers/model-capabilities/text/generate-text) for Grok models, and the examples in this section use it. It carries the model's reasoning from one request to the next, either in the `reasoning` items you send back or through `previous_response_id`; Chat Completions has no field for encrypted reasoning. Prompt caching itself works the same way on both APIs.
6
5When consecutive requests share the same starting messages, the xAI API automatically caches them. On the next request, messages at the beginning that match exactly are served from cache:7When consecutive requests share the same starting messages, the xAI API automatically caches them. On the next request, messages at the beginning that match exactly are served from cache:
6 8
7* **Faster time-to-first-token** — the model skips re-computing cached messages9* **Faster time-to-first-token** — the model skips re-computing cached messages
9 11
10> [!NOTE]12> [!NOTE]
11>13>
12> The xAI API performs prompt caching **automatically**. However, we recommend setting the `x-grok-conv-id` HTTP header to maximize your cache hit rate.14> The xAI API performs prompt caching **automatically**. However, we recommend setting `prompt_cache_key` (or the `x-grok-conv-id` HTTP header) to maximize your cache hit rate.
13 15
14## In this section16## In this section
15 17
16* [How It Works](/developers/advanced-api-usage/prompt-caching/how-it-works) — Understand how caching works from the start of your messages array18* [How It Works](/developers/advanced-api-usage/prompt-caching/how-it-works) — Understand how caching works from the start of your messages array
17* [Maximizing Cache Hits](/developers/advanced-api-usage/prompt-caching/maximizing-cache-hits) — Set up `x-grok-conv-id` and `prompt_cache_key` for optimal caching19* [Maximizing Cache Hits](/developers/advanced-api-usage/prompt-caching/maximizing-cache-hits) — Set up `prompt_cache_key` or `x-grok-conv-id` for optimal caching
18* [What Breaks Caching](/developers/advanced-api-usage/prompt-caching/multi-turn) — Common mistakes that cause cache misses20* [What Breaks Caching](/developers/advanced-api-usage/prompt-caching/multi-turn) — Common mistakes that cause cache misses
19* [Usage & Pricing](/developers/advanced-api-usage/prompt-caching/usage-and-pricing) — Read cached token counts and understand billing21* [Usage & Pricing](/developers/advanced-api-usage/prompt-caching/usage-and-pricing) — Read cached token counts and understand billing
20* [Best Practices & FAQ](/developers/advanced-api-usage/prompt-caching/best-practices) — Tips, supported models, and common questions22* [Best Practices & FAQ](/developers/advanced-api-usage/prompt-caching/best-practices) — Tips, supported models, and common questions