AI Agent Semantic Cache 2026
"Beijing's weather" and "What's the temperature in Beijing today" — asked 100 times, hitting the API 100 times. Semantic caching lets similar questions share the same answer.
💡 What You Will Learn
"Beijing's weather" and "What's the temperature in Beijing today" — asked 100 times, hitting the API 100 times. Semantic caching lets similar questions share the same answer.
from sentence_transformers import SentenceTransformer
import numpy as np
model = SentenceTransformer("BAAI/bge-small-zh-v1.5")
cache = {}
def semantic_get(query, threshold=0.92):
q_vec = model.encode(query)
for cached_q, response in cache.items():
c_vec = model.encode(cached_q)
sim = np.dot(q_vec, c_vec) / (np.linalg.norm(q_vec) * np.linalg.norm(c_vec))
if sim > threshold:
return response
return None
Results
|:--------|:-----:|:----------:| || 0% | 0% | || 15-20% | 15-20% | || 40-60% | 40-60% |
Summary
Related Articles
2026-07-16
AI Agent Prompt Versioning 2026
2026-07-20
AI Agent Step-by-Step Tutorial 2026: Build Your First Autonomous Agent from Scratch
2026-08-01
Best LLM Fine-Tuning Tools in 2026: 8 Open-Source Compared by Stars and Ease
Written by our editorial team; tools listed here are tested or verified against public sources. Links point to official sites or GitHub repos for reference only — no paid placements.
