Franklin icon indicating copy to clipboard operation
Franklin copied to clipboard

Stream-idle timeout (90s) is shorter than documented 60-120s first-token latency → reasoning models time out before first token

Open Zambala108 opened this issue 6 months ago • 1 comments

Stream-idle timeout (90s) is shorter than the documented 60–120s first-token latency → reasoning models time out before the first token

Summary

getModelStreamIdleTimeoutMs() defaults to 90,000 ms and wraps every reader.read() in parseSSEStream, including the wait for the first content chunk. Because the gateway flushes SSE response headers before the first token, the fetch() call (guarded by the 180 s request timeout) resolves almost immediately, so time-to-first-token is actually governed by the 90 s stream-idle timeout, not the 180 s request timeout.

Reasoning / extended-thinking models — which the code's own comment notes take 60–120 s to first token on cache-cold prompts — therefore abort at 90 s before producing anything.

Environment

  • Package: @blockrun/franklin
  • Version: 3.23.0
  • Location: getModelStreamIdleTimeoutMs() / parseSSEStream() in agent/llm.js (compiled; source likely src/agent/llm.ts)
  • Model observed: anthropic/claude-opus-4.8
  • Severity: High for reasoning-model + long-generation workloads (e.g. synthesizing a long document from accumulated context)

Error

Retrying 1/1 on anthropic/claude-opus-4.8 — Timeout: Model stream timed out after 90000ms on anthropic/claude-opus-4.8

Request failed
• Type: Timeout
• Message: Model stream timed out after 90000ms on anthropic/claude-opus-4.8
• Tip: Check your network connection. Use /retry to try again.

The retry replays the same prompt and hits the same 90,000 ms wall; the agent then loops on the same planning turn without ever emitting output.

Root cause

Two separate timeouts are in play:

Function Default Wraps Governs
getModelRequestTimeoutMs() 180_000 fetch(endpoint, …) time-to-headers
getModelStreamIdleTimeoutMs() 90_000 each reader.read() in parseSSEStream gap between SSE chunks, including the first content chunk

The 180 s request timeout was raised specifically to cover the documented 60–120 s first-token latency. Its own comment states the assumption it relies on:

180s budget for time-to-headers (the gateway flushes SSE headers only once the upstream model emits its first token).

That assumption does not hold. The failure surfaces as a stream timeout (Model stream timed out), not a request timeout — i.e. headers arrived quickly and the long wait happened on the first reader.read(). So the gateway flushes headers before the first token, which means:

  • The 180 s request timeout passes immediately (headers are already in).
  • The wait for the first token falls under the 90 s stream-idle timeout.
  • Since 90 s < 120 s, any first-token latency in the 90–120 s band — routine for thinking models on large / cache-cold prompts — fails deterministically.

The mitigation the author intended (raise the budget to 180 s for slow first tokens) therefore never takes effect, because the timeout that actually guards the first token is still 90 s.

Reproduction

  1. Use a reasoning / extended-thinking model (anthropic/claude-opus-4.8) with a large, cache-cold prompt that pushes first-token latency above 90 s — e.g. ask it to synthesize a long document from a large accumulated context.
  2. The request fails with Model stream timed out after 90000ms.
  3. /retry replays the same prompt and fails identically.

This is threshold-dependent rather than universal: it reproduces whenever first-token latency (or any inter-chunk gap) exceeds 90 s, which is common for thinking models on big / cold prompts and uncommon for short, fast generations.

Proposed fix

Preferred — separate "time to first chunk" from "inter-chunk idle" in parseSSEStream: give the first reader.read() the request-timeout budget (the real first-token budget), and apply the smaller idle timeout only to subsequent reads. That keeps a tight guard against genuinely stalled mid-streams while not cutting off slow first tokens.

- // every read uses the 90s stream-idle timeout, incl. the wait for the first token
- const { done, value } = await withAbortableTimeout(
-     () => reader.read(), controller,
-     createModelTimeoutError('stream', model, timeoutMs), timeoutMs);
+ // first read = time-to-first-token (use the larger request budget);
+ // later reads = inter-chunk idle (use the smaller stream-idle budget)
+ const budget = firstRead ? requestTimeoutMs : timeoutMs;
+ const { done, value } = await withAbortableTimeout(
+     () => reader.read(), controller,
+     createModelTimeoutError('stream', model, budget), budget);
+ firstRead = false;

Simpler fallback — make the stream-idle default at least the documented first-token latency and align it with the request timeout: raise getModelStreamIdleTimeoutMs()'s default from 90_000 to ≥ 180_000. (The env overrides FRANKLIN_MODEL_STREAM_IDLE_TIMEOUT_MS / FRANKLIN_MODEL_IDLE_TIMEOUT_MS already allow this per-user, but the shipped default is the problem.)

Suggested regression test

Mock an SSE response whose first chunk arrives after ~100 s (headers flushed immediately). Assert the stream is not aborted at 90 s — i.e. the first-token wait honors the request/first-token budget, while a >idle-timeout gap between later chunks still aborts.

Zambala108 avatar Jun 01 '26 03:06 Zambala108

Follow-up / clarification on the "preferred" structural fix above.

Giving only the first reader.read() the request-timeout budget and keeping the smaller idle timeout for subsequent reads is correct for the common shape — slow first token, then a steady, continuous stream — but it leans on an assumption that is weaker for adaptive-thinking models (e.g. Opus 4.7+/4.8): that once the model starts emitting, gaps between chunks stay small.

Adaptive-thinking models can go silent again mid-stream to think between segments (before a tool call, between reasoning steps, etc.). When that happens a later reader.read() also exceeds the idle window, so keeping subsequent reads at 90s can still abort a legitimate mid-stream think — the same Model stream timed out after 90000ms just surfaces mid-stream instead of at the first token.

Implications:

  • First-read-only widening is necessary but not sufficient for adaptive-thinking models.
  • The subsequent-read idle timeout also needs to be larger than 90s (or model-aware), not just the first read.
  • The simpler fallback — raising the single getModelStreamIdleTimeoutMs() default to >= 180_000 — actually covers mid-stream silence better than the first-read-only change, because it applies to every read.

Suggested combined approach:

  • First read (time-to-first-token): use the request-timeout budget.
  • Subsequent reads (inter-chunk idle): keep a guard against genuinely stalled streams, but set it generously for thinking models (e.g. >= 120-180s, ideally configurable, with a higher default for known adaptive-thinking models) rather than 90s.

Net: the first/subsequent split is a good shape, but the subsequent-read budget must be raised above 90s for adaptive-thinking models — otherwise the timeout just moves from "before the first token" to "mid-stream" without being fixed.

Zambala108 avatar Jun 01 '26 06:06 Zambala108