Skip to content

improve(ai): experiment with cached Responses WebSocket prewarm - #122542

Closed
steipete wants to merge 1 commit into
mainfrom
steipete/cranky-mendel-47d82b
Closed

steipete wants to merge 1 commit into
mainfrom
steipete/cranky-mendel-47d82b

Conversation

@steipete

Copy link
Copy Markdown
Contributor

Related: #122497
Related: #122483

Caution

Negative experiment — do not merge. The authenticated latency gate failed. This draft preserves the implementation and evidence, but the direct API-key SSE default should remain unchanged and the prewarm behavior should not ship.

What Problem This Solves

Direct OpenAI Responses users who explicitly select a session-cached WebSocket transport pay the cold connection cost on the first generated turn. The experiment tests whether Codex-style handshake preconnect plus a response.create frame with generate=false can hide that cold cost and make the cached WebSocket path competitive with the direct SSE default.

Why This Change Was Made

The branch reuses the existing session-owned WebSocket cache, starts a v2 WebSocket before payload construction completes, sends a non-generating request with the stable system prompt and tools, waits for response.completed, and lets the real request continue from that response ID. It adds no config, environment, protocol, dependency, or storage surface, and keeps direct API-key traffic on SSE by default.

The implementation is intentionally isolated from normal prompt-retry admission, model-call diagnostics, idle-timeout accounting, assistant output, transcript persistence, degradation cooldown, and SSE fallback. Speculative failure closes only the candidate socket; the real request keeps the existing pre-dispatch fallback behavior.

Production LOC: +198/-11 (net +187) | Tests: +177/-2 (net +175) | Docs: +18/-8 (net +10)

User Impact

No behavior should ship from this branch. The prewarm wire contract worked, but operator-visible latency regressed materially. Keep direct API-key Responses on SSE by default and keep explicit cached WebSocket continuation from #122483 without adding this prewarm round trip.

A future attempt would need either a real idle session-start window before the operator submits a prompt or a server-side warmup whose completion cost is close to zero. Merely moving this request earlier inside the active turn is not sufficient.

Evidence

Authenticated live OpenAI A/B on this exact branch, gpt-5.6-luna, 12 alternating samples per lane, 12,162-character stable system prefix:

Lane First provider event median First text median Total median Reuse / continuation Fallbacks
SSE 320 ms 580 ms 703 ms n/a 0/12
Cold cached WebSocket 417 ms 835 ms 923 ms 0/12 / 0/12 0/12
Warm cached WebSocket 114 ms 896 ms 973 ms 12/12 / 12/12 0/12
Preconnected + generate=false prewarm 550 ms 1,062 ms 1,162 ms 12/12 / 12/12 0/12

The prewarm request was accepted and continuation succeeded on every sample, so this is not a correctness or fallback failure. It is a latency failure: median first text was about 83% slower than SSE and 27% slower than a cold cached WebSocket request.

Focused regression proof:

  • node scripts/run-vitest.mjs packages/ai/src/transports/openai-responses-websocket.test.ts packages/ai/src/transports/openai-responses-websocket-client.test.ts src/agents/embedded-agent-runner/provider-prompt-state.test.ts src/agents/embedded-agent-runner/run/attempt.model-diagnostic-events.test.ts src/agents/embedded-agent-runner/run/llm-idle-timeout.test.ts
  • Result: 5 files, 172 tests passed across 3 Vitest shards.
  • git diff --check: clean.
  • Targeted oxfmt and oxlint: clean.
  • Codex autoreview (gpt-5.6-sol, high): TruffleHog clean; no accepted/actionable findings; patch correctness 0.98. The review agreed the measured regression argues against retaining the experiment.

Dependency and sibling-contract inspection:

  • codex-rs/core/src/client.rs: session-owned WebSocket cache, handshake-only preconnect, v2 beta header, generate=false, completion drain, and response-ID continuation.
  • codex-rs/codex-api/src/endpoint/responses_websocket.rs: exclusive socket request lifecycle and discard-on-terminal-error behavior.
  • OpenAI SDK 6.49.0 ResponsesWS: constructor-time connect, connecting-state send queue, auth headers, and async iterator lifecycle.

AI-assisted: yes. The implementation, upstream contract review, focused proof, authenticated live A/B, and final decision were reviewed by the maintainer.

@clawsweeper

clawsweeper Bot commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

@openclaw-barnacle openclaw-barnacle Bot added docs Improvements or additions to documentation agents Agent runtime and tooling labels Aug 12, 2026
@steipete

Copy link
Copy Markdown
Contributor Author

Closing this experiment rather than leaving a merge candidate that the operator-visible evidence does not support.

The transport contract worked: all 12 prewarmed samples reused one socket, continued with previous_response_id, and completed without fallback or warning. The latency result was still materially worse:

  • SSE median first text: 580 ms
  • Cold cached WebSocket: 835 ms
  • Preconnected + generate=false prewarm: 1,062 ms

That is about 83% slower than SSE and 27% slower than the cold cached WebSocket path. The extra provider round trip costs more than the warm response state saves when OpenClaw starts it inside the active operator turn.

Supported alternative: retain the direct API-key SSE default and the explicit cached-WebSocket continuation repair from #122483, without this prewarm request.

Evidence that would change the decision: a real session-start window that completes warmup before prompt submission, or a server-side warmup path whose completion cost is near zero, followed by a fresh alternating A/B showing prewarmed first-text latency no longer materially worse than SSE.

@steipete

Copy link
Copy Markdown
Contributor Author

Follow-up research and live testing reinforce the original close decision while clarifying where WebSockets do help.

OpenAI's official WebSocket guide documents generate:false as warming known request state for an upcoming turn. Its published performance claim is for long workflows with at least 20 tool calls, not isolated first-turn TTFT. Codex likewise starts prewarm during session startup and records its age when the first turn arrives; it has separate timeout/cancellation handling for stalled warmup.

Additional authenticated results on gpt-5.6-luna:

Fully hidden 1-second lead

  • Small request, 15 samples: SSE median first text 599 ms; handshake-only preconnect 558 ms; generate:false 570 ms. Preconnect gained only 42 ms / 6.9% on text, though first event improved from 316 ms to 118 ms.
  • 7.4k-token + 40-tool request, 5 samples: SSE first text 894 ms; preconnect 942 ms; generate:false 1,033 ms. Both WebSocket variants remained slower on operator-visible text.
  • Warmup had significant tail risk in the small-request run: p90 connection 3.0 s and p90 phase-to-text 4.3 s.

20 sequential tool calls

Four alternating workflows per lane, 21 generated Responses calls each:

  • SSE full history: median 16.264 s
  • Plain WebSocket continuation: median 14.183 s (12.8% faster than SSE)
  • WebSocket + generate:false: median 15.747 s (11.0% slower than plain WebSocket)

Prewarm also used 30,954 input tokens versus 29,820 for plain WebSocket (+3.8%).

Updated verdict:

  • Keep SSE as the direct API-key default.
  • Keep explicit cached WebSocket continuation from fix(ai): continue cached OpenAI WebSocket turns #122483; it is valuable for long tool-heavy sessions.
  • Do not ship generate:false during the active turn.
  • Do not ship handshake-only preconnect yet: the text benefit is small or absent and does not justify reintroducing startup/fallback risk.
  • Reconsider only with a genuine session-start lifecycle before prompt submission, bounded connect timeout/cancellation, and product-path proof of a material first-text win.

Official references:

@RomneyDa
RomneyDa deleted the steipete/cranky-mendel-47d82b branch October 22, 2026 00:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling docs Improvements or additions to documentation maintainer Maintainer-authored PR size: M

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant