LLM Application Development
Beginner
4.5
Cache Embeddings to Save Time and Money
Never pay to embed the same text twice.
0h 15m
1 lesson
1.2K students
What You'll Learn
Learning objectives will be added soon.
Tutorial Content
The waste
Re-embedding unchanged documents on every run burns money and time. Cache by a hash of the text.
import hashlib, json, os
def cached_embed(text, cache="emb.json"):
store = json.load(open(cache)) if os.path.exists(cache) else {}
key = hashlib.sha256(text.encode()).hexdigest()
if key not in store:
store[key] = embed(text)
json.dump(store, open(cache, "w"))
return store[key]In production
Use a real store (Redis, your database) keyed by content hash, and re-embed only when the source text changes. Simple, and it pays for itself immediately on large corpora.
Your Progress
Sign in to track your progress
Tags
Embeddings
API