Repository navigation
feat(llm): add first_token_timeout to FallbackAdapter - #3080
rosetta-livekit-bot[bot] wants to merge 1 commit into
Conversation
🦋 Changeset detectedLatest commit: 35eefa5 The changes in this PR will be included in the next version bump. This PR includes changesets to release 42 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
There was a problem hiding this comment.
Devin Review found 2 potential issues.
1 flag not posted on this PR by your GitHub settings — view it in Devin Review. (Configure)
| } catch (error) { | ||
| if (this.abortController.signal.aborted) return; | ||
| throw error; |
There was a problem hiding this comment.
🔴 False recovery after fallback replies
When a fallback reply finishes, tryGenerate treats cancellation of a pending recovery probe as normal completion. The default LLM node closes the stream, so tryRecovery marks the timed-out provider available without seeing any output. The next turn tries that provider again.
Learn more
When a provider times out, the adapter starts a background request to check whether it recovered before moving on to the backup. tryRecovery treats normal exhaustion of that request's generator as success. The default LLM node closes the adapter stream after draining the backup's reply. If the probe is still waiting for its first output, that close rejects its wait, but the generator returns normally and the probe restores availability without a response.
Example: With a 300 ms deadline, the primary times out and its recovery probe starts. The backup finishes in 100 ms while the probe still has no output. Closing the reply cancels the probe and restores the primary, so another turn can repeat the 300 ms wait.
Recommended fix: Give the background probe a cancellation lifetime separate from the reply. A cancelled probe must not reach the success branch in tryRecovery. Add a test where the fallback reply is drained and closed while the probe still has no output, then verify that closing the reply does not restore the primary.
Was this helpful? React with 👍 or 👎 to provide feedback.
| } | ||
| throw error; | ||
| } finally { | ||
| stream.close(); |
There was a problem hiding this comment.
🟡 Provider generation continues after failover
On a first-token timeout, tryGenerate closes the local stream but leaves some provider requests running. Anthropic generation and OpenAI Responses HTTP generation never pass the abort signal to their SDKs. The original request can keep generating while the backup answers.
Learn more
The adapter's first-token timer can expire while the provider is still processing an otherwise live request. LLMStream.close only aborts the stream's controller. The Anthropic request and OpenAI Responses HTTP request neither pass that signal to the SDK nor stop consuming events when it fires. The adapter moves on without stopping the network request or its event-processing loop.
Example: An Anthropic request needs 900 ms before it starts emitting text. A 300 ms deadline moves the agent to a backup, but the Anthropic plugin still reads and processes the original response.
Recommended fix: Wire the stream abort signal into provider request options and treat local abort as cancellation rather than a provider error. Follow the signal and cancellation handling in inference generation and its error path. Test that a first-token timeout aborts the transport of the original request while the backup still responds.
Was this helpful? React with 👍 or 👎 to provide feedback.
Summary
firstTokenTimeoutdeadline toFallbackAdapter@livekit/agentschangesetPorts livekit/agents#8691.
Testing
pnpm test agents(Node 22: 5,125 passed, 10 skipped)pnpm --filter @livekit/agents buildpnpm --filter @livekit/agents lintpnpm --filter @livekit/agents typecheckpnpm --filter @livekit/agents api:checkpnpm exec prettier --check "agents/src/**/*.{ts,tsx,md,json}"Source diff coverage
livekit-agents/livekit/agents/llm/fallback_adapter.py: adapted toagents/src/llm/fallback_adapter.ts, with the public API reflected inagents/etc/agents.api.md. The Python seconds option becomes milliseconds per agents-js conventions; the absolute first-output deadline and cancellation use native JavaScript timers and stream cancellation.tests/test_llm_fallback.py: adapted toagents/src/llm/fallback_adapter.test.ts. Ports both source cases: keep-alive/usage-only chunks trigger failover, while text before later keep-alives disables the deadline.livekit-agents/pyproject.toml: not applicable. The source only adds the Python <3.11async-timeoutcompatibility dependency; Node provides native timer and cancellation primitives.uv.lock: not applicable. This is the lockfile result of the Python-only compatibility dependency and has no target counterpart.Ported from livekit/agents#8691
Original PR description
llm.FallbackAdapterfails over when an attempt errors, andattempt_timeoutis the only time limit on an attempt. The OpenAI-compatible plugins pass that value to httpx as a read timeout, so any bytes reset it: SSE keep-alive comments such as OpenRouter's: OPENROUTER PROCESSING, a role-only first delta, or reasoning deltas that_parse_choicedrops. A provider can hold an attempt open far pastattempt_timeoutwith noChatChunk, and the caller hears silence instead of the next provider.attempt_timeoutcannot carry this limit, because each plugin givesconn_options.timeoutits own meaning: a read timeout in the OpenAI-compatible plugins, a total deadline in Google.first_token_timeoutsets a deadline from the start of each attempt to its first chunk with text or a tool call. When the deadline expires, the attempt raisesasyncio.TimeoutErrorand the adapter fails over as it does for any other failed attempt.Why it is a separate argument that defaults to off:
attempt_timeoutstops healthy fallbacks. A cold fallback on a large prompt can take more than 5 s to its first token, and #6171 reports 10.8 s on a 19k-token turn. With the defaultNone, existing adapters keep their current behavior.ChatChunk.has_response(). After the first token, gaps in the stream have no limit.The value is in seconds, and it covers the internal retries of one attempt.
Fixes #6171. Alternative to #6173. Related to #6916.
Context for reviewing and coding agents