/chat/completions endpoint. That means you can point the official OpenAI Python SDK at any 0G provider and use features you’re already familiar with — streaming, tool calls, structured output — with billing handled by the 0G ledger instead of an OpenAI account.
How it works
The OpenAI SDK sends a POST to{base_url}/chat/completions with an Authorization: Bearer <key> header. 0G providers accept exactly that shape. You supply:
base_url— frombroker.inference.get_service_metadata(provider)["endpoint"]api_key— anapp-sk-…token frombroker.inference.get_secret(provider)model— from the same service metadata
Install the OpenAI SDK
Basic usage
get_secret() returns a persistent token (never expires by default). For short-lived scripts you can swap it out for ephemeral headers — see Ephemeral tokens below.Streaming
Just passstream=True like any OpenAI call:
Tool / function calling
Provider support for tools depends on the underlying model. If the model is OpenAI-compatible with tool calling (e.g. recent Qwen, Llama, GPT-compatible models), it works identically:Multi-turn conversations
Ephemeral tokens via default_headers
If you don’t want to create a persistent API key, use ephemeral session headers via the client’s default_headers:
Async client
The async version of the OpenAI SDK works identically:With a fine-tuned adapter
If you’ve deployed a LoRA adapter via the fine-tuning flow, pass the adapter name as themodel:
What’s not supported
A few OpenAI features are specific to OpenAI’s infrastructure and won’t work against 0G providers:- File upload / Assistants API — these live on OpenAI’s side
- Embeddings — unless the provider advertises an embedding service type
- Moderation — provider-specific
- Billing / usage endpoints — replaced by
broker.ledger.get_ledger()on 0G
Full example
Troubleshooting
401 Unauthorized
401 Unauthorized
403 Forbidden
403 Forbidden
The
base_url is missing the /v1/proxy path. Always use broker.inference.get_service_metadata(provider)["endpoint"] — it appends the path automatically.Model returns empty response or errors on tool calls
Model returns empty response or errors on tool calls
Not every provider supports every OpenAI feature. Check the model’s capabilities or try a different provider. The chat-completion shape is universal; advanced features vary.
Rate limits / 429
Rate limits / 429
Individual providers set their own rate limits. Transfer more funds to the provider sub-account or spread load across providers.
Next steps
Inference
The end-to-end flow without the OpenAI SDK.
API keys & session tokens
Ephemeral vs persistent tokens and revocation.