Overview
OpenRouter routes a single model slug across multiple backend providers, and each provider runs different hardware, load levels, and weight precisions. By default, OpenRouter balances traffic using effective price, speed, and recent reliability rather than always picking the fastest endpoint.
- Why latencies vary: Different providers use different GPUs, batching settings, and quantization levels, and OpenRouter also shifts traffic dynamically when a provider hits rate limits (
429) or latency spikes. - How to pin quantizations: Pass
provider.quantizationsin your request body (such as["fp8", "bf16"]) so OpenRouter only routes to endpoints serving those exact precision levels.
Steps
- Set
provider.quantizationsin the request payload: Add an array of allowed precision strings (fp16,bf16,fp8,int8,int4, etc.) inside theproviderobject. - Control provider order or sorting: Combine
quantizationswithprovider.sort: "latency"(or"throughput") or an explicitprovider.orderlist to stabilize speed. - Decide whether fallbacks are allowed: Keep
allow_fallbacks: truefor high availability, or setallow_fallbacks: falsewhen you must fail fast rather than drop to another quantization or provider. - Log the actual serving provider from the response: Check
response.providerin OpenRouter’s response metadata to verify which provider handled each call.
Example
export type QuantizationLevel =
| "int4"
| "int8"
| "fp4"
| "fp6"
| "fp8"
| "fp16"
| "bf16"
| "fp32"
| "unknown";
export interface OpenRouterProviderConfig {
quantizations?: QuantizationLevel[];
sort?: "price" | "throughput" | "latency";
order?: string[];
only?: string[];
ignore?: string[];
allow_fallbacks?: boolean;
preferred_max_latency?: number | { p50?: number; p90?: number; p99?: number };
preferred_min_throughput?: number | { p50?: number; p90?: number; p99?: number };
require_parameters?: boolean;
}
export interface OpenRouterChatResponse {
id: string;
model: string;
provider?: string;
choices: Array<{
message: {
role: string;
content: string;
};
}>;
}
export async function callOpenRouterPinnedQuantization(params: {
apiKey: string;
model: string;
prompt: string;
quantizations: QuantizationLevel[];
providerOrder?: string[];
strictNoFallback?: boolean;
}): Promise<{ text: string; providerUsed: string; latencyMs: number }> {
const providerConfig: OpenRouterProviderConfig = {
quantizations: params.quantizations,
allow_fallbacks: params.strictNoFallback ? false : true,
...(params.providerOrder && params.providerOrder.length > 0
? { order: params.providerOrder }
: { sort: "latency" }),
};
const startedAt = performance.now();
const response = await fetch("[https://openrouter.ai/api/v1/chat/completions](https://openrouter.ai/api/v1/chat/completions)", {
method: "POST",
headers: {
Authorization: `Bearer \${params.apiKey}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
model: params.model,
messages: [{ role: "user", content: params.prompt }],
provider: providerConfig,
}),
});
if (!response.ok) {
const errorBody = await response.text();
throw new Error(`OpenRouter request failed (${response.status}):${errorBody}`);
}
const data = (await response.json()) as OpenRouterChatResponse;
const latencyMs = Math.round(performance.now() - startedAt);
return {
text: data.choices[0]?.message?.content ?? "",
providerUsed: data.provider ?? "unknown",
latencyMs,
};
}
Tips
- Include a small list of acceptable precisions: Passing
["bf16", "fp8"]instead of only one precision keeps more endpoints eligible so requests do not fail during traffic spikes. - Measure p90 and p99 latency instead of averages: Tail latency shows when fallback retries or overloaded providers slow down real users.
- Use
require_parameters: truewith strict tool or JSON schemas: This stops OpenRouter from routing to endpoints that ignore structured output or tool parameters.
You must log in or register to comment.
