FastAPI
When FastAPI streams an AI reply straight from a StreamingResponse, a dropped signal or a page
reload cuts the stream in half. Put Cabby in front instead: FastAPI runs generation in
BackgroundTasks and posts each chunk over HTTP with httpx, while Cabby holds the Server-Sent
Events connection and keeps the stream's history in SQLite so clients can reconnect or
reload and pick up every missed chunk, without Redis.
1. Run Cabby
Install Cabby, then start it in standalone mode with SSE and the SQLite broker:
--norpcand--noauthrun Cabby standalone; signed stream names decide what each visitor can read.--sseenables the Server-Sent Events endpoint at/events.--broker=sqliteand--sqlite_path=./cabby.dbstore recent stream history in one local file. By default Cabby keeps the last 100 messages per stream for 5 minutes; adjust--history_limitand--history_ttlfor longer replies (Configuration).--allowed_originslimits browser connections to pages served by your FastAPI app. The value is the page's host and port, withouthttp://.
2. Add a small helper module
Install FastAPI, Uvicorn and httpx:
Add a cabby.py helper that signs stream names and broadcasts asynchronously. CABBY_SECRET and
CABBY_BROADCAST_KEY match --secret and --broadcast_key, and CABBY_BROADCAST_URL points at
Cabby's HTTP broadcast endpoint:
# cabby.py
=
=
=
=
=
=
return f
= await
3. Stream from a background task
In main.py, POST /runs starts generation in BackgroundTasks and returns the signed stream name
and start timestamp right away. Generation keeps running even if the visitor closes the tab or loses
connectivity:
# main.py
yield
await
=
:
# Replace this loop with your model SDK's async token stream.
= f
=
await
await
await
= .
= f
=
return
4. Subscribe and resume in the browser
Connect with the browser's built-in EventSource — no client package needed:
const params =
const events =
Both kinds of interruption recover automatically:
- Network drop:
EventSourcereconnects on its own and sends theLast-Event-IDheader that Cabby attached to each event. Cabby uses that position ahead ofhistory_sinceand replays only the chunks the browser missed. - Page reload or second device: opening a fresh
EventSourcewith the samesigned_streamandhistory_sincereplays every chunk from the start of the run, then switches to live tokens. raw=true: omits protocol events (welcome,confirm_subscription) and sends keepalives as SSE comments (: ping), soonmessageonly sees your JSON payloads.
5. Long streams, Durable Streams and WebSockets
- Long LLM replies: raise
--history_limitand--history_ttl, or group tokens into small chunks every 50–100 ms so a full reply stays well inside the history window. - Explicit cursors over HTTP: turn on Durable Streams with
--dsand--ds_skip_auth, then readGET /ds/runs/<run_id>?offset=-1&live=sse&signed=<signed_stream>to replay from the start and track eachstreamNextOffsetyourself. - WebSockets: the same
sign_streamandbroadcasthelpers also work with@anycable/webover WebSockets, as in the Django guide. - Multiple Cabby nodes: swap
--broker=sqlitefor Redis, PostgreSQL or NATS when you scale past one server.