Skip to content

feat(fallback): first_token_timeout fails over a stalled stream early - #66

Merged
kaiser-data merged 3 commits into
mainfrom
feat/first-token-timeout
Dec 1, 2026
Merged

kaiser-data merged 3 commits into
mainfrom
feat/first-token-timeout

Conversation

@kaiser-data

Copy link
Copy Markdown
Owner

Problem

A provider that accepts a streaming request and then never sends a token held the request for the full provider timeout (90s by default) before failover. Both external reviews still held this against the router.

Change

  • New fallback.first_token_timeout. For streaming calls shorter than the provider timeout, a second timer starts. Content or reasoning chunks reset it. When it fires, the call is cancelled and the router moves to the next provider.
  • Off by default in code, so existing configs keep their behaviour. configs/config.example.yaml sets 10s.
  • peekStream takes a progress hook. Reasoning-only chunks (reasoning_content / reasoning) keep a thinking model alive, and role-only chunks do not.
  • Timeout errors say which timer fired (no first token within … vs provider timeout …).
  • FAILOVER_BENCH_FIRST_TOKEN knob for make bench-failover.
  • docs/first-token-timeout.md: when to use it, tradeoffs, value guide, and a real-board test plan (results still to come).

Measured (Mac, 200 req/row, 16 conc, provider timeout 2s)

Scenario before first_token_timeout: 500ms
stalled provider, cold p50 2028 ms 528 ms
healthy / 429 rows — unchanged

BenchmarkChatCompletions_Buffered stays at 151 allocs/op, the same as main.

Tests

pkg/proxy/first_token_test.go: StallFallsBackEarly, OffKeepsProviderTimeout, ReasoningKeepsAlive (fails without the progress hook, checked by mutation), BufferedUnaffected. go vet and go test -race ./... are green.

🤖 Generated with Claude Code

A provider that accepts a streaming request but never sends a token used
to hold the request for the full provider timeout before failover.
fallback.first_token_timeout arms a second timer for streaming calls that
is reset by content or reasoning chunks; when it fires, the router moves
to the next provider. Off by default in code; the example config sets 10s.

- peekStream gains a progress hook; reasoning-only chunks keep the timer alive
- timeout errors name which timer fired
- FAILOVER_BENCH_FIRST_TOKEN knob for make bench-failover
- docs/first-token-timeout.md with tradeoffs and a real-board test plan

Measured (Mac, 2s provider timeout, 500ms first-token): stalled-provider
cold p50 2028 -> 528 ms; healthy and 429 rows unchanged; buffered chat
benchmark stays at 151 allocs/op.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
kaiser-data and others added 2 commits December 1, 2026 17:30
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kaiser-data
kaiser-data merged commit 8ab2e3d into main Dec 1, 2026
2 checks passed
@kaiser-data
kaiser-data deleted the feat/first-token-timeout branch December 1, 2026 16:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant