How to Override Default Routing Behavior Per Request in FreeLLMAPI
Send an X-FreeLLMAPI-Routing-Override HTTP header containing a JSON payload with parameters like strategy, keySelection, or explore to temporarily change routing logic for a single request without modifying global settings.
FreeLLMAPI uses a sophisticated router with bandit-style scoring, rate-limit caps, and head-room thresholds to determine which model and API key to use for each request. While the global configuration handles most traffic patterns, you often need to deviate from these defaults for specific API calls—such as forcing the fastest model for latency-sensitive operations or exploring new models during beta testing. By sending a per-request routing override in a dedicated HTTP header, you can modify routing behavior on the fly while leaving the persisted database and environment configuration untouched.
Understanding the X-FreeLLMAPI-Routing-Override Header
The X-FreeLLMAPI-Routing-Override header accepts a UTF-8 encoded JSON object containing one or more override keys. The router merges these temporary settings with the global configuration for the duration of the request, then discards them after completion.
Supported Override Parameters
All override keys are optional—omitted values fall back to global settings:
strategy– Switches the routing strategy topriority,balanced,smartest,fastest,reliable, orcustomfor the current request only.keySelection– Changes the key-selection policy toautoorleast-remainingto control API key consumption.customWeights– Supplies a temporary weight vector withreliability,speed, andintelligencevalues to customize scoring.explore– Forces the router to explore unmeasured models by settingtrueorfalse, overriding the globalexplore_enabledsetting.headroom– Provides ad-hoc head-room thresholds withrampStartandfloorvalues to adjust capacity limits for high-budget requests.
The header name is case-insensitive, but the JSON must not contain secrets or sensitive credentials. Unknown keys are silently ignored by the router.
Implementation Examples
cURL Example
Use this approach for testing or shell-based automation where you need lowest-latency routing:
curl https://api.freellmapi.com/v1/chat/completions \
-H "Authorization: Bearer $YOUR_KEY" \
-H "Content-Type: application/json" \
-H "X-FreeLLMAPI-Routing-Override: {\"strategy\":\"fastest\",\"keySelection\":\"least-remaining\"}" \
-d '{
"model":"gpt-4o-mini",
"messages":[{"role":"user","content":"What is the weather in Paris?"}]
}'
JavaScript Fetch Example
For Node.js applications requiring dynamic exploration of new models:
await fetch("https://api.freellmapi.com/v1/chat/completions", {
method: "POST",
headers: {
"Authorization": `Bearer ${process.env.FREELLMAPI_KEY}`,
"Content-Type": "application/json",
"X-FreeLLMAPI-Routing-Override":
JSON.stringify({ explore: true, headroom: { rampStart: 0.3, floor: 0.1 } })
},
body: JSON.stringify({
model: "auto",
messages: [{ role: "user", content: "Explain quantum tunnelling." }]
})
});
How the Server Processes Routing Overrides
The request lifecycle handles per-request overrides through specific pipeline stages in the FreeLLMAPI source code:
-
Header Extraction – In
server/src/routes/proxy.ts(approximately lines 450–460), the router middleware extracts the header viaparseRoutingOverride(req.headers.get('x-freellmapi-routing-override')). -
Validation – The
server/src/services/router.tsmodule validates the payload against the internalRoutingOverrideSchema(defined around line 1905) using Zod. Invalid fields are discarded without error, allowing the request to proceed with global defaults. -
Temporary Context Creation – The
applyRoutingOverridesfunction (approximately line 2152 inrouter.ts) merges the parsed override with persisted settings, creating a temporaryRoutingContextobject. -
Routing Decision – The core
routeRequestpipeline uses the mergedRoutingContextfor all scoring, head-room calculations, and key-selection logic. The normal flow continues with lease acquisition, model selection, and provider calls. -
Cleanup – After the request completes, the temporary override context is discarded. No changes are written to the database or
.envfiles.
Common Use Cases
- Latency-sensitive operations – Force the fastest available model with
{ "strategy":"fastest" }for real-time applications. - Quota management – Prefer less-used API keys using
{ "keySelection":"least-remaining" }to avoid hitting rate limits. - Model exploration – Enable
{ "explore":true }during beta testing to route traffic to newly added models with insufficient historical data. - Custom scoring experiments – Apply temporary weight vectors like
{ "customWeights":{ "reliability":0.5, "speed":0.3, "intelligence":0.2 } }for A/B testing routing strategies. - High-budget requests – Adjust capacity thresholds with
{ "headroom":{ "rampStart":0.4, "floor":0.15 } }for premium workloads.
Key Source Files for Reference
server/src/routes/proxy.ts– Entry point for the/v1/chat/completionsendpoint; extracts and initial-parses the override header.server/src/services/router.ts– Core routing engine containingRoutingOverrideSchema,applyRoutingOverrides, and therouteRequestimplementation.server/src/services/model-weight-overrides.ts– Handles global per-model weight overrides (distinct from per-request overrides).shared/types.ts– TypeScript definitions forRoutingContextand override object shapes.
Summary
- Send the
X-FreeLLMAPI-Routing-Overrideheader as a JSON object to modify routing for single requests. - Supported keys include
strategy,keySelection,customWeights,explore, andheadroom. - The server validates overrides against
RoutingOverrideSchemainserver/src/services/router.tsand merges them viaapplyRoutingOverrides. - Overrides affect only the current request lifecycle and leave global configurations untouched.
- Reference
server/src/routes/proxy.tsfor header extraction logic androuter.tsfor validation and merging implementations.
Frequently Asked Questions
What is the exact header name for per-request routing overrides?
The header name is X-FreeLLMAPI-Routing-Override. According to the source code in server/src/routes/proxy.ts, the header is case-insensitive during extraction, but conventionally uses this PascalCase format with hyphens.
Can I combine multiple routing parameters in a single override?
Yes. The JSON payload accepts multiple keys simultaneously. For example, you can combine {"strategy":"fastest","explore":true,"keySelection":"least-remaining"} to apply all three overrides to the same request. The router merges these with global defaults, applying only the specified overrides while maintaining other settings.
Do per-request routing overrides persist after the request completes?
No. The override context exists only for the duration of the request lifecycle. The applyRoutingOverrides function in server/src/services/router.ts creates a temporary merge that is discarded after routeRequest finishes. No data is written to the database or environment files.
What happens if I send an invalid JSON payload in the override header?
The router validates the payload against RoutingOverrideSchema using Zod. Invalid fields or malformed JSON are silently ignored, and the request proceeds using only the global configuration. The server does not return an error for invalid override syntax to ensure high availability, though invalid overrides are logged for debugging purposes.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →