fastloop-guard is a high-performance semantic cache daemon for LLM query responses. It uses a three-gate lookup — exact BLAKE2b fingerprint → fuzzy MinHash similarity → miss — to dramatically reduce redundant LLM API calls. Built as a Unix socket server with async Tokio runtime, it provides sub-millisecond cache lookups with configurable TTL and LRU eviction.
LLM inference is expensive: $0.01–$0.06 per 1K tokens for frontier models. In production agent systems, 30–60% of queries are near-duplicates — rephrased questions, case variants, trivial whitespace differences. Without caching, you're paying for the same answer repeatedly.
FastLoop Guard intercepts the query stream:
| Gate | Match Type | Example | Cost Saving |
|---|---|---|---|
| 1: Exact | BLAKE2b fingerprint | "check disk usage" == "check disk usage" | 100% |
| 2: Fuzzy | MinHash Jaccard ≥ threshold | "check disk usage" ≈ "check disk space" | 100% |
| 0: Miss | No match | Novel query | Forward to LLM |
Real-world results: 40–70% hit rates in agent workloads, cutting API costs proportionally.
Query is normalized (lowercase, whitespace collapse) and hashed with BLAKE2b-512, truncated to 32 bytes:
Lookup is an O(1) LRU cache hit by 32-byte key. Normalization ensures "Check Disk" and "check disk" map to the same fingerprint.
Complexity: O(n) hash computation (n = query length), O(1) cache lookup.
If Gate 1 misses, the query is converted to a MinHash signature and compared against all cached entries.
The query is lowercased, whitespace-normalized, and split into character 3-grams (shingles):
Using k = 128 universal hash functions
Each of the 128 hash functions independently estimates the Jaccard similarity.
The estimated Jaccard similarity between signatures:
By the MinHash theorem,
If
No match in either gate — forward to the LLM, then store the result.
| Mechanism | Implementation |
|---|---|
| Eviction | LRU by access time |
| Expiry | TTL (default 1 hour) |
| Capacity | Configurable (default 4096 entries) |
| Concurrency | parking_lot::Mutex for thread-safe access |
| Stats | Atomic hit/miss counters, hit-rate computation |
| Operation | Time | Notes |
|---|---|---|
| Gate 1 (exact) | O(n) hash + O(1) lookup | n = query length |
| Gate 2 (fuzzy) | O(n + k·m) | k = 128 hash funcs, m = cached entries scanned |
| Insert | O(n + k) | Hash + signature + LRU put |
| Stats | O(1) | Atomic reads |
[dependencies]
fastloop-guard = "0.2"# Start the guard daemon (listens on /tmp/fastloop.sock)
cargo run --release
# Query via JSON over Unix socket
echo '{"type":"lookup","query":"check disk usage","threshold":0.95}' | \
nc -U /tmp/fastloop.sockuse fastloop_guard::gate::Gate;
use std::time::Duration;
let gate = Gate::with_params(4096, Duration::from_secs(3600));
// Lookup
let resp = gate.lookup("check disk usage", 0.95);
if !resp.hit {
// Miss → call LLM, then store
gate.insert("check disk usage", "df -h");
}
// Stats
let (hits, misses, rate) = gate.cache.stats();Request:
{ "type": "lookup", "query": "...", "threshold": 0.95 }
{ "type": "store", "query": "...", "response": "..." }
{ "type": "stats", "stats": true }Response:
{ "hit": true, "response": "df -h", "gate": 1, "latency_us": 42 }
{ "stored": true }
{ "hits": 1503, "misses": 891, "hit_rate": 0.628 }| Type | Method | Description |
|---|---|---|
Gate |
new() |
Default config (4096 capacity, 1h TTL) |
Gate |
with_params(capacity, ttl) |
Custom config |
Gate |
lookup(query, threshold) |
Three-gate lookup, returns CacheResponse |
Gate |
insert(query, response) |
Store a query→response pair |
Gate |
stats() |
Get hits, misses, hit-rate |
FastLoop Guard implements γ + η = C:
- γ (gamma): The caching protocol — the specification of how queries are normalized, fingerprinted, compared, and cached. This includes the three-gate strategy, the MinHash parameters (128 permutations, 3-gram shingles), and the LRU + TTL eviction policy.
- η (eta): The Rust implementation — BLAKE2b via the
blake2crate,lru::LruCachewithparking_lot::Mutex, Tokio async Unix socket server,serde_jsonprotocol framing. This is the concrete machinery. - C (Configuration): Cost-effective query acceleration — the emergent property when the caching protocol (γ) is correctly realized in the runtime (η). When aligned, identical and near-identical queries return in microseconds instead of triggering expensive LLM calls, with measurable hit-rate statistics for continuous optimization.
The three-gate design is deliberate: Gate 1 (exact) handles the common case with maximum speed and zero false positives. Gate 2 (fuzzy) captures semantically equivalent rephrasings at the cost of scanning cached signatures. The threshold θ trades precision for recall: higher θ = fewer false fuzzy matches but more misses; lower θ = more hits but occasional wrong answers.
fastloop-guard/
├── src/
│ ├── main.rs # Unix socket server, connection handler
│ ├── gate.rs # Gate: owns QueryCache, handles requests
│ ├── cache.rs # QueryCache: three-gate lookup logic
│ ├── hash.rs # BLAKE2b fingerprinting + normalization
│ ├── similarity.rs # MinHash signatures + Jaccard estimation
│ └── protocol.rs # Serde JSON request/response types
- Broder, A. Z. (1997). "On the Resemblance and Containment of Documents." Proc. Compression and Complexity of Sequences (SEQUENCES'97), pp. 21–29. IEEE. — The MinHash technique and Jaccard estimation theorem.
- Broder, A. Z., Charikar, M., Frieze, A. M., & Mitzenmacher, M. (1998). "Min-Wise Independent Permutations." Proc. 30th ACM STOC, pp. 327–336. — Formal analysis of MinHash estimation error.
- Aumüller, M., et al. (2020). "FairEval: Evaluating Deduplication with MinHash." Proc. SIGMOD. — Practical MinHash parameter selection.
- Saier, R., & Saake, G. (2023). "Semantic Caching for Large Language Models." Datenbank-Spektrum, 23, 1–12. — LLM-specific semantic cache design.
- Aumasson, J.-P., Neves, S., Wilcox-O'Hearn, F., & Winnerlein, C. (2013). "BLAKE2: Simpler, Smaller, Fast as MD5." Proc. ACNS, LNCS 7954. — BLAKE2b hash function specification.
- Tokio Project. (2024). "Asynchronous Programming in Rust." tokio.rs. — Async runtime for the Unix socket server.
- Cormen, T. H., et al. (2022). Introduction to Algorithms, 4th ed. MIT Press. — LRU cache (Ch. 15), universal hashing (Ch. 11).
MIT