Spaces:
Sleeping
pipeline: give the hedge the web, instead of skipping it
Browse filesWeb queries felt broken next to ChatGPT, and the search was not why.
Measured on "bitcoin price today":
70ms bidding starts
22391ms auction resolves <- 22.3s of nothing on screen
22392ms web search starts
25264ms first token <- search + generation: 2.9s
The pipeline set run_hedge=False for any query needing search, so that
a stale ungrounded draft wouldn't flash before the search. Sound, but
it meant the queries with the longest wait showed nothing at all, while
ordinary queries had hedge tokens covering the same gap.
Now the hedge runs web-grounded instead of not running: at t=0 when the
regex knows, or restarted ~500ms in when the gate says so. And a
web-grounded hedge is allowed to win the auction, which also skips the
redundant second draft.
Same query, after: first token 22391ms -> 3864ms, total 29.8s -> 15.9s.
Non-web queries are unchanged.
Known rough edge: when the auction picks a model other than the hint,
the web hedge is discarded and the client sees two resets in quick
succession. Costs one wasted draft on those queries; hint-priority
routing means the hint model usually wins.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Files changed (3)
842 unmodified lines8438448458468478488492 unmodified lines85285385485585685785885986086186286386486586686786886987087187287387487587687787811 unmodified lines89089189289389489589689789889990090190227 unmodified lines930931932933934935936937938939940842 unmodified lines# would flash a stale "I don't know" to the user before the search kicks in.# We then go straight to the web-enabled draft after the auction.run_hedge = not _needs_web_heuristic(query)if not run_hedge:state["needs_web"] = True# The gate model races the hedge rather than preceding it: its TTFT is2 unmodified lines# Skipped when the regex already decided — that answer costs nothing and# is never wrong in the "web needed" direction.gate_task = (asyncio.ensure_future(webgate.decide(query))if run_hedge and webgate.enabled() else None)hedge_q: asyncio.Queue = asyncio.Queue()async def _hedge_stream():try:resp = Noneasync for ev in chat_stream(hedge_spec, prompts.ANSWER_SYSTEM,query, history=state["history"],prefer_paid=True):if ev["type"] == "delta":hedge_q.put_nowait(("delta", ev["text"]))elif ev["type"] == "final":resp = ev["response"]hedge_q.put_nowait(("final", resp))except Exception:hedge_q.put_nowait(("final", None))hedge_task = asyncio.create_task(_hedge_stream()) if run_hedge else Nonebid_task = asyncio.create_task(bid_collection(state))yield {"type": "stage", "stage": "bidding"}# Forward hedge tokens the moment they exist, while bids come inhedge_final: Optional[object] = None11 unmodified linesif verdict is True:gate_says_web = Truestate["needs_web"] = Trueif hedge_task is not None:hedge_task.cancel()hedge_task = Nonerun_hedge, hedge_done = False, Trueyield {"type": "reset"}yield {"type": "token","text": "🔎 Let me search the web for you…\n\n"}if not run_hedge:await asyncio.sleep(0.05)continue27 unmodified lines"reason": state.get("escalation_reason"),}# A needs_web winner can't use the hedge draft (generated at t=0 with no# web access) — drop it and let the web-enabled draft path run instead.hedge_won = (run_hedge and not state.get("escalated")and state.get("winner") == hedge_keyand not state.get("needs_web"))if not hedge_won:# Another model won (or we're escalating): drop the hedge and# clear its provisional text on the client. If no hedge ran (t=0 web842 unmodified lines8438448458468478488498502 unmodified lines85385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789811 unmodified lines91091191291391491591691791891992092192292392492527 unmodified lines953954955956957958959960961962963964842 unmodified lines# would flash a stale "I don't know" to the user before the search kicks in.# We then go straight to the web-enabled draft after the auction.run_hedge = not _needs_web_heuristic(query)regex_web = not run_hedgeif regex_web:state["needs_web"] = True# The gate model races the hedge rather than preceding it: its TTFT is2 unmodified lines# Skipped when the regex already decided — that answer costs nothing and# is never wrong in the "web needed" direction.gate_task = (asyncio.ensure_future(webgate.decide(query))if not regex_web and webgate.enabled() else None)def _start_hedge(web: bool) -> tuple[asyncio.Task, asyncio.Queue]:"""Launch a speculative draft, optionally web-grounded.A web query used to run NO hedge at all, on the reasoning that ano-web draft would flash something stale. True, but it left thelongest queries with nothing on screen: measured, the auction took22s while the search and generation took 2.9s. The answer isn't toskip the hedge, it's to give the hedge the web."""q: asyncio.Queue = asyncio.Queue()async def _run():try:resp = Noneasync for ev in chat_stream(hedge_spec, prompts.ANSWER_SYSTEM,query, history=state["history"],prefer_paid=True, web=web):if ev["type"] == "delta":q.put_nowait(("delta", ev["text"]))elif ev["type"] == "final":resp = ev["response"]q.put_nowait(("final", resp))except Exception:q.put_nowait(("final", None))return asyncio.create_task(_run()), qhedge_is_web = regex_webhedge_task, hedge_q = _start_hedge(web=regex_web)run_hedge = Truebid_task = asyncio.create_task(bid_collection(state))if regex_web:# Tell the UI a search is underway; the tokens that follow are already# web-grounded rather than a draft awaiting replacement.yield {"type": "stage", "stage": "searching","model": hedge_spec.display_name}yield {"type": "stage", "stage": "bidding"}# Forward hedge tokens the moment they exist, while bids come inhedge_final: Optional[object] = None11 unmodified linesif verdict is True:gate_says_web = Truestate["needs_web"] = True# Swap the ungrounded hedge for a web-grounded one rather than# abandoning generation until the auction: the search itself is# fast (~3s), it was the bid wait that made web queries feel# broken. Restarting here costs ~500ms of discarded tokens.hedge_task.cancel()yield {"type": "reset"}hedge_task, hedge_q = _start_hedge(web=True)hedge_is_web = Trueyield {"type": "stage", "stage": "searching","model": hedge_spec.display_name}if not run_hedge:await asyncio.sleep(0.05)continue27 unmodified lines"reason": state.get("escalation_reason"),}# The hedge stands unless it's ungrounded on a query that turned out to# need the web — a web-grounded hedge is exactly the draft we'd otherwise# be about to run, so keeping it skips the whole second round-trip.hedge_won = (run_hedge and not state.get("escalated")and state.get("winner") == hedge_keyand (hedge_is_web or not state.get("needs_web")))if not hedge_won:# Another model won (or we're escalating): drop the hedge and# clear its provisional text on the client. If no hedge ran (t=0 web
556 unmodified lines55755855956056156256352 unmodified lines61661761861962062162240 unmodified lines663664665666667668669556 unmodified lines</span></div>)}<div className="text-stone-200"><Markdown>{msg.text}</Markdown></div><ImageStrip images={msg.run?.images} />52 unmodified lines</div>)}{live.text && (<div className="text-stone-200"><Markdown highlight={false}>{live.text}</Markdown></div>)}40 unmodified lines}}rows={1}placeholder={PLACEHOLDERS[phIdx]}className="w-full resize-none rounded-full bg-stone-900 px-6 py-3.5 text-left text-stone-100 outline-none placeholder:text-stone-400"/>{/* One button, two jobs: send when idle, stop while streaming —it's the only control that stays live mid-answer. */}556 unmodified lines55755855956056156256352 unmodified lines61661761861962062162240 unmodified lines663664665666667668669670671556 unmodified lines</span></div>)}<div className="text-stone-100"><Markdown>{msg.text}</Markdown></div><ImageStrip images={msg.run?.images} />52 unmodified lines</div>)}{live.text && (<div className="text-stone-100"><Markdown highlight={false}>{live.text}</Markdown></div>)}40 unmodified lines}}rows={1}placeholder={PLACEHOLDERS[phIdx]}// Sits between stone-900 (#1c1917) and stone-800 (#292524) — a full// step to 800 reads as a different surface; this just lifts it.className="w-full resize-none rounded-full bg-[#201d1b] px-6 py-3.5 text-left text-stone-100 outline-none placeholder:text-stone-400"/>{/* One button, two jobs: send when idle, stop while streaming —it's the only control that stays live mid-answer. */}