Documentation
¶
Overview ¶
Hedging: ask several providers at once and keep the first answer.
Tail latency is not the average. A provider that usually answers in 400ms occasionally takes eight seconds, and a single request has no way to know which kind it drew until it has already waited. Asking three and keeping the first is how that tail gets cut — the slow draw stops mattering because it is racing two others.
It costs what it sounds like: N upstream calls for one answer. That is the whole trade and it is the customer's to make per request — BUT NOTHING HERE BILLS FOR IT, and a caller must arrange that before wiring this up.
The completion path writes ONE usage record, for the provider that served (controllers/openai_api.go), and recordRefusals bills nothing for the rest (controllers/failover.go). So a three-way hedge burns three real completions upstream and settles one. Left alone, fast mode is a way for a customer to spend 3x our money at 1x their price.
THE RULE, and why the failover ledger cannot be reused for it:
controllers.recordRefusals bills nothing, and its comment says exactly why — "an attempt that refused before producing a token adds nothing to it". That is right for failover, where a loser ERRORED: no tokens, no upstream charge, and the "failover" status exists to record a refusal that cost nothing.
A hedge loser is the opposite case wearing the same shape. It was CANCELLED mid-generation, so the upstream produced tokens and billed us for them. It costs real money and must write a REAL usage row, with the token count it actually reached before Race cancelled it — never the "failover" status, which would file a genuine charge as a free refusal and make fast mode invisible in the ledger the team reads.
So each attempt needs its own writer, not a shared one: the loser's token count is on ITS writer, and a single writer reset between attempts (which is what failover does, controllers/openai_writer.go) destroys exactly the number the row needs. That is a call-site decision about money, which is why this package names it and does not quietly make it.
FIRST BYTE WINS, and that is the only definition of "fastest" a stream can act on. A provider that will finish sooner but has not started is not yet the winner, because the caller is waiting on bytes rather than on completion — and by the time completion is knowable the whole response has arrived and there was nothing to race.
NOTHING HERE TOUCHES A PROVIDER. Every provider already writes into an io.Writer; this hands each attempt a different one and only lets the winner's reach the caller. The seam was already the right shape.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNoAttempts — Race was called with nothing to run. ErrNoAttempts = errors.New("hedge: no attempts") // ErrNoAnswer — every attempt returned without writing a byte, and none of // them reported why. ErrNoAnswer = errors.New("hedge: no attempt produced an answer") )
var ErrLost = errors.New("hedge: another attempt claimed the stream")
ErrLost is what a losing attempt's writer returns once another attempt has claimed the stream. A provider that checks its write error stops early instead of finishing a response nobody will read.
Functions ¶
func Race ¶
Race runs every attempt against dst and returns the index of the one whose bytes reached the caller, with that attempt's own error.
It returns as soon as the WINNER finishes, not when the losers do. A loser is cancelled and left to unwind on its own: waiting for a provider we are abandoning would hand it exactly the latency this exists to avoid.
With no attempts it returns (-1, ErrNoAttempts). With one it is a plain call with one allocation of overhead — so a caller can hedge conditionally without branching on whether hedging is on.