A hedge helps model calls only when the delay is in queueing, not generation. Pick the delay from time to first token, commit to one stream, count the tokens.
A slow call is not always a long call
Hedged requests are an old tail-latency technique. In "The Tail at Scale" (Communications of the ACM, February 2013), Jeffrey Dean and Luiz André Barroso describe sending a request to one replica and, if it has not answered after a brief delay, sending a second copy to another replica and using whichever answers first. In their BigTable benchmark, hedging after 10 ms cut the 99.9th-percentile latency for reading 1,000 values from 1,800 ms to 74 ms while sending only 2% more requests. They suggest waiting for something like the 95th-percentile expected latency before hedging, to cap the extra work.
The idea transfers to model calls, with two differences that change the design. First, a model call's latency has two parts with different causes: waiting (queueing, prompt processing, time to the first token) and generating (output tokens at some rate). A hedge can help with the first, because a second attempt draws a fresh wait. It cannot help with the second, because a second attempt that produces the same length of answer takes just as long. Second, every attempt costs money. The input tokens of a hedge are paid again, and depending on the provider, so are output tokens produced before the loser is cancelled.
This guide works through both with a small simulation, then gives the delay rule, the cancellation code and the cases where hedging must not be used.
The simulation
The companion example hedged_model_requests simulates 20,000 synthetic calls. Each attempt's wait is drawn independently: usually around 0.6 seconds, but in 4% of attempts a stall adds 2 to 8 seconds, standing in for a busy replica or a long queue. Output length is 150 to 600 tokens at 60 tokens per second, the same for both attempts of a call, so generation takes 2.5 to 10 seconds. Every attempt pays 3,000 input tokens. These are illustrative numbers, not measurements of any provider.
| Policy | Hedge delay | Calls hedged | p99 latency | p99 time to first token | Tokens paid per call |
|---|---|---|---|---|---|
| No hedge | none | 0% | 13.56 s | 6.98 s | 3,375 |
| Hedge on total latency, p95 | 10.49 s | 5.0% | 13.56 s | 6.98 s | 3,528 |
| Hedge on first token, p95 | 1.35 s | 5.0% | 10.88 s | 2.15 s | 3,525 |
| Hedge on first token, p90 | 1.02 s | 10.1% | 10.81 s | 1.83 s | 3,678 |
The second row is the trap. Applying the classic rule to total latency sets the delay at 10.49 seconds. The calls that are still running at that point are mostly long answers, not stalled ones, and a second copy of a long answer finishes no sooner. The p99 does not move at all, and the hedges still cost 5% more input tokens.
Hedging on time to first token works. A call that has not produced its first token after 1.35 seconds is very likely stalled, a fresh attempt usually starts within a second, and the p99 falls from 13.56 to 10.88 seconds, about 20%, for 5% more input tokens. Lowering the delay to the 90th percentile doubles the hedge rate and the extra cost for almost no further gain.
With the stall rate set to zero, every hedging policy leaves the p99 at 10.69 seconds and still pays its 5 or 10%. Hedging only helps when the slowness is in a part that a second attempt redraws.
Choosing the delay
Measure time to first token per model and per route, separately from total latency, and pick the delay from its distribution. A delay near the 95th percentile caps the hedge rate near 5% if the distribution is stable. Recompute it regularly: a model upgrade, a longer system prompt or a change in traffic moves the distribution, and a stale delay either hedges far too often or never.
Bound the rate as well as the delay. A client-side budget, such as at most 5% of calls in a sliding window, stops a provider-wide slowdown from turning into a doubling of traffic at the worst possible moment. When the budget is exhausted, stop hedging and let the calls wait.
Hedge to a different replica, region or deployment where you can. Two attempts against the same overloaded endpoint share the cause of the stall, and the second one rarely wins.
Committing to a stream and cancelling the other
For streaming calls, the natural moment to decide is the first chunk. Start the primary stream; if no chunk arrives within the delay, open a second; commit to whichever produces its first chunk first and close the other immediately. The example's hedged_stream does exactly that:
async def first_chunk(it):
return await it.__anext__()
async def hedged_stream(open_stream, delay):
a = open_stream()
ta = asyncio.create_task(first_chunk(a))
done, _ = await asyncio.wait({ta}, timeout=delay)
if done:
return ta.result(), a, 'primary'
b = open_stream()
tb = asyncio.create_task(first_chunk(b))
done, pending = await asyncio.wait({ta, tb}, return_when=asyncio.FIRST_COMPLETED)
for t in pending:
t.cancel()
winner = done.pop()
loser = b if winner is ta else a
await loser.aclose() # release the connection promptly
return winner.result(), (a if winner is ta else b), ('primary' if winner is ta else 'hedge')Committing at the first chunk avoids paying for two full answers and avoids showing a user two different answers. The tests in the example check that the hedge wins when the primary is slow, that the losing stream is closed, and that a fast primary never opens a second stream.
Closing the connection does not guarantee that the provider stops generating or stops billing at that instant; that behaviour is provider-specific and worth checking in their documentation and invoices. For non-streaming calls, the example's hedged helper races the whole call and cancels the loser, which is only worth doing when answers are short.
Calls that must never be hedged
A hedge sends the same request twice. That is only safe when the request has no side effects. A model call that is part of an agent step which then executes a tool is safe to hedge only if the tool call happens after the hedge is resolved, on the winning answer alone. Never hedge a request that itself triggers an action, such as a tool-calling API configured to execute server-side tools, unless the action carries an idempotency key and the server deduplicates it.
Also avoid hedging when the two attempts can produce materially different answers and something downstream depends on consistency, for example when an answer is cached or shown alongside an earlier one. Sampling with temperature above zero makes the hedge a different answer, not a faster copy of the same one.
At Belgavi's Book Store, Geetha applies hedging only to the read-only step that drafts a reply to a customer from the case notes. The step that issues a store credit is never hedged; it runs once, on the winning draft, with an idempotency key.
Cost accounting
Count the cost per call, not per hedge. With 3,000 input tokens and a 5% hedge rate, the expected extra input is 150 tokens per call. If output is streamed and the loser is closed at the winner's first chunk, the loser usually has produced nothing. If calls are raced to completion, add the loser's output up to cancellation. Put both numbers on the same dashboard as the p99, so the trade is visible.
Prompt caching can change the arithmetic, because a repeated prefix may be billed at a lower rate on some providers. Do not assume it; measure a hedged call's billed tokens directly.
Verification checklist
- Log time to first token, total latency, the model and the endpoint for every attempt, including the loser.
- Derive the hedge delay from the first-token distribution and record the date it was computed.
- Enforce a hedge-rate budget and alert when it is exhausted.
- Confirm in tests that the losing stream is closed and that no side effect runs before the hedge is resolved.
- Compare billed tokens per call before and after enabling hedging on a canary.