Caching efficiency for Grok 4.6 at Vertex

Hi, we have a problem with caching efficiency for Grok 4.6 on Vertex (global endpoint, OpenAI-compatible Chat Completions). In our test scenario, an append-only multi-turn conversation of 8 calls (~18k → 40k tokens, earlier messages never changed), the prompt cache is mostly lost when calls are ~90 s apart, which is typical for agents that run tools between model calls:

  • 3 s between calls: 76% of prompt tokens cached
  • 90 s between calls: 9% and 31% cached; 9 of 14 follow-up calls got cached_tokens = 0

Example zero-cache request IDs: 12b1e2df-6ae9-4cf6-af61-29814886d774, 144f70b6-35ef-453e-8d96-dd0f3cfb8cee, 631eea8c-94a2-4bec-88e4-67a031ba9113. The repro script is attached; run it with PROJECT_ID=<project> PAUSE_S=90 RUNS_B=1 SKIP_A=1 python grok_vertex_cache_repro.py.

“”"Repro: Grok 4.6 prompt caching on Vertex AI (global endpoint, OpenAI-compatible Chat Completions).

Requirements: pip install openai google-auth ; credentials via ADC / GOOGLE_APPLICATION_CREDENTIALS.

Usage: PROJECT_ID= [PAUSE_S=90] [RUNS_B=2] [SKIP_A=1] python grok_vertex_cache_repro.py

Test A (control): the same ~18k-token prompt sent 5 times.

Test B (multi-turn): a conversation that only ever appends. Starts at ~18k tokens and appends an

assistant turn + a ~2.5k-token user turn per call (8 calls), like an agent tool loop. PAUSE_S sets the gap between calls (default 3 s). Earlier

messages are never modified, so each prompt is an exact prefix extension of the previous one and

call N should be able to read call N-1’s whole prompt from cache.

“”"

import os

import random

import time

import uuid

import google.auth

import google.auth.transport.requests

from openai import OpenAI

PROJECT = os.environ[“PROJECT_ID”]

MODEL = “xai/grok-4.6”

RUNS_B = int(os.environ.get(“RUNS_B”, “2”))

PAUSE_S = int(os.environ.get(“PAUSE_S”, “3”))

SKIP_A = os.environ.get(“SKIP_A”) == “1”

creds, _ = google.auth.default(scopes=[“https://www.googleapis.com/auth/cloud-platform”])

creds.refresh(google.auth.transport.requests.Request())

client = OpenAI(

base_url=f"https://aiplatform.googleapis.com/v1/projects/{PROJECT}/locations/global/endpoints/openapi",

api_key=creds.token, timeout=120, max_retries=0,

)

rnd = random.Random(42)

WORDS = [“alpha”, “delta”, “river”, “stone”, “cloud”, “metric”, “vertex”, “cache”, “table”, “window”, “orange”, “signal”]

BASE = " ".join(rnd.choice(WORDS) + str(rnd.randint(0, 999)) for _ in range(9000))

def call(messages):

t0 = time.time()

r = client.chat.completions.create(model=MODEL, messages=messages, max_tokens=16)

u = r.usage

cached = getattr(getattr(u, "prompt_tokens_details", None), "cached_tokens", 0) or 0

return u.prompt_tokens, cached, time.time() - t0, r.id

def test_a():

print("\\n== Test A: identical prompt x5")

msgs = \[{"role": "user", "content": f"\[run {uuid.uuid4()}\]\\n{BASE}\\nReply with OK."}\]

for i in range(5):

    p, c, dt, rid = call(msgs)

    print(f"  call {i + 1}: prompt={p:>6} cached={c:>6} ({100 \* c // p:>3}%) {dt:4.1f}s id={rid}")

    time.sleep(PAUSE_S)

def test_b(run):

print(f"\\n== Test B run {run}: append-only conversation, 8 calls")

msgs = \[{"role": "user", "content": f"\[run {uuid.uuid4()}\]\\n{BASE}\\nReply with OK."}\]

prev, tot_p, tot_c = 0, 0, 0

for i in range(8):

    if i:

        msgs += \[{"role": "assistant", "content": f"step {i}: checking"},

                 {"role": "user", "content": " ".join(rnd.choice(\["row", "col", "sum", "avg", "id"\]) + str(rnd.randint(0, 99999))

                                                      for \_ in range(900)) + "\\nReply with OK."}\]

    p, c, dt, rid = call(msgs)

    tot_p, tot_c = tot_p + p, tot_c + c

    print(f"  call {i + 1}: prompt={p:>6} cached={c:>6} ({100 \* c // p:>3}%) cacheable-from-previous={prev:>6} {dt:4.1f}s id={rid}")

    prev = p

    time.sleep(PAUSE_S)

print(f"  run {run} total: {tot_c}/{tot_p} prompt tokens cached ({100 \* tot_c // tot_p}%)")

print(f"project={PROJECT} model={MODEL} endpoint=global pause={PAUSE_S}s start={time.strftime(‘%Y-%m-%dT%H:%M:%SZ’, time.gmtime())}")

if not SKIP_A:

test_a()

for k in range(1, RUNS_B + 1):

test_b(k)
1 Like