Community Python Snippet

An Embedding Cache With Content-Hash Keys

Re-embedding the same paragraphs on every deploy was costing us $400 a month. This is the SQLite-backed cache I shipped: the key is sha256(model + normalized text), TTL is per-row, and a single batch call backfills misses.

An Embedding Cache With Content-Hash Keys

Re-embedding the same paragraphs on every deploy was costing us $400 a month. This is the SQLite-backed cache I shipped: the key is sha256(model + normalized text), TTL is per-row, and a single batch call backfills misses.

Python
Compiler
3 snippets
embedding
caching
vector-search
hashing
amaragupta

By @amaragupta

April 12, 2026

·

Updated May 20, 2026

477 views

9

4.3 (11)

The cache is a 40-line SQLite table with a single (key, vector, expires_at) row per text. The interesting choice is the key: NFKC-normalized text plus the model id, hashed with sha256. NFKC means caf\u00e9 (single codepoint) and cafe\u0301 (combining accent) collapse to the same key, which actually matters because OpenAI tokenizes them identically and we do not want two cache rows for the same vector. The TTL is per-row rather than per-cache because some embeddings (product names) churn weekly while others (legal text) stay valid for a year. The model id in the key is the safety net: when we upgrade from text-embedding-3-small to large, every key changes and the cache invalidates itself.