fix(ratelimit): enforce TPM/TPD + concurrency on streamed /v1/messages and /v1/responses - #690
Conversation
…s and /v1/responses Streamed /v1/messages and /v1/responses reserved the rate-limit layers but, when usage_handled_by_stream was set, dropped the reservation at handler return without ever committing the terminal token cost. So their TPM/TPD counters never moved and the concurrency permit was released when the stream STARTED flowing rather than when it ended — a caller could exceed token-rate limits and run more than N concurrent streams by streaming (the streaming analog of #450). Thread the reservation into the per-attempt dispatch as `&mut Option`: the winning streaming attempt take()s it, derives the layer keys, and converts it into a StreamConcurrencyGuard moved into the end-of-stream guard. There the terminal usage is applied via limiter.add_tokens_post_stream (the sync analog of the async commit_tokens this closure can't await) and the hold drops at stream end. Non-streaming / failed attempts leave the reservation in place to commit or retry. Mirrors the chat.rs streaming path; matches LiteLLM's post-completion TPM accounting (parallel_request_limiter async_log_success_event). E2E: with TPM=10 and a stream reporting 16 tokens, the first streamed call commits 16 and the next is 429, for both /v1/messages (cross-provider bridge) and /v1/responses (verbatim passthrough). Fails before the fix (next call stays 200), passes after. Fixes #688
|
Warning Review limit reachedYou’ve reached a temporary PR review limit under our Fair Usage Limits Policy. Next review available in: 24 seconds Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available. How can I continue?After more reviews become available, a review can be triggered using the To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews. How do review limits work?CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability. For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window. Please refer docs for additional details. Review details⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (3)
✨ Finishing Touches🧪 Generate unit tests (beta)
Comment |
Streamed
/v1/messagesand/v1/responsesreserved the rate-limit layers but, whenusage_handled_by_streamwas set, dropped the reservation at handler return without ever committing the terminal token cost. Two consequences:commit_tokensnever ran — the token-rate counter never moved. A caller could exceed TPM/TPD by streaming through these two endpoints.dispatch()returned (the streaming body outlives it), a key capped at N concurrent could run more than N simultaneous streams — the streaming analog of the/v1/chat/completionsfix in security: make quota, streaming, and telemetry accounting consistent across request modes #450.RPM (counted at
pre_commit) and the$budget (via the cp-api ledger fed by the post-stream UsageEvent) were unaffected, so the residual gap was token-rate + concurrent-stream count.Fix
Thread the reservation into the per-attempt dispatch as
&mut Option<MultiReservation>. The winning streaming attempttake()s it, derives the layer keys, and converts it into aStreamConcurrencyGuardmoved into the end-of-stream guard. There:limiter.add_tokens_post_stream(key, total)— the sync analog of the reservation's asynccommit_tokens, which the end-of-stream closure can't await;Non-streaming and failed attempts leave the reservation in place for the handler to commit or a retry to reuse. This mirrors the existing chat.rs streaming path (
reservation.keys()+into_stream_hold()+add_tokens_post_stream), extending it to the two endpoints that were still on the pre-#450 behavior. It also matches LiteLLM's post-completion TPM accounting (parallel_request_limiterincrementscurrent_tpmfrom the response usage inasync_log_success_event, streaming included).Covers all four streaming dispatch closures:
/v1/messages(Anthropic verbatim passthrough + cross-provider bridge) and/v1/responses(verbatim passthrough + cross-provider bridge).Behavior / compatibility
/v1/chat/completions. A tenant relying on the old gap to stream past a token-rate or concurrency cap will now be limited.Tests
New DP E2E
streaming-tpm-commit-e2e.test.ts: with TPM=10 and an upstream reporting 16 tokens per stream, the first streamed call commits 16 and the next call is rejected 429, for both/v1/messages(cross-provider bridge) and/v1/responses(verbatim passthrough). Fails before the fix (the next call stays 200), passes after.Fixes #688