Skip to content

fix: duplicate litellm stream call doubles provider requests - #477

Open
quevedoSteven wants to merge 4 commits into
aliasrobotics:mainfrom
quevedoSteven:fix/duplicate-litellm-stream-request-v2
Open

fix: duplicate litellm stream call doubles provider requests#477
quevedoSteven wants to merge 4 commits into
aliasrobotics:mainfrom
quevedoSteven:fix/duplicate-litellm-stream-request-v2

Conversation

@quevedoSteven

Copy link
Copy Markdown

Summary

The streaming path in \ etch_response_litellm_openai\ called \litellm.acompletion\ twice with the same kwargs. The first result was assigned to a variable that is never read \u2014 only the second call's stream was returned.

Impact

Every streamed request consumed two provider requests (e.g. two NVIDIA NIM requests) with the same API key:

  • Halves the effective per-key rate limit (a 40 req/min NIM key only supports 20 streamed turns/min).
  • With multi-key round-robin rotation, both calls share the same rotated key, so rotation spreads turns \u2014 not requests \u2014 and a busy session (several concurrent agents) quickly exhausts both keys \u2192 429 \u2192 \LLMRateLimited: Rate limit after 3 attempts.
  • The first call's stream is abandoned, leaking an unconsumed stream on every request.

Change

Keep a single \litellm.acompletion\ call for the streamed path, and the same fix in the \ ool_call_id\ truncation retry branch (which had the identical double-call pattern).

Verification

  • \stream=True: one \�completion\ call; the returned stream is the only request sent.
  • \stream=False: unchanged.
  • Syntax checked (\�st.parse); no references to the removed
    et\ variable remain.

Dependency

This fix requires the NIM key rotation from #475 (round-robin \NVIDIA_NIM_API_KEY_1..N) to provide \custom_llm_provider\ and \�pi_base\ for NIM models. This PR is based on that branch so the full fix works end-to-end.

quevedoSteven and others added 2 commits July 31, 2026 15:51
Adds cai.util.nim_rotation which cycles NVIDIA_NIM_API_KEY_1..N per
request, keeping under NIM per-key rate limits (~40 req/min). Rotation
applies in the OpenAI-compatible LLM path and the direct httpx
completion path when the API base is a NIM endpoint (api.nvidia.com).

Also defaults cache_write_tokens to 0 in the streamed usage object:
litellm CustomResponseUsage now requires the field while NIM usage
payloads omit it, which previously aborted streaming at end of response.
…penai

The streaming path called litellm.acompletion twice with the same kwargs.
The first result was assigned to a variable that is never read, and only
the second call's stream was returned. Every streamed request therefore
consumed two provider requests (e.g. two NVIDIA NIM requests) with the
same API key, halving effective per-key rate limits and leaking the
first stream. Keep a single acompletion call for the streamed path; same
fix in the tool_call_id truncation retry branch.
The NIM key rotation was incorrectly applying to all models when
NVIDIA_NIM_API_KEY_N are configured. This broke non-NIM models like
z-ai/glm-5.2 which should route through the Alias gateway, not
NVIDIA NIM. Add _is_nim_model() check with known NIM provider
prefixes to gate rotation. Fix applies to both LiteLLM path and
direct httpx path.
z-ai/glm-5.2 is hosted on NVIDIA NIM (build.nvidia.com/z-ai/glm-5.2).
The NIM rotation was incorrectly excluding it. Add "z-ai" to the
known NIM provider list in both LiteLLM and direct httpx paths.
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.

2 participants