Repository navigation
perf: pre-warm upstream realtime connection pool to cut session-establishment latency #31163
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
24 commits merged into
litellm_internal_staging
from
litellm_rust_gateway_realtime_pool
Jun 24, 2026
Merged
Changes from 20 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
8ab1346
docs: realtime pre-warmed connection pool design + raw-passthrough foβ¦
ishaan-jaff 80c341d
docs: realtime pool benchmark, repro steps, and deploy guidance
ishaan-jaff 51e9496
feat: expose Router::deployments() for host-side upstream enumeration
ishaan-jaff 030516f
refactor: split realtime dial/splice and add warm-handoff entry point
ishaan-jaff bc917b7
feat: pre-warmed upstream realtime connection pool with fresh-dial faβ¦
ishaan-jaff c82aae6
feat: add realtime pool handle to gateway AppState
ishaan-jaff 1fabaaf
feat: try warm pooled upstream before fresh-dial in realtime service
ishaan-jaff 9392d0c
feat: thread realtime pool through the realtime route bridge
ishaan-jaff 5ee042c
feat: build and pre-warm the realtime pool at gateway startup
ishaan-jaff 25e68dd
build: lean Dockerfile for the realtime gateway (default features, enβ¦
ishaanj-berri 4fa5c1b
docs: add generic ai-gateway benchmarking skill
ishaanj-berri 6210975
docs: drop per-endpoint realtime benchmark README
ishaanj-berri ffc661e
test: add sanitized realtime WS load-gen harness for gateway benchmarks
ishaanj-berri 01ee3cf
test: add hosted-runner serve.sh wrapper for the realtime harness
ishaanj-berri a42c0be
test: make serve.sh publish result atomically and clear stale output
ishaanj-berri 08d5a4c
perf: refill the realtime pool concurrently so warm supply keeps up aβ¦
ishaanj-berri 4882202
build: drop Dockerfile.lean
ishaanj-berri f01eeb7
docs: slim benchmarks/realtime to a README (harness moved to its own β¦
ishaanj-berri e128f45
docs: add realtime route README with pooling design + diagram
ishaanj-berri e1f3fb6
fix: satisfy clippy manual_flatten on concurrent pool refill
ishaanj-berri 1ad035b
Update litellm-rust/crates/providers/src/realtime_pool.rs
ed952e5
Update litellm-rust/crates/providers/src/realtime_pool.rs
6d59860
fix: back off the realtime pool replenisher for failing keys
ishaanj-berri 2988237
docs: document the replenisher backoff in the realtime route README
ishaanj-berri File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
litellm-rust/crates/ai-gateway/benchmarks/realtime/README.md
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| # Realtime gateway benchmark β pool on/off | ||
|
|
||
| Measures what the gateway adds over talking to OpenAI's realtime WebSocket | ||
| directly, and what the pre-warmed connection pool removes. See | ||
| `../../src/routes/realtime/README.md` for how the pool works. | ||
|
|
||
| ## Results | ||
|
|
||
| 5000 calls / 500 concurrency, gateway at 10 instances, pool ON | ||
| (`REALTIME_POOL_SIZE=64`), upstream OpenAI `gpt-realtime`. Each leg run twice. | ||
| Times in **ms**. Phases per connection: **dial** = TCP+TLS+WS upgrade, | ||
| **session** = upgrade β `session.created` (the phase the pool removes), | ||
| **1st-audio** = `response.create` β first audio delta (OpenAI inference), | ||
| **total** = full wall-clock. | ||
|
|
||
| | metric | Direct OpenAI | Gateway (pool ON) | Overhead (ms) | vs OpenAI | | ||
| | ------------------ | ------------- | ----------------- | ------------- | ---------- | | ||
| | success rate (%) | 99.8 | 99.8 | β | β | | ||
| | dial p50 (ms) | 276 | 158 | β118 | **faster** | | ||
| | session p50 (ms) | 7 | 0 | β7 | **faster** | | ||
| | 1st-audio p50 (ms) | 440 | 664 | +224 | slowerΒΉ | | ||
| | total p50 (ms) | 816 | 1010 | +194 | slowerΒΉ | | ||
| | total p95 (ms) | 2152 | 1970 | β182 | **faster** | | ||
| | total p99 (ms) | 2692 | 2610 | β82 | **faster** | | ||
|
|
||
| The gateway is **faster than direct on 4 of 6 metrics**. The warm pool makes the | ||
| **session phase sub-millisecond** at the median β ~76% of connects hit the pool, | ||
| ~70% had session < 1 ms. ΒΉ The two "slower" rows are not gateway overhead: | ||
| `1st-audio` is OpenAI's own inference time (the gateway only relays it), which ran | ||
| slower during the gateway legs and drags `total p50` with it. | ||
|
|
||
| **Pool OFF** (control, `REALTIME_POOL_SIZE=0`): session p50 was **367 ms** β the | ||
| fresh-dial overhead the pool removes. | ||
|
|
||
| ## Reproduce | ||
|
|
||
| The load generator lives in a separate repo: | ||
| **https://github.com/ishaan-berri/litellm-realtime-bench** | ||
|
|
||
| ```bash | ||
| git clone https://github.com/ishaan-berri/litellm-realtime-bench | ||
| cd litellm-realtime-bench && go build -o wsbench . | ||
|
|
||
| # Direct to OpenAI (baseline) | ||
| ./wsbench -host api.openai.com -key "$OPENAI_API_KEY" -m gpt-realtime -n 5000 -c 500 -t 60 | ||
|
|
||
| # Through the gateway β run once with pool ON, once with REALTIME_POOL_SIZE=0 | ||
| ./wsbench -host <gateway-host> -key "$LITELLM_MASTER_KEY" -m gpt-realtime -n 5000 -c 500 -t 60 | ||
| ``` | ||
|
|
||
| Run the gateway with the env stand-in (`OPENAI_REALTIME_MODEL=gpt-realtime`, | ||
| `OPENAI_API_KEY`, `LITELLM_MASTER_KEY`, `REALTIME_POOL_SIZE`, `HOST=0.0.0.0`). At | ||
| 500 concurrency over N instances, size the pool to `β 500 / N` per instance (64 was | ||
| used here for 10 instances). The bench repo's README covers running 500-concurrency | ||
| legs from a hosted multi-vCPU runner. **Never commit keys β pass them via `-key`.** |
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
82 changes: 82 additions & 0 deletions
82
litellm-rust/crates/ai-gateway/src/routes/realtime/README.md
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| # Realtime route (`GET /v1/realtime`) | ||
|
|
||
| Proxies OpenAI's realtime WebSocket. `mod.rs` is the axum surface (handler + | ||
| socketβevents adapter); `service.rs` is the pure logic (select a deployment, then | ||
| splice client β upstream). The pool itself lives in | ||
| `crates/providers/src/realtime_pool.rs`. | ||
|
|
||
| ## Connection pooling | ||
|
|
||
| ### The problem | ||
|
|
||
| The gateway's realtime overhead lives **entirely in session establishment**. On each | ||
| client connect it dials a *fresh* upstream WS to OpenAI and waits for | ||
| `session.created` before it can serve. Measured at 5000 calls / 500 concurrency, the | ||
| fresh-dial session phase is **~360 ms** vs **~7 ms** direct; dial, first-audio, and | ||
| streaming add ~0. So the one lever is removing that per-connect handshake from the | ||
| critical path. | ||
|
|
||
| ### The idea | ||
|
|
||
| Keep a few upstream OpenAI sockets **already connected and already past | ||
| `session.created`** (buffered). On a client connect, hand off a warm socket β relay | ||
| its buffered `session.created` instantly (a local `Vec::pop`, sub-millisecond) and | ||
| splice exactly as a fresh dial would. A background task keeps the pool topped up. On | ||
| a miss or dead socket we fall back to fresh-dial: the pool is a latency optimization, | ||
| never a correctness dependency. | ||
|
|
||
| ``` | ||
| βββββββββββββββββββββββββββββββββββββββββ | ||
| client connect βββββββΊ β routes/realtime β service::run β | ||
| β pool.take(key) β | ||
| β hit β relay buffered β | ||
| β session.created, then splice β | ||
| β miss β fresh dial (original path) β | ||
| βββββββββββββββββ¬ββββββββββββββββββββββββ | ||
| β replenish (async, concurrent) | ||
| βββββββββββββββββΌββββββββββββββββββββββββ | ||
| background task ββββββΊ β RealtimePool: per-key warm sockets β | ||
| β each = { ws, buffered session.created}β | ||
| β liveness-checked before handoff β | ||
| βββββββββββββββββββββββββββββββββββββββββββ | ||
| ``` | ||
|
|
||
| A warm session is indistinguishable from a fresh one: OpenAI sends `session.created` | ||
| unprompted on connect, we pre-read exactly that one frame and relay it on handoff, | ||
| and we send nothing else on the socket before a client exists β so the client's first | ||
| `session.update` behaves identically either way. | ||
|
|
||
| ### Sizing | ||
|
|
||
| Each warm socket serves **exactly one** session (realtime isn't multiplexed), so the | ||
| pool is sized to the **peak concurrent connects per instance**, not total live | ||
| connections: | ||
|
|
||
| ``` | ||
| REALTIME_POOL_SIZE β peak_concurrency / instance_count | ||
| ``` | ||
|
|
||
| e.g. 500 concurrency over 10 instances β ~50β64 per instance. The replenisher dials | ||
| the missing sockets **concurrently**, so a drained pool refills in ~one handshake | ||
| window and keeps supply close to the connect rate. Over-provisioning just burns idle | ||
| upstream sockets, which is why warm sockets are short-lived | ||
| (`REALTIME_POOL_MAX_IDLE_SECS`). | ||
|
|
||
| ### Config | ||
|
|
||
| | env | default | meaning | | ||
| | ----------------------------- | ------- | --------------------------------------------------------------- | | ||
| | `REALTIME_POOL_SIZE` | `4` | target warm sockets per key. `0` disables pooling (fresh-dial). | | ||
| | `REALTIME_POOL_MAX_IDLE_SECS` | `30` | max time a warm socket sits before it's closed and replaced. | | ||
|
|
||
| ### Notes | ||
|
|
||
| - **Miss / dead socket β fresh dial.** Burst beyond warm supply, or a socket that | ||
| died, never blocks or fails β it falls back to the original path. The pool can only | ||
| make a connect faster, never slower or more fragile. | ||
| - **Auth scope.** The pool key includes `api_key`, so a warm socket is only handed to | ||
| a request resolving to the same key β no cross-tenant reuse. | ||
| - **Idle billing.** Warm sockets are liveness-checked at handoff and capped at | ||
| `REALTIME_POOL_MAX_IDLE_SECS` to bound idle billing and dodge OpenAI's idle timeout. | ||
|
|
||
| Benchmarks and repro: `../../benchmarks/realtime/README.md`. | ||
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,3 +2,4 @@ pub mod mistral; | |
| pub mod ocr; | ||
| pub mod openai; | ||
| pub mod realtime; | ||
| pub mod realtime_pool; | ||
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.