- Set
base_urltohttps://api.deepinfra.com/v1/openai - Set
api_keyto your DeepInfra token - Set
modelto a model from our catalog
Install the SDK
Basic chat completion
Multi-turn conversations
To create a longer conversation, include the full message history in every request. The model uses this context to provide better answers.Supported parameters
We may not be 100% compatible with all OpenAI parameters. Let us know on Discord or by email if something you need is missing.
Service tier
Set the optionalservice_tier parameter to run a request on a non-standard tier. Two tiers are available on tagged models: priority (faster, at a premium) and flex (cheaper, best-effort). Leave service_tier unset for standard real-time scheduling and pricing.
Priority
Setservice_tier to "priority" to request priority inference on supported models. Priority requests get faster time-to-first-token and higher throughput during peak demand.
Flex
Setservice_tier to "flex" to run Chat Completions requests at a lower cost in exchange for slower response times and occasional resource unavailability. It’s ideal for non-production or lower-priority tasks such as model evaluations, data enrichment, and asynchronous workloads. When a model is busy, a flex request may wait up to 10 minutes for available capacity before it runs or is rejected with an HTTP 429, so use it for work you can retry.
Flex inference is billed at a 20% discount off the model’s standard per-token price.
service_tier field confirming which tier was actually used. Not all models support these tiers — check the model page for availability. If a model doesn’t support the requested tier, the request is served at the standard tier and billed at the standard price; no error is returned.
Fail fast
By default, a request sent to a model that is at capacity waits in the queue until capacity frees up. Set the optionalfail_fast parameter to true to get an immediate HTTP 429 instead of waiting.
This is meant for latency-sensitive callers that would rather go somewhere else than sit in a queue — for example, clients that fail over to another provider on rejection. The 429 arrives as soon as the request would have been queued, so you don’t spend your latency budget waiting.
fail_fast request is rejected only when the model is busy enough that serving it would actually be slow. Whenever there is spare capacity the request is admitted and served exactly as if fail_fast were unset, so enabling it does not cost you throughput on an idle model.
When a request is rejected, the response is an HTTP 429 carrying the engine_overloaded code:
A rejected request never reaches the model, so no inference happens and nothing is billed.
fail_fast: true and service_tier: "priority", fail_fast wins — the explicit request not to wait is honored, and you get a 429 rather than a priority slot in the queue.
Max output tokens
The maximum number of tokens that can be generated in a single response is model-dependent, with a hard cap of 16384 tokens for most models. Setmax_tokens to control the limit for a specific request.
Continuing responses beyond the limit
If you need a longer response, use response continuation: send a follow-up request with the previous response included as an assistant message, and the model will continue from where it left off.What’s next
Streaming
Stream tokens as they’re generated.
Structured Outputs
Get responses in JSON format.
Tool Calling
Give models access to external functions.
Vision
Send images alongside text.
Reasoning Models
Control chain-of-thought reasoning behavior.