Max queue wait
Bound how long a request may wait behind queued work, and use the bound to fail over to another provider when sference is congested.
Max queue wait
max_queue_wait_s is an admission bound. It states the maximum projected wait your request is willing to accept behind work that is already queued. If the scheduler cannot admit the request inside that budget, the request is rejected immediately with a max_queue_wait_exceeded error instead of sitting in a queue.
That rejection is the point: it turns "this will be slow" from something you discover after a timeout into something you can act on in milliseconds.
response = client.chat.completions.create(
model="zai-org/GLM-5.3",
messages=[{"role": "user", "content": prompt}],
max_queue_wait_s=15,
)What the bound covers
| Counts against the bound | Does not count |
|---|---|
| Projected wait behind already-queued work | Your own prompt's prefill time |
| Pending time already spent waiting | Decode time after the first token |
| For flex requests, the deliberate dispatch hold | Network transfer to and from the API |
The bound is on congestion, not on prompt size. A 100k-token prompt on an idle fleet is admitted immediately; a short prompt behind a deep queue is not.
Accepted range: 5–300 seconds. Omit the field to use the platform default ceiling (60s for streaming requests, 300s for synchronous ones).
Why you would set it
Fast fallback to another provider
This is the main use. Set a bound that reflects your real latency budget, and treat max_queue_wait_exceeded as a routing signal: retry the request against your fallback provider, or against a different sference model, rather than waiting.
Rather not write the retry loop?
Model fallback does this server-side: configure an OpenAI-compatible endpoint + API key once in the console, and requests we can't serve (503 / capacity) fail over to it automatically — no code changes, $0 from sference (it's your own key). Keep the manual pattern below when you want to make the failover call yourself, per request.
from openai import OpenAI
import openai
sference = OpenAI(
base_url="https://api.sference.com/v1",
api_key=SFERENCE_KEY,
max_retries=0, # otherwise the SDK retries 429 and 5xx twice before this except runs
)
fallback = OpenAI(base_url="https://api.other-provider.com/v1", api_key=OTHER_KEY)
def complete(prompt: str):
try:
return sference.chat.completions.create(
model="zai-org/GLM-5.3",
messages=[{"role": "user", "content": prompt}],
max_queue_wait_s=15,
)
except (openai.RateLimitError, openai.InternalServerError) as e:
# 429 when your bound fires, 503 at the platform ceiling.
# Both subclass openai.APIStatusError.
if e.code == "max_queue_wait_exceeded":
return fallback.chat.completions.create(
model="gpt-5.6",
messages=[{"role": "user", "content": prompt}],
)
raiseAdopting sference as a preferred provider
The bound is what makes sference safe to put first in a multi-provider chain. You get open-model economics on the overwhelming majority of requests, and a hard, fast signal to fall back when the fleet is busy — instead of a client timeout that wastes the whole budget before failing.
Pair it with service_tier: "flex" when the request is not user-facing: flex requests count their deliberate dispatch hold against the bound, so a bound on a flex request bounds end-to-end scheduling latency rather than just queue depth.
Protecting a batch of concurrent calls
When you fan out hundreds of requests at once, a bound keeps a congested moment from turning into hundreds of connections held open for minutes. Requests that cannot be admitted fail fast and can be retried in a later wave.
The error
A rejected request returns 429 when your max_queue_wait_s bound fires, and 503 when the platform ceiling fires. Both carry a Retry-After header. On /v1/chat/completions and /v1/responses the body is the OpenAI error envelope:
{
"error": {
"message": "...",
"type": "rate_limit_error",
"param": "max_queue_wait_s",
"code": "max_queue_wait_exceeded"
}
}| Field | Your bound (429) | Platform ceiling (503) |
|---|---|---|
type | rate_limit_error | server_error |
code | max_queue_wait_exceeded | max_queue_wait_exceeded |
param | max_queue_wait_s | null |
message | Human-readable detail | Human-readable detail |
The OpenAI SDKs raise openai.RateLimitError for the 429 and openai.InternalServerError for the 503.
On /v1/messages the HTTP body is the Anthropic envelope, which has no code or param: {"type": "error", "error": {"type": "rate_limit_error", "message": "..."}}, plus a top-level request_id when the scheduler made the decision. The ceiling's error.type is overloaded_error. Tell the two apart by status: 429 is your bound, 503 is the ceiling.
The distinction matters for monitoring. A 429 is your own bound: raise it, or route to a fallback. A 503 max_queue_wait_exceeded (param: null on the OpenAI surfaces) is platform capacity. It is sent when the platform's admission ceiling fires, and also when the model's realtime queue is full, realtime dispatch is restarting, or the worker running the request went away. Lowering your own bound turns the first case into a 429 you control, but does nothing for the others: retry after Retry-After, or fail over.
With stream: true, the API holds the response headers only while it can still answer with an HTTP status: up to your bound plus 15 seconds when you set max_queue_wait_s, or about one second for an unbounded realtime request. A rejection in that window arrives as the same HTTP status and body before the stream starts. A rejection that arrives later ends the stream instead: with an error frame on chat completions and messages (see chat and messages), and with a response.failed event on /v1/responses (see Responses errors). The in-stream error also carries retry_after_s and source ("client" or "ceiling").
Rejected requests are not billed
A max_queue_wait_exceeded rejection happens before any inference work, so it costs nothing. Retrying it against a fallback is free on our side.
Interaction with flex
Flex requests are dispatched at lower priority by design, and that hold is counted against max_queue_wait_s. A flex request with a 30s bound will be rejected rather than held for the full flex window — which is usually what you want for a workload that has a deadline, and not what you want for a pure backfill job. For backfill, either omit the bound or use background: true with the 24h batch window instead. The two are mutually exclusive: sending max_queue_wait_s with background: true returns 400 (max_queue_wait_s requires background=false).
Where it is accepted
| Surface | Field |
|---|---|
/v1/chat/completions | max_queue_wait_s |
/v1/responses | max_queue_wait_s |
/v1/messages | max_queue_wait_s |
| Batch rows | Not supported — batch rows are already on the 24h window and cannot be rejected for congestion |
Images and vision
Send images alongside text to multimodal models on sference: request shapes, the detail quality tiers, how images are downscaled, limits, and billing.
Model fallback
Point sference at any OpenAI-compatible endpoint and key, and requests we can't serve (503 / capacity) fail over to it automatically — no code changes.