Caching Embeddings: Reducing API Costs by 90%

  • author: "Vikas Dhyani"
  • date: "2026-03-21"
  • read_time: "~1 min"

Large volume LLM applications suffer from high latency and scaling costs. A significant percentage of user queries are semantically similar. By implementing **Semantic Caching** using Redis and embeddings cosine similarity, we can bypass LLM generation entirely for repeat queries.

Semantic Matching Logic

When a query arrives, we compute its vector embedding and query Redis using Vector Similarity Search. If the distance is below our similarity threshold (e.g. cosine distance < 0.1), we return the cached response.


# Semantic cache verification logic
def get_cached_response(query_vector, redis_client):
    match = redis_client.ft("idx:cache").search(
        Query("*=>[VECTOR_RANGE 0.1 $vec] AS score").sort_by("score")
    )
    if match.docs:
        return match.docs[0].response
    return None

One log a week. No hype.

Join 1,000+ developers getting practical insights on full-stack AI engineering, vectors optimization, and agent security. Direct to your inbox.

  • One practical engineering walkthrough every week
  • Real systems and architectural evaluations
  • No spam, unsubscribe with a single click

Why Work With Me?

17+ Years of
Experience

Proven experience building secure, reliable, and business-critical software systems.

AI + Enterprise
Expertise

Practical AI solutions integrated with scalable enterprise architecture.

End-to-End
Ownership

From requirements and architecture through development, deployment, and support.

Clear
Communication

Transparent progress, realistic timelines, and maintainable solutions.