Learning resourcesReviewed · xAI

xAI Grok API: Prompt Caching in Multi-Turn Conversations (What Breaks Caching)

This is a short xAI documentation page that teaches developers using the Grok API how to keep multi-turn chats cache-friendly, and its main catch is that it covers only what breaks the cache, not pricing, cache lifetime or limits. It says to only append messages and never edit, remove or reorder earlier ones. Reasoning models must also send back reasoning_content or use previous_response_id. It gives a cache-hit example in Python, curl and JavaScript, three cache-miss examples, and shows how to read cached_tokens from the response usage.

Researched 15 Nov 2026

In short.

This is a short xAI documentation page that teaches developers using the Grok API how to keep multi-turn conversations cache-friendly, and its main catch is that it covers only what breaks the cache, not prices, cache lifetime or size limits. The central rule is to only append new messages at the end of a conversation and never edit, remove or reorder earlier ones. For reasoning models, the page says you must include reasoning_content from previous responses, or use previous_response_id, to keep cache hits. It shows a cache-hit flow in Python (OpenAI SDK), curl and JavaScript (OpenAI SDK), using the x-grok-conv-id header and the model name grok-4.7. It then shows three cache-miss patterns: editing, removing and reordering messages. None of the code was run, and the cache behaviour described here was not observed.

Researched and written by an AI model and checked by a second AI model. How it works

This report covers only the complete documentation page at [https://docs.x.ai/developers/advanced-api-usage/prompt-caching/multi-turn.md], captured as one text file. Linked pages, including Usage and Pricing, Encrypted Reasoning Content and Chaining the Conversation, are excluded and were not read. The page carries no licence or copyright notice. Nothing was executed, so the code samples and the cache-hit and cache-miss behaviour are untested here, and every claim about caching is the page's own statement.

The overview

What this resource is

This is one page of xAI's developer documentation, titled What Breaks Caching, in the Prompt Caching part of the Advanced API Usage section. The address is [https://docs.x.ai/developers/advanced-api-usage/prompt-caching/multi-turn.md]. It is a guide to one feature of the Grok API, not a software package or a model. The page ends with a Next link to a Usage and Pricing page in the same section.

The author is presumably xAI, since it is on docs.x.ai. The page itself shows no author name, date or version.

Overall assessment

The page is narrow but clear. It teaches one rule well: for a cache hit in a multi-turn conversation, the earlier part of the prompt must be unchanged and you only add to the end. It backs the rule with a working-looking hit example in three languages and three short miss examples. A developer who builds chat history by appending messages to a list will find it quick to read and act on.

The catch is scope. The page does not say how long a cache entry lasts, how long a prompt must be before it is cached, what cached tokens cost, or whether the x-grok-conv-id header is required. It points to other pages for pricing and reasoning details, and those were not part of the reviewed material. None of the examples were run, so the claims about hits and misses are the page's own statements, not observed results.

Who it is for

The page is for developers who call the xAI Grok API in a multi-turn setting, such as chat assistants or agent loops that resend the growing message history on every request. Its examples use the OpenAI Python and JavaScript SDKs pointed at the xAI base URL, plus raw curl, so it suits people already familiar with the OpenAI-style chat completions format.

It is especially relevant to people using reasoning models, because the page calls omitting reasoning_content the top cause of cache misses. It is not a beginner's introduction to the Grok API or to prompting. It assumes you already know how to send a messages array and read a response. It also does not suit anyone looking for pricing, quota or architecture details.

What it teaches

The page states the main rule in two ways: any change to earlier messages breaks the cache, and you should only append new messages at the end. A warning box adds that you should never edit, remove or reorder earlier messages.

For reasoning models, the page gives two ways to keep cache hits. One is to send back the encrypted reasoning content by including reasoning_content from the previous response. The other is to use stateful responses with previous_response_id so the conversation continues automatically. Each option links to another xAI documentation page (Encrypted Reasoning Content and Chaining the Conversation), and those linked pages were not reviewed.

The examples show how to check the result: the response field usage.prompt_tokens_details.cached_tokens is printed after each turn in the Python and JavaScript samples. Four labelled scenarios are shown: a cache hit from appending a message, and cache misses from editing, removing and reordering messages.

Using it

What you need

From the examples, you need an xAI API key, the base URL [https://api.x.ai/v1], and a way to make HTTP calls. The Python and JavaScript samples use the OpenAI SDK with a custom base URL, and the curl samples send a bearer token in the Authorization header. The samples use the model name grok-4.7. Whether that model is available to every account is not stated.

The samples also send a header named x-grok-conv-id with a conversation identifier such as conv_abc123. The page does not say whether this header is required for caching, optional, or only a hint. Account tier, rate limits, supported models for caching and any minimum prompt size are not stated on the page.

How it works

The page describes prompt caching as prefix matching: a request gets a cache hit when its prompt prefix is identical to the previous request's, with only new messages added at the end. Changing, removing or reordering any earlier message changes that prefix, so the match fails. The sample assistant text in the curl examples describes the idea as storing key-value pairs from unchanged prompt prefixes so they can be reused, which makes responses faster and cheaper.

For reasoning models, the earlier turn includes reasoning output. If you leave it out of the next request, the prefix no longer matches what the server saw before. The page says that is the top cause of misses and offers two fixes: replay reasoning_content, or let the server chain the conversation with previous_response_id.

Details such as whether matching works at token or message level, how long entries are kept, and how the conversation header affects routing are not explained, so they remain unknown.

Setup steps implied by the page

The page has no install or configuration section. Following its examples, you would create an OpenAI-compatible client with your xAI key and the base URL [https://api.x.ai/v1]. Then you would keep the conversation as a list and append the assistant reply and next user message after each turn, without rewriting earlier entries. You would also send the same x-grok-conv-id value on every request in a conversation and read cached_tokens from the usage details.

If you use a reasoning model, you would also carry reasoning_content from each response into the next request, or switch to previous_response_id. How exactly to pass reasoning_content in a request is not shown on this page. It is delegated to a linked page that was not reviewed.

Worked example from the page

The Python example builds a messages list with a system message and a user question, and sets conversation_id to conv_abc123. It makes a first call to grok-4.7 with the x-grok-conv-id header and prints cached_tokens. It then appends the assistant reply and a new user message, and calls again with the same header. The page labels the second call a cache hit because the prefix is unchanged. JavaScript follows the Python flow. The first curl request already includes an assistant reply; its second request appends only the next user message. That differs from the Python and JavaScript versions, which add the assistant reply only after the first call, and it is a documentation quirk, not an observed failure. The page does not show the token counts these calls return, so the sample outputs are unknown.

The three miss examples are curl requests. One is meant to shorten the assistant message, one is meant to remove it, and one swaps the system and user messages. In the captured plain text, these changes are marked only by code-block annotations (deletedLines and addedLines) and short What changed notes. The edit example lists both the original and the shortened assistant message, and the removal example's text still contains the assistant line that the annotation marks as deleted. Read as plain text without that highlighting, these two samples can be confusing, and copying them literally would not reproduce the described changes. The reordering example's text does show the user message placed before the system message.

Assessment & evidence

Limitations and caveats

The page covers only what breaks caching in multi-turn conversations. It does not explain cache lifetime, minimum prompt size, per-model support, cache scope across users or keys, privacy handling of cached data, rate-limit effects or how to debug a miss beyond the listed causes. It also does not cover other things that might change a prefix, such as editing tool definitions, changing the model or changing request parameters. It only discusses message edits, removals and reordering.

The code was not run, so it is unknown whether the Python and JavaScript samples work as written. The model name grok-4.7 and the header x-grok-conv-id may change. The example system message names the assistant as built by SpaceXSI, which is just sample text. Plain-text capture loses the diff highlighting in the miss examples, as noted in the example section. The page is one part of a documentation set, and its guidance depends on linked pages that were not reviewed.

Why preserving conversation history matters

The page's core idea is that cache hits depend on an unchanged prompt prefix. In a multi-turn conversation each new request resends the earlier messages. According to the page, if those earlier messages are identical and in the same order as in the previous request, with only new messages appended at the end, the request can get a cache hit. Editing, removing or reordering any earlier message changes the prefix and, per the page, causes a cache miss. The page's three miss examples are intended to illustrate those three changes. As the example section explains, the captured text of the edit and removal examples does not literally show the described change, so they are best read as illustrations of the rule rather than exact templates.

For reasoning models, the page calls omitting reasoning_content from previous responses the top cause of cache misses. It documents two ways to continue a conversation while keeping cache hits: send back the encrypted reasoning content by including reasoning_content from the previous response, or use stateful responses with previous_response_id so the conversation continues automatically. The page does not compare the two options, and the details of each are on linked pages that were not reviewed. Nothing was executed, so all of this is the page's own description, not observed behaviour.

Alternatives and related material

Within the page, the alternatives are for reasoning models only: replay reasoning_content yourself, or use previous_response_id to let the server continue the conversation. The page does not compare their trade-offs.

The page links to Usage and Pricing for cost and usage details, Encrypted Reasoning Content for the reasoning_content approach, and Chaining the Conversation for previous_response_id. Those pages were not reviewed here and are the natural next reading. How this guidance compares with prompt-caching documentation from other model providers was not assessed.

Evidence base

The evidence is the single documentation page at [https://docs.x.ai/developers/advanced-api-usage/prompt-caching/multi-turn.md]. It contains the What Breaks Caching heading and the rule to only append new messages; a warning box about keeping earlier messages unchanged and including reasoning_content for reasoning models; the two reasoning-model options (reasoning_content and previous_response_id); a cache-hit example in Python, curl and JavaScript; three curl cache-miss examples for editing, removing and reordering messages, each with a short What changed note; and a closing Next link. The page appears complete from its heading to that closing link.

The page links to three other pages: Encrypted Reasoning Content, Chaining the Conversation and Usage and Pricing. Those pages were not part of the evidence. No second source, repository, release notes or licence file was available, and nothing was executed, so every statement about caching behaviour comes from the page itself.

Access & freshness

Costs

The page gives no prices. It says in its sample text that caching makes responses faster and cheaper, but it states no cached-token rate, discount, minimum charge or free allowance. Pricing is delegated to a linked Usage and Pricing page that was not reviewed, so the cost of cached versus uncached tokens is unknown.

Using the examples requires calls to the xAI API with an API key, and the charges for those calls are not described in the evidence. Whether reading the page requires an account, or any other access condition, is not stated and remains unknown.

Rights and reuse

The captured page contains no licence, copyright notice or reuse terms. The code snippets look intended as illustrative examples, but the page does not state permission to copy, modify or redistribute them or the surrounding text. Reuse terms are therefore unknown. Check xAI's documentation terms before republishing any of the text.

Freshness

The capture is dated 2026-11-15, but the page provides no publication or update date. Its age, current model availability and current API behaviour are unknown. The page also shows no version number. The page may have changed since capture, and the examples may not match your account's current models. Re-check the live page and the xAI model list before relying on it.

Suggested next steps

If you build multi-turn chats on the Grok API, keep your message history append-only and avoid trimming or rewriting earlier turns. If you must shorten long histories, expect cache misses, because the page treats removing messages as a miss. With reasoning models, carry reasoning_content forward or use previous_response_id.

Check the effect yourself by logging usage.prompt_tokens_details.cached_tokens on each turn, as the examples do, and compare results with and without your changes. Before relying on cost savings, read the Usage and Pricing page, which this page links to, and confirm the rates for your model. If you plan to copy the text or code into your own materials, confirm xAI's reuse terms first.

Evidence for this report

selected-page.txtselected · checked 2026-11-15