fix(anthropic): raise default httpx read timeout for streaming; add configurable timeout param#5529
Open
SuperMarioYL wants to merge 1 commit intolivekit:mainfrom
Open
Conversation
…d timeout param The Anthropic plugin was using a flat `httpx.AsyncClient(timeout=5.0)` which sets all sub-timeouts — including the per-chunk read timeout for streaming SSE responses — to 5 seconds. Claude's adaptive-thinking phases and large-context requests can produce 10-30 s silences between streamed chunks; the 5 s read timeout fires during those pauses, raising `APIConnectionError` and killing voice sessions mid-turn. Changes: - Default httpx timeout is now `httpx.Timeout(5.0, read=30.0)`: connect stays tight at 5 s (genuine TCP failures surface quickly) while the per-chunk read window is 30 s (covers standard thinking budgets with headroom). - New `timeout: httpx.Timeout | None = None` constructor parameter lets callers supply a fully-customised timeout without having to build a whole `anthropic.AsyncClient` (e.g. `httpx.Timeout(5.0, read=60.0)` for extended thinking or very large contexts). Matches the pattern already used by the OpenAI plugin. - Six unit tests in `tests/test_plugin_anthropic.py` verify the default split, the tight connect value, custom timeout pass-through, and that a caller- supplied `client=` wins over `timeout=`. Fixes livekit#5508.
|
|
longcw
approved these changes
Apr 24, 2026
Contributor
longcw
left a comment
There was a problem hiding this comment.
looks good to me! something nit:
| parallel_tool_calls: NotGivenOr[bool] = NOT_GIVEN, | ||
| tool_choice: NotGivenOr[ToolChoice] = NOT_GIVEN, | ||
| caching: NotGivenOr[Literal["ephemeral"]] = NOT_GIVEN, | ||
| timeout: httpx.Timeout | None = None, |
Contributor
There was a problem hiding this comment.
maybe use NOT_GIVEN
Suggested change
| timeout: httpx.Timeout | None = None, | |
| timeout: NotGivenOr[httpx.Timeout ] = NOT_GIVEN |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #5508.
The Anthropic LLM plugin used
httpx.AsyncClient(timeout=5.0), which sets all httpx sub-timeouts — including the per-chunk SSE read timeout — to 5 seconds. Claude's adaptive-thinking phases routinely pause for 10–30 s before emitting the first content chunk, so the 5 s read timeout fires during normal usage and raisesAPIConnectionError, killing voice sessions mid-turn.Changes
httpx.Timeout(5.0, read=30.0)— connect stays at 5 s (genuine TCP failures surface fast) while the per-chunk read window is 30 s (covers standard thinking budgets with headroom).timeoutconstructor parameter:timeout: httpx.Timeout | None = Nonelets callers supply a custom timeout without constructing a wholeanthropic.AsyncClient— e.g.httpx.Timeout(5.0, read=60.0)for extended thinking or very large contexts. Aligns with the pattern already used by the OpenAI plugin.tests/test_plugin_anthropic.pycover the default split, tight connect value, custom timeout pass-through, andclient=precedence overtimeout=.Before / after
Usage (new parameter)
Test plan
uv run pytest tests/test_plugin_anthropic.py -v— 6/6 passuv run ruff check— no errorsuv run ruff format --check— no changes needed