The Route: Order status
The request goes to OpenAI first, and to Anthropic as well when OpenAI is still answering after 2000 ms. The first full answer wins.
A worked example: one endpoint that asks gpt-6.1-sol first, asks claude-fable-5-1 too when the first has not answered within 2 seconds, and answers with whichever full reply comes first.
The request goes to OpenAI first, and to Anthropic as well when OpenAI is still answering after 2000 ms. The first full answer wins.
You need a Model Waygate account and an API key of your own with each provider. Model Waygate stores the provider keys encrypted and uses them only to forward your requests; your application only ever holds a gateway key. The wizard has no hedge among its starting routes, so you start from the input alone and draw the hedge in the route editor.
OpenAI with your OpenAI key, then an Anthropic provider named Anthropic with your Anthropic key. Each row's Test button asks the provider for its models, which proves the key works before any request depends on it.Order status. The slug fills in as order-status and becomes part of the endpoint's URL, never to change.Draw it myself. Create Endpoint opens the route editor with the input alone.hedge-1, output-1 and output-2. Select the input and set Routes to hedge-1.output-1 and set Provider OpenAI, Model gpt-6.1-sol; select output-2 and set Provider Anthropic, Model claude-fable-5-1.hedge-1, set Primary to output-1 and Backup to output-2, and Backup delay (milliseconds) to 2000. Then tick the box that says "The backup is a second, separate call to its provider. Both calls may be charged to your provider keys, and both count against their rate limits." A hedge cannot be saved until it is ticked.mw_ and is shown once) and copy the endpoint's URL beside it.The diagram above is the endpoint's whole route: the request goes into a hedge, which asks its primary and, only if that is still pending after the delay, its backup as well. Most requests are answered inside the delay and make one call; the hedge only pays for a second call on the slow tail.
hedge hedge-1 primary started → OpenAI (gpt-6.1-sol) → hedge hedge-1 won when OpenAI answered inside the delay, and hedge hedge-1 backup after 2000ms → Anthropic (claude-fable-5-1) → hedge hedge-1 won; other cancelled when Anthropic was asked after it and answered first.GET /requests/{id} in the control API gives each call its hedge_leg and hedge_result.model field names the provider model that actually answered, so your code can tell the backup's answer from the primary's without asking the dashboard.Point the SDK at the endpoint's URL, https://modelwaygate.com/USER_ID/order-status/api/v1, with the gateway key in place of an OpenAI key. USER_ID is your account's public id, which the endpoint's URL on the dashboard already carries. Nothing else in your code changes: the hedge happens inside the gateway, and your code waits for one answer as it always did.
mw_your_gateway_key stands for the key you minted. Keep it in an environment variable or a secret store, never in source control.stream field, or stream: false. Embeddings are raced too. See Streams below for what a hedge does with a stream.curl https://modelwaygate.com/USER_ID/order-status/api/v1/chat/completions \
-H "Authorization: Bearer mw_your_gateway_key" \
-H "Content-Type: application/json" \
-d '{
"model": "order-status",
"messages": [
{"role": "system", "content": "You answer questions about orders in one short paragraph."},
{"role": "user", "content": "Where is order 1042?"}
]
}'
from openai import OpenAI
client = OpenAI(base_url="https://modelwaygate.com/USER_ID/order-status/api/v1", api_key="mw_your_gateway_key")
response = client.chat.completions.create(
model="order-status",
messages=[
{"role": "system", "content": "You answer questions about orders in one short paragraph."},
{"role": "user", "content": "Where is order 1042?"},
],
)
print(response.model, response.choices[0].message.content)
import OpenAI from "openai";
const client = new OpenAI({ baseURL: "https://modelwaygate.com/USER_ID/order-status/api/v1", apiKey: "mw_your_gateway_key" });
const response = await client.chat.completions.create({
model: "order-status",
messages: [
{ role: "system", content: "You answer questions about orders in one short paragraph." },
{ role: "user", content: "Where is order 1042?" },
],
});
console.log(response.model, response.choices[0].message.content);
OkHttpClient client = new OkHttpClient();
String json = "{\"model\": \"order-status\", \"messages\": ["
+ "{\"role\": \"system\", \"content\": \"You answer questions about orders in one short paragraph.\"}, "
+ "{\"role\": \"user\", \"content\": \"Where is order 1042?\"}]}";
Request request = new Request.Builder()
.url("https://modelwaygate.com/USER_ID/order-status/api/v1/chat/completions")
.header("Authorization", "Bearer mw_your_gateway_key")
.post(RequestBody.create(json, MediaType.get("application/json")))
.build();
try (Response response = client.newCall(request).execute()) {
System.out.println(response.body().string());
}
import Data.Aeson (encode, object, (.=))
import Network.HTTP.Client
import Network.HTTP.Client.TLS (tlsManagerSettings)
main :: IO ()
main = do
manager <- newManager tlsManagerSettings
initial <- parseRequest "POST https://modelwaygate.com/USER_ID/order-status/api/v1/chat/completions"
let turn role content = object ["role" .= (role :: String), "content" .= (content :: String)]
body = object ["model" .= ("order-status" :: String), "messages" .= [turn "system" "You answer questions about orders in one short paragraph.", turn "user" "Where is order 1042?"]]
request = initial{requestHeaders = [("Authorization", "Bearer mw_your_gateway_key"), ("Content-Type", "application/json")], requestBody = RequestBodyLBS (encode body)}
response <- httpLbs request manager
print (responseBody response)
A hedge is not a fallback: it does not wait for the first provider to fail before asking the other. It waits a fixed delay, then asks both, and takes whichever full reply comes first.
The backup is a second, separate call. The editor says so beside the box you ticked, and it is the price of a faster slow tail.
The delay decides how often you pay for a second call. Too short, and most requests make two; too long, and the slow ones wait almost as long as they did without a hedge.
output-2's counts are how often the backup was asked and how often it won. When it wins most races it is simply the faster provider, and may be worth making the primary.Hedging is for buffered requests only. A streamed request is never raced: nothing is sent twice, and nothing is cancelled.
stream=True (stream: true) the hedge acts as a two-child fallback. OpenAI is asked, and Anthropic only if OpenAI fails before anything has reached your code. The route taken says so: hedge hedge-1 backup in turn (stream).The backup is optional while the primary can still answer, so none of these stops the request: it waits for the primary alone, and the route taken says why, such as hedge hedge-1 backup not launched (call cap).