Architecture

SSE Streaming Passthrough

bytes_stream().inspectis_sseupstreamstream_client (no total timeout)body_wants_streamproxy_handlerclientbytes_stream().inspectis_sseupstreamstream_client (no total timeout)body_wants_streamproxy_handlerclientalt[stream:true]alt[Ok(bytes)][Err]loop[each chunk (no buffering, no compression, no cache write)]alt[is SSE][non-SSE]POST /v1/chat/completions {"stream":true}1body_wants_stream(body)2select stream_client (connect_timeout only, no .timeout())3select bounded client (cli.timeout)4forward request5response headers (Content-Type)6is_sse(content_type) (prefix match text/event-stream)7response.bytes_stream().inspect(count)8chunk9response_body_bytes += len10sse_stream_errors += 111forward chunk (Body::from_stream)12200 X-Aphrodite-Streamed: true13accumulate_body → compress path (see 02-chat-compression)14
SSE DETECTION & PASSTHROUGH
SSE response detectedcompression SKIPPEDresponse_cache SKIPPEDchunks are partial JSON deltas - not wholemessage.content;can't classify/hash/marker a fragmentcache_key_from_body already returns None whenrequest stream:truetimeout-free stream_client - a slow-but-validstreammust not be killed mid-answermid-stream errors counted separately fromupstream_connect_errors/upstream_timeouts(pre-header only)
WHY COMPRESSION IS SKIPPED FOR SSE
IN THIS DOCUMENT

Crimson marks the heading currently in view; every entry is one of the document's own headings.

Server-sent-event (text/event-stream) responses bypass compression and the response cache entirely, stream chunk-by-chunk with a timeout-free client, and account bytes and errors mid-stream.

Detection happens twice: on the request ("stream": true selects the timeout-free client) and on the response (Content-Type selects the passthrough branch).

SSE detection & passthrough

bytes_stream().inspectis_sseupstreamstream_client (no total timeout)body_wants_streamproxy_handlerclientbytes_stream().inspectis_sseupstreamstream_client (no total timeout)body_wants_streamproxy_handlerclientalt[stream:true]alt[Ok(bytes)][Err]loop[each chunk (no buffering, no compression, no cache write)]alt[is SSE][non-SSE]POST /v1/chat/completions {"stream":true}1body_wants_stream(body)2select stream_client (connect_timeout only, no .timeout())3select bounded client (cli.timeout)4forward request5response headers (Content-Type)6is_sse(content_type) (prefix match text/event-stream)7response.bytes_stream().inspect(count)8chunk9response_body_bytes += len10sse_stream_errors += 111forward chunk (Body::from_stream)12200 X-Aphrodite-Streamed: true13accumulate_body → compress path (see 02-chat-compression)14
SSE DETECTION & PASSTHROUGH

Why compression is skipped for SSE

SSE response detectedcompression SKIPPEDresponse_cache SKIPPEDchunks are partial JSON deltas - not wholemessage.content;can't classify/hash/marker a fragmentcache_key_from_body already returns None whenrequest stream:truetimeout-free stream_client - a slow-but-validstreammust not be killed mid-answermid-stream errors counted separately fromupstream_connect_errors/upstream_timeouts(pre-header only)
WHY COMPRESSION IS SKIPPED FOR SSE

Byte accounting: only response_body_bytes accumulates during the stream (via the .inspect closure); tokens_saved is not touched because nothing was compressed.

sse_stream_errors surfaces in /stats and /metrics (aphrodite_sse_stream_errors_total).

Call sites

ConcernModule
body_wants_stream (request-side detection)crates/aphrodite/src/proxy.rs
stream_client construction (no total timeout)crates/aphrodite/src/proxy.rs
Client selection in handlercrates/aphrodite/src/proxy.rs
is_sse (response-side detection)crates/aphrodite/src/proxy.rs
SSE stream branch (bytes_stream().inspect, byte/error counting)crates/aphrodite/src/proxy.rs
sse_stream_errors in /metricscrates/aphrodite/src/main.rs