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

TypeScript / JavaScript:
Get an API key: api.malibu.tech/auth/github/start

Basic completion

Any MLX chat model in the pool works. List available models: client.models.list() or curl https://api.malibu.tech/v1/models.

Streaming

Server-Sent Events, OpenAI-compatible chunks. No Malibu-specific handling needed.

Tool calling

Malibu ships OpenAI-shape tools + 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.
Note. 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 the X-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_id before forwarding — one buyer’s thread-42 never collides with another buyer’s thread-42.
  • If the sticky provider is unavailable, the request falls through to normal routing and cached_prompt_tokens will be 0.
  • 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 via X-Malibu-Pin-Provider. Useful for reproducibility, debugging, or when you’re contracting a specific Mac.
Provider IDs are visible on the pool dashboard at console.malibu.tech. If the pinned provider is offline the request returns 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 carries X-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 the X-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:
Verify offline with the malibu-verify CLI:
Pure Go, no runtime dependencies, cross-platform. When to verify: most buyers verify post-hoc for audit trails rather than on every request. Batch a day’s receipts, run 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 on openai-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_format works; Instructor’s patch() layer sees Malibu as plain OpenAI.
  • Aider. Point OPENAI_API_BASE at https://api.malibu.tech/v1; existing config keys work.
  • Cline / Continue. Set the OpenAI-compatible endpoint to https://api.malibu.tech/v1.
If you find a framework where the OpenAI SDK works but Malibu doesn’t, that’s a wire-shape bug — file an issue with a minimal repro.

What’s not in the OpenAI SDK

Two things live outside the SDK because they don’t fit its request/response shape:
  1. Receipt verification — one-way offline check, better as a separate tool (malibu-verify).
  2. Provider selection / autotune — buyer-side (malibu-cli models browse, malibu-cli autotune) is separate from the API surface.
Everything else — routing, retries, billing, model catalog, model-hash verification, tool calling, structured output, streaming, sticky affinity, provider pinning — is transparent through the OpenAI SDK.