Skip to main content
When you run agents, long system prompts, or multi-turn conversations on Grok, prompt caching bills the cached portion of your input at 0.25× (a 75% saving) — and no code changes are needed, because caching is fully automatic. Set expectations up front: xAI states explicitly that cache entries may be evicted under memory pressure, on restart, or when a request lands on a different server, so hits are not guaranteed. Treat the cache discount as a nice-to-have, and budget at the uncached price. This page follows xAI’s official documentation (docs.x.ai/developers/advanced-api-usage/prompt-caching) and is grounded in hands-on testing of grok-4.6 on the APIYI gateway on 2026-08-19 (124 calls, reconciled line-by-line against backend billing records).

In One Sentence

Whenever the opening portion (the prefix) of your request matches a recent request byte for byte, the upstream skips the redundant work: the matched portion is billed at 0.25×. No parameters, no markers. How it differs from the other two:
  • vs Claude: no cache_control markers — it just happens once the conditions are met
  • vs OpenAI: equally automatic and equally free to write, but Grok gives you no prompt_cache_key-style routing control

Why Bother — Look at the Multipliers

Taking the model’s raw input token price as : Break-even: the second request. There is no write fee to amortize, so the first time a prefix is reused, everything you save is pure gain. In dollars for grok-4.6 (per 1M tokens, both context tiers): Tier breakpoints and cached-read rates for the other Grok models are in the tiered pricing table on the Grok overview.

Good Fits

  • One long system prompt plus tool definitions, called over and over (agents, support bots)
  • Batch work over one document (50 questions against one contract)
  • RAG where stable document chunks sit at the front of the prompt
  • Multi-turn conversations — but note that on Grok the two ways of doing this behave very differently (see below)

Poor Fits

  • Requests that differ from the very first character every time
  • Prompts below the thousand-token range — in testing, repeatedly calling such a request never built a reusable cache

Both Endpoints, Streaming and Non-Streaming, All Reconciled

/v1/chat/completions and /v1/responses, each streaming and non-streaming: we reconciled all four combinations against backend billing records on 2026-08-19, and the cached portion was billed at the cache rate in every one:
No client-side adaptation is needed for the gateway. Cache behavior is passed through to the upstream, cached_tokens is echoed back verbatim, and the backend bill lists the cached portion as its own “cache read” line item.

Conditions for a Hit

Hits Round Down to 128 Tokens

Two rounds of testing agree: an 8802-token prefix hit 8704 (= 68 × 128), and an earlier round’s 2735-token prefix hit 2688 (= 21 × 128). So cached_tokens is normally a little smaller than your stable prefix — that is expected.

Append Only: Editing History Breaks It

Same prefix, sent back to back, with one call altered: What this means in practice: stable content first, volatile content last.

Minimal Runnable Example

Send the same long prefix twice with different questions: the first writes the cache, the second hits it.
Expected output:
On the second call cached is close to the system prompt length (rounded down to 128), and that portion is billed at 0.25×.
The /v1/responses endpoint works the same way automatically; the field is usage.input_tokens_details.cached_tokens. Long conversations get an extra benefit on that endpoint — see “Long conversations belong on the responses chain” below.

Telling a Hit From a Miss — Read the usage Field

How to Read It: Small Values Are Not Hits

Do not just check for “greater than zero”. Compare cached_tokens against your stable prefix length: In testing even a cold first call sometimes echoes a value of one or two hundred. Do not be fooled — that does not mean your prefix was cached.

Reconciling: The Cache Billing Detail in the Console

The backend log for a single call lists the cached-read token count and its discount multiplier as a separate line, which you can match against the cached_tokens in the response. When you need to know exactly how one call was billed, that is the authoritative view. Three-step self-check:
  1. Build a stable prefix above a thousand tokens and send two requests back to back
  2. The second response should show cached_tokens clearly in the thousands
  3. In the backend call logs, that request shows a “cache read” line item and a visibly lower input cost than the first

Improving Your Hit Rate

Engineer a Stable Prefix

  • Long instructions, few-shot examples, and tool definitions go first; user input and timestamps go last
  • Keep tool-definition ordering and JSON serialization fixed (do not let your serializer shuffle keys)
  • Image inputs participate in prefix matching too — keep base64 / URL and parameters identical when reusing one
  • Reuse the same prefix in a tight burst rather than spreading calls out
The methodology matches OpenAI’s; for the long version see the OpenAI prompt caching guide.

Long Conversations Belong on the Responses Chain

This is an easily missed difference on Grok: So for long conversations and multi-step agents, prefer the Responses API chain:
Endpoint differences are covered in the endpoint overview on the Grok overview page.

About x-grok-conv-id

xAI’s best practices recommend sending an x-grok-conv-id header (a UUID or session ID) on every request to improve hit rates. We ran a symmetric A/B on APIYI — several independent prefixes with and without the header, several reuses each — and saw no observable difference between the two groups. Sending it does no harm, but do not count on it for hit rates.

Hit Rates and What to Expect

Cache hits are not guaranteed. xAI’s documentation states that entries can be lost to memory pressure, service restarts, or a request being routed to a different server.In testing, most requests hit when a stable prefix is reused in a tight burst, but there is real jitter, and it originates upstream — nothing on the caller’s side controls it. Budget at the uncached price and treat hits as a bonus.
One more thing worth saying plainly: the value of caching is cost, not speed. Measured time-to-first-token differed by only a few hundred milliseconds between hits and misses — do not expect caching to make long-context requests fast.

Common Pitfalls

Quick Comparison With Other Channels

For caching support across the whole platform, see the cache billing FAQ.
Everything on this page was measured on grok-4.6 (2026-08-19). xAI states that all Grok language models support prefix caching; we have not benchmarked the others one by one, so treat details such as block granularity and short-prompt behavior as something to confirm on your own workload.If the billing you see for a given prefix clearly disagrees with what is described here, contact support with the request-id from the response headers.

Recap

1. Fully automatic

No markers, no write fee. Meet the conditions and it caches; the second reuse is pure savings.

2. Append only

Matching runs byte by byte from the start of messages; editing history invalidates it, and hits round down to 128 tokens.

3. Chain long conversations

Multi-turn chat only reuses the original static prefix; responses + previous_response_id grows hits with every turn.

4. Do not bank on hits

Hits are not guaranteed. Budget at the uncached price and treat the discount as a bonus.