CCR

CCR Lifecycle

IN THIS DOCUMENT

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

CCR (Compress-Cache-Retrieve) is the lossless end-to-end compression path for LLM proxy traffic: Aphrodite hashed the content, stored it, and replaced it with a compact marker in the response, and the agent retrieved the original by hash on demand.

This document walks the six phases of that lifecycle, from compression to expiry.

Phase 1: Compress

The chat-completions path rewrites each choices[].message.content that is large enough to be worth compressing:

  1. Detect the content type (proxy::proxy_detect_content_type).
  2. Compute the threshold for that type (base threshold x type multiplier x auto-tune factor x budget multiplier - see Thresholds below).
  3. If content.len() > threshold: compute the BLAKE3 hash, check the CCR backend (hit or miss), store on miss, and replace the content with a rendered marker. A content block is only replaced when the store actually holds the hash - a failed store leaves the content uncompressed rather than emitting an unresolvable marker.
  4. If the content is above the inline threshold but below the compression threshold, store it in the inline cache instead (see Inline).
  5. Update the savings counters and the compression-ratio EMA used by auto-tune.

tool_calls[].function.arguments is never a compression target: it is client-executable JSON, not model-facing prose, and compressing it would produce a tool call the client cannot parse.

Streaming (text/event-stream) responses bypass this phase entirely.

The direct POST /ccr/create endpoint stores content without rendering a marker in any response, but feeds the same counters and the same compression-ratio EMA (using a byte-entropy estimate - unique 3-byte trigrams in the first 4096 bytes - as the effective compressed size).

Phase 2: Cache

Before storing, Aphrodite checked the hash against the backend (ccr_hits / ccr_misses counters) and against the inline cache (inline_ccr_hits / inline_ccr_misses).

A separate LLM response cache avoids repeat upstream round-trips for identical requests:

PropertyDetail
KeyFNV-1a 64-bit over api key, model, messages, tools, tool_choice, temperature, top_p, n, and response_format, in fixed canonical order
CapacityLRU, 128 entries, 1 MiB per body
TTLccr_ttl_seconds (default 3600, env APHRODITE_CCR_TTL)
ScopeStreaming requests are never cached; successful responses only
Response headerX-Aphrodite-Cache: HIT or MISS

Phase 3: Store

Three storage tiers, plus the session-side inline store used by the Hermes plugin integration:

TierThresholdBackendCapacityExpiry
Inline (proxy)inline threshold (256 B default) to compression thresholdlru::LruCache1,024 entriesLRU eviction only
Inline (session)any prefetched / compressed blobHashMap + LRU order, byte budget500 entries, 256 MiBLRU + byte-budget eviction
Cache mode> 8 KiBInMemoryCcrStore (DashMap)10,000 entriesSliding idle TTL (default 3600 s), 8x max lifetime
Token mode> 1 KiBSqliteCcrStore (SQLite)Unlimited (disk)Sliding idle TTL (default 3600 s), 8x max lifetime

Backend details live in backends. Thresholds are configurable: env var > TOML [compression] > compiled-in default for cache, token, inline, and code multiplier.

Phase 4: Return Marker

Cache mode replaces content with a plain 512-character excerpt plus the marker line; token mode renders a type-aware summary with metadata.

Both use the same format_ccr_output template (see Marker Format).

Compressed responses carry:

  • X-Aphrodite-Compressed: true
  • X-Aphrodite-Cache: HIT or MISS
  • X-Aphrodite-Fill-Pct: XX.X (headroom fill derived from the compression ratio EMA)

Phase 5: Retrieve

The HTTP /retrieve endpoint resolves a hash to its original content:

  1. Normalize the hash argument (strip a trailing |type|size suffix, trim).
  2. Check the inline cache first; on a hit, return immediately.
  3. Fall back to the CCR backend; on a miss, return 404 NOT_FOUND.
  4. Apply an optional query filter (case-insensitive line match, query capped at 512 chars) and optional pagination (offset + limit; an explicit limit is clamped to 10,000 lines, limit: 0 returns the full document).
  5. The endpoint prefixed windowed results with a [lines a-b/total] header and flagged them via the truncated field.

Full-document retrieval is byte-identical - including a trailing newline - so the returned body hashes back to the marker’s own hash.

The Hermes aphrodite_retrieve tool is a separate path that resolves against the session’s in-process inline store:

  • Recursive expansion of nested markers up to 5 levels deep; at the depth limit the raw content is returned rather than a placeholder.
  • Workspace-bounded file-path reads (10 MiB cap).
  • Inline-store fallback; never writes expanded content back over the stored entry, preserving the content-address invariant.

Phase 6: Expire

BackendExpiry behavior
SQLiteLazy purge on get and put, debounced to once per 60 seconds; rows expire when idle past TTL or older than the 8x max lifetime. store_max_entries / store_max_bytes evict least-recently-accessed rows past either space cap. No background thread.
In-MemoryLazy TTL check on get; expired entries evicted via an atomic check-and-remove; queue compaction when the eviction order grows past twice capacity.
Inline (proxy)LRU eviction at capacity (1,024). No TTL.
Inline (session)LRU eviction at 500 entries and/or over the 256 MiB byte budget. No TTL.

Thresholds

Base Thresholds

ConstantDefaultMode
Cache compress8,192Cache
Token compress1,024Token
Inline256All

Each is resolved as env var > TOML [compression] value > compiled-in default. The code multiplier defaults to 3.0.

Per-Type Multipliers

TypeMultiplier
errorx8
code_rust/code_python/code_go/code_js/codex code multiplier (default 3.0)
diff, git, textx2
tool_output, jsonx1 (base)
linter, build_output, logx1 (base, never discounted)

linter, build_output, and log are pinned at the base threshold before any multiplier is applied, so build output stays visible in coding sessions.

Auto-Tune

The compression-ratio EMA (x100) adjusts every threshold:

EMA ratioTune factorEffect
> 20.02.0Raise thresholds (compress less)
3.0 .. 20.01.0Default
< 3.00.5Lower thresholds (compress more)
0.01.0No history - default

Budget Multiplier

An x-headroom-budget request header (fill percentage 0-100) scales the effective threshold on a smooth linear curve, never below 0.5x:

budget_mult = clamp(0.50 + (budget% / 100) * 0.50, 0.50, 1.0)
Budget (fill %)Multiplier
0%0.50
50%0.75
100% or absent1.00

Session Catalog Emission

Per-turn catalog summaries were emitted delta-only: the renderer remembers last_emitted_marker_count and last_emitted_file_count and reports +N new compressions this turn / +N new files only when new items arrived, with a stable “no change” line otherwise.

Counters reset on session start.