from openai import OpenAI
conversation_id = "thread-42" # any opaque string stable across turns
client_sticky = OpenAI(
base_url="https://api.malibu.tech/v1",
api_key="<your-malibu-api-key>",
default_headers={"X-Malibu-Conversation": conversation_id},
)
# Turn 1: cold prefix, provider caches it
resp1 = client_sticky.chat.completions.create(
model="mlx-community/Qwen2.5-7B-Instruct-4bit",
messages=[
{"role": "system", "content": "You are a code review assistant."},
{"role": "user", "content": "Explain the SOLID principles."},
],
)
# Turn 2: same conversation_id -> same provider, warm KV-cache reused
resp2 = client_sticky.chat.completions.create(
model="mlx-community/Qwen2.5-7B-Instruct-4bit",
messages=[
{"role": "system", "content": "You are a code review assistant."},
{"role": "user", "content": "Explain the SOLID principles."},
{"role": "assistant", "content": resp1.choices[0].message.content},
{"role": "user", "content": "Which one is most often violated in practice?"},
],
)
# Observe the cache hit
print(resp2.usage.model_dump())
# {"prompt_tokens": 1500, "completion_tokens": 300, "total_tokens": 1800, "cached_prompt_tokens": 1200}