home
diamond Go Premium
Data Engineering Path  ·  Data & AI

Token Cost Management & Semantic Caching

+-------------------------------------------------------------------------------+
|                            SEMANTIC CACHING FLOW                              |
+-------------------------------------------------------------------------------+
|  User Query: "How do I optimize PySpark join?"                                |
|                                                                               |
|  1. Compute Embedding: Vector [0.14, -0.82, ...]                              |
|  2. Vector Index Query in Redis (Cosine Distance Threshold < 0.08)            |
|                                                                               |
|  Hit: Found past query: "Best way to tune joins in PySpark"                   |
|  -> Return Cached Answer Instantly (5ms latency, $0.00 cost!)                |
+-------------------------------------------------------------------------------+

Exact vs. Semantic Caching

Standard key-value caches (like Memcached or Redis exact match) fail when users rephrase the same question:

  • "What is delta lake?" vs. "Can you explain what Delta Lake is?"
  • Exact match misses 100% of the time.
  • Semantic caching embeds the prompt, queries a vector index of previous prompts, and serves the cached response if cosine similarity is above a threshold (e.g. $> 0.95$).

Implementing Semantic Cache in Redis

import redis
import numpy as np
from openai import OpenAI

client = OpenAI()
r = redis.Redis(host='localhost', port=6379, decode_responses=False)

def get_embedding(text: str) -> list[float]:
    res = client.embeddings.create(input=text, model="text-embedding-3-small")
    return res.data[0].embedding

def query_with_semantic_cache(prompt: str, threshold: float = 0.96) -> str:
    prompt_emb = np.array(get_embedding(prompt), dtype=np.float32)

    # In production, use Redis VSS (Vector Similarity Search)
    # redis_query = ...

    # If cache miss:
    print("Cache miss! Querying LLM...")
    res = client.chat.completions.create(
        model="gpt-4o-mini",
        messages=[{"role": "user", "content": prompt}]
    )
    answer = res.choices[0].message.content
    # Store prompt embedding and answer
    return answer
Find this content helpful? ☕ Buy me a coffee

Entity Details

Create New Item

help

Submit Technical Query

Have a question or run into an issue? Describe it below, upload an optional screenshot, and our engineering team will answer it!

image Attach image (optional)

Submit Feedback

build Free Developer Utility Free Tool
gavel

Privacy & Legal Disclaimer

1. Client-Side Browser Processing

All utility tools on DeepEngineerHub (including Image to PDF, Text Formatters, JSON Converters, and Encryptors) execute 100% locally within your client browser using WebAssembly and JavaScript. No uploaded images, text, or documents are transmitted, collected, or stored on remote servers.

2. Limitation of Liability ("As-Is" Provision)

Tools and services are provided free of charge for convenience and educational purposes "as-is" without warranties of any kind. DeepEngineerHub shall not be held liable for any data loss, formatting inconsistencies, or indirect damages resulting from tool usage.

3. Open Source & Third-Party Software

Certain utilities utilize open-source client libraries (such as jsPDF, Mermaid.js, Pyodide) licensed under MIT, Apache, or BSD open licenses. All intellectual property remains with their respective copyright holders.