Architecture

Chat-Completion Compression

smart_marker / cache_markerCcrStore / inline_ccr LRUthreshold_for + EMAproxy_detect_content_typecompress_chat_completionupstream LLM APIresponse_cache (LRU, 1MB cap)proxy_handlerOpenAI/Anthropic clientsmart_marker / cache_markerCcrStore / inline_ccr LRUthreshold_for + EMAproxy_detect_content_typecompress_chat_completionupstream LLM APIresponse_cache (LRU, 1MB cap)proxy_handlerOpenAI/Anthropic clientalt[exists]leave content untouchedalt[content.len() > threshold (COMPRESS)][content.len() > inline_ccr_threshold (INLINE only)][below all thresholds]loop[each choice.message.content (string)]tool_calls[].function.arguments never visited(the loop reads message.content only)alt[Some(compressed)][None]alt[is_chat_completion && state.ccr.is_some()][not chat / no CCR]alt[content-type is text/event-stream][non-streaming]alt[cache HIT][miss]POST /v1/chat/completions {messages,tools,...}1requests_total++ · is_chat_completion = path==/v1/chat/completions2cache_key_from_body(body, api_key) (skips if stream:true)3cached bytes4200 X-Aphrodite-Cache: HIT (early return)5forward (stream_client if body stream:true, else client) · ≤3 conn retries6response (headers + body)7passthrough stream (NO compression - see 06-sse-streaming)8accumulate_body (cap 64MB)9compress_chat_completion(state, body, x-headroom-budget)10base_threshold = compress_threshold() (mode dispatch)11budget_mult = clamp(0.50 + hdr/100*0.50, 0.50..1.0)12proxy_detect_content_type(content)13ct (tool_output|code_rust|error|diff|json|text|...)14threshold = threshold_for(ct).max(base) * budget_mult15hash = compute_key(bytes) - BLAKE3, 40 hex16ccr_get(hash)? (dedup)17hit → ccr_hits++18ccr_put(hash, content) → ccr_created++19Cache→cache_marker / Token→smart_marker20"preview\n[ct: meta]\n<<<CCR:hash|ct|size>>>"21update_compression_ratio(orig,marker) → EMA22replace content with marker · did_compress=true23inline_ccr.put(hash, content) (no marker swap)24Some(response) if did_compress else None25write (success + ≤1MB)26200 X-Aphrodite-Compressed: true, X-Aphrodite-Fill-Pct27raw body passthrough, X-Aphrodite-Cache: MISS28raw passthrough29
RESPONSE COMPRESSION SEQUENCE
update_compression_ratioyesno200elseerrorcode_*diff/git/texttool_output/jsonthreshold_for(ct)ct in linter/build_output/log?return base (no tune, no mult)ratio = compression_ratio_ema/100ratiotune = 2.0 (compressing well → raise bar)tune = 0.5 (weak → lower bar)tune = 1.0base' = base*tunefixed per-type multiplierbase'*8base' * code_multiplier (default 3.0)base'*2base'ratio = orig/comp * 100new = 0.2*ratio + 0.8*old (α=0.2)fill_pct = clamp(100 - ema/20, 1..99)*100
PER-TYPE THRESHOLD (EMA AUTO-TUNE → FIXED MULTIPLIER)
IN THIS DOCUMENT

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

The token and cache proxies’ central flow: a /v1/chat/completions response comes back from upstream, and compress_chat_completion classifies each message.content, applies an EMA-tuned per-type threshold (scaled by the x-headroom-budget header), BLAKE3-hashes over-threshold content, stores it, and replaces the text with a self-describing preview plus a <<<CCR:hash|type|size>>> marker.

tool_calls[].function.arguments is a sibling field the loop never visits, so it always passes through untouched.

The HTTP proxy path is a separate pipeline from the Hermes FFI hook path: it classifies via proxy_detect_content_type and builds previews via proxy_build_preview, and never calls the staged transform pipeline (transforms::detect, stage2::compress_stage2, struct_extract::extract_code_structure) - those run only on the Hermes hook path (see 04-hook-ffi.md).

The two pipelines share the BLAKE3 compute_key and the marker wire format, but nothing else.

Response compression sequence

smart_marker / cache_markerCcrStore / inline_ccr LRUthreshold_for + EMAproxy_detect_content_typecompress_chat_completionupstream LLM APIresponse_cache (LRU, 1MB cap)proxy_handlerOpenAI/Anthropic clientsmart_marker / cache_markerCcrStore / inline_ccr LRUthreshold_for + EMAproxy_detect_content_typecompress_chat_completionupstream LLM APIresponse_cache (LRU, 1MB cap)proxy_handlerOpenAI/Anthropic clientalt[exists]leave content untouchedalt[content.len() > threshold (COMPRESS)][content.len() > inline_ccr_threshold (INLINE only)][below all thresholds]loop[each choice.message.content (string)]tool_calls[].function.arguments never visited(the loop reads message.content only)alt[Some(compressed)][None]alt[is_chat_completion && state.ccr.is_some()][not chat / no CCR]alt[content-type is text/event-stream][non-streaming]alt[cache HIT][miss]POST /v1/chat/completions {messages,tools,...}1requests_total++ · is_chat_completion = path==/v1/chat/completions2cache_key_from_body(body, api_key) (skips if stream:true)3cached bytes4200 X-Aphrodite-Cache: HIT (early return)5forward (stream_client if body stream:true, else client) · ≤3 conn retries6response (headers + body)7passthrough stream (NO compression - see 06-sse-streaming)8accumulate_body (cap 64MB)9compress_chat_completion(state, body, x-headroom-budget)10base_threshold = compress_threshold() (mode dispatch)11budget_mult = clamp(0.50 + hdr/100*0.50, 0.50..1.0)12proxy_detect_content_type(content)13ct (tool_output|code_rust|error|diff|json|text|...)14threshold = threshold_for(ct).max(base) * budget_mult15hash = compute_key(bytes) - BLAKE3, 40 hex16ccr_get(hash)? (dedup)17hit → ccr_hits++18ccr_put(hash, content) → ccr_created++19Cache→cache_marker / Token→smart_marker20"preview\n[ct: meta]\n<<<CCR:hash|ct|size>>>"21update_compression_ratio(orig,marker) → EMA22replace content with marker · did_compress=true23inline_ccr.put(hash, content) (no marker swap)24Some(response) if did_compress else None25write (success + ≤1MB)26200 X-Aphrodite-Compressed: true, X-Aphrodite-Fill-Pct27raw body passthrough, X-Aphrodite-Cache: MISS28raw passthrough29
RESPONSE COMPRESSION SEQUENCE

Per-type threshold (EMA auto-tune → fixed multiplier)

update_compression_ratioyesno200elseerrorcode_*diff/git/texttool_output/jsonthreshold_for(ct)ct in linter/build_output/log?return base (no tune, no mult)ratio = compression_ratio_ema/100ratiotune = 2.0 (compressing well → raise bar)tune = 0.5 (weak → lower bar)tune = 1.0base' = base*tunefixed per-type multiplierbase'*8base' * code_multiplier (default 3.0)base'*2base'ratio = orig/comp * 100new = 0.2*ratio + 0.8*old (α=0.2)fill_pct = clamp(100 - ema/20, 1..99)*100
PER-TYPE THRESHOLD (EMA AUTO-TUNE → FIXED MULTIPLIER)

Call sites

ConcernModule
proxy_handler request/response orchestrationcrates/aphrodite/src/proxy.rs
compress_chat_completioncrates/aphrodite/src/proxy.rs
proxy_detect_content_type (classify)crates/aphrodite/src/proxy.rs
proxy_build_preview / proxy_format_ccr_outputcrates/aphrodite/src/proxy.rs
threshold_for / update_compression_ratio / compute_fill_pctcrates/aphrodite/src/proxy.rs
compute_key (BLAKE3, 40-hex)vendor/headroom/crates/headroom-core/src/ccr/mod.rs
smart_marker / cache_markercrates/aphrodite/src/proxy.rs
tool_calls pass-through rationale + testcrates/aphrodite/src/proxy.rs