Using Malibu with the OpenAI SDK
The OpenAI SDK is the Malibu SDK. Every buyer-facing pattern in one page.Malibu speaks OpenAI-compatible
/v1/chat/completions. If you have openai-python or openai-node installed you already have a Malibu client — set the base_url, use your Malibu API key, done. This page collects the copy-pasteable patterns for every buyer-facing surface: streaming, tool calling, structured output, sticky conversations with prefix caching, provider pinning, and signed-receipt verification.
There is no malibu SDK. The OpenAI SDK IS the Malibu SDK. That is deliberate — every agent framework already targets openai, and a wrapper would just add friction. When you need something the OpenAI SDK doesn’t cover directly (a Malibu-specific header, a receipt), it’s a one-liner on the client or a call to the malibu-verify CLI.
Base setup
Basic completion
client.models.list() or curl https://api.malibu.tech/v1/models.
Streaming
Tool calling
Malibu ships OpenAI-shapetools + tool_choice. Multi-turn tool loops, streamed tool-call deltas, and tool_call_id back-references all work for Qwen 2.5 and Llama 3.3 chat templates.
function.arguments is a JSON-encoded STRING per OpenAI’s wire shape — json.loads(call.function.arguments) to get a dict.
Caps: 1 MiB per function.arguments, 2 MiB per response, 256 KiB per role: "tool" message.
For the buyer-side safety obligation on emitted tool calls, see Agentic tooling → Buyer-side validation.
Structured output (JSON schema)
Grammar-constrained sampling. Works in both streaming and non-streaming.strict: true guarantees schema conformance via constrained decoding.
Sticky conversations (prefix-cache reuse)
Multi-turn conversations get routed to the same provider via a buyer-supplied opaque tag on theX-Malibu-Conversation header. When the sticky provider serves turn N+1, its warm KV-cache is reused for the shared prefix and the request is billed at a discounted prompt_cache_hit_rate_per_mtok for the cached tokens.
- The tag is opaque and buyer-chosen. Use a stable per-thread identifier.
- The gateway HMACs the tag with your
account_idbefore forwarding — one buyer’sthread-42never collides with another buyer’sthread-42. - If the sticky provider is unavailable, the request falls through to normal routing and
cached_prompt_tokenswill be0. - Legacy providers that don’t report cache hits emit
cached_prompt_tokens: 0— safe to consume unconditionally.
Pinning to a specific provider
Route every request in a session to a specific provider viaX-Malibu-Pin-Provider. Useful for reproducibility, debugging, or when you’re contracting a specific Mac.
503 no_provider_available rather than routing to a substitute — pinning is strict.
X-Malibu-Pin-Provider and X-Malibu-Conversation compose: pinning trumps sticky. If you pin, sticky becomes redundant.
Which provider served this request?
Every response carriesX-Malibu-Provider identifying which provider actually served it — useful for logging, debugging, or feeding a subsequent pin.
Signed inference receipts
Every response carries an Ed25519-signed receipt in theX-Malibu-Receipt response header. The receipt binds the canonical prompt hash, output hash, provider public key, model hash, and timestamp — the provider signs the tuple, you verify it offline, no gateway trust required. This is what makes Malibu verifiable inference.
Capture the receipt at request time:
malibu-verify CLI:
malibu-verify in a nightly job, keep the verified receipts for dispute resolution.
Full working example
Header reference
Full reference: API → Headers.
Framework compatibility
Any framework built onopenai-python or openai-node works out of the box:
- LangChain.
ChatOpenAI(base_url=..., api_key=...)— full compatibility including tool calling and structured output. - LlamaIndex.
OpenAI(base_url=..., api_key=...)— same story. - Instructor. Structured output via
response_formatworks; Instructor’spatch()layer sees Malibu as plain OpenAI. - Aider. Point
OPENAI_API_BASEathttps://api.malibu.tech/v1; existing config keys work. - Cline / Continue. Set the OpenAI-compatible endpoint to
https://api.malibu.tech/v1.
What’s not in the OpenAI SDK
Two things live outside the SDK because they don’t fit its request/response shape:- Receipt verification — one-way offline check, better as a separate tool (
malibu-verify). - Provider selection / autotune — buyer-side (
malibu-cli models browse,malibu-cli autotune) is separate from the API surface.