Marcio Cunha

How to reduce LLM costs using semantic cache with Qdrant

Learn how to architect a vector caching layer to intercept duplicate prompts and save on AI API costs using Qdrant.

Marcio Cunha•3 min
Also available in:PortuguêsEspañol
Summary
  • Repeated or similar queries sent to large language model APIs drain budgets and add unnecessary latency
  • Semantic caching compares the meaning of current and past questions using text embeddings
  • Qdrant allows storing these embeddings and performing high-performance searches using cosine similarity
  • Interception happens before hitting the external API, ensuring instant responses for equivalent questions
  • Proper distance threshold tuning prevents false positives while maintaining flexibility in prompt interpretation

Why artificial intelligence API costs spiral out of control

As companies and developers scale applications powered by large language models (LLMs), the budget slice dedicated to external providers skyrockets. Every query sent to the API generates considerable computational consumption, even when hundreds of users ask the exact same question or ask things with identical meanings using different wording. This financial and bandwidth waste demands smarter architectural approaches to mitigate the problem.

Traditional relational database or Redis caching fails miserably in this scenario because it requires exact string matching. If one user types 'what are your opening hours' and another types 'when do you open', a traditional cache treats both inputs as completely different. In practice, we need an intermediate layer that understands the human intent behind sentences before hitting the external API, something we detail when integrating tools like vector memory and RAG with Qdrant in production to optimize data flows.

What semantic caching is and how it intercepts prompts

Semantic caching works by transforming each user prompt into a numerical vector, also known as an embedding, through a lightweight vectorization model executed locally. This vector represents the conceptual meaning of the phrase in a multidimensional mathematical space. When a new question arrives, the system calculates the mathematical distance between the current vector and the vectors stored in the vector database to identify if an identical or very similar question was previously answered.

If the similarity between the current prompt and a previous record exceeds a configured threshold (such as 92% proximity), the application bypasses the request and immediately returns the cached response. This eliminates network latency and zeroes out inference costs for the main LLM on that interaction. This operational gain resembles the security optimizations we apply when implementing secure function calling in Python with Pydantic during payload validation.

Architecture and practical implementation using Qdrant

To build this caching layer, Qdrant stands out as an excellent choice due to its indexing speed and real-time metadata filtering support. The architectural flow begins at the application middleware: upon receiving the request, the system generates the question embedding, queries Qdrant looking for the nearest neighbor, and decides whether to reuse the generated text or proceed to the main API.

Below is a conceptual Python example demonstrating how this verification and storage logic is structured simply and directly within the user interaction flow:

from qdrant_client import QdrantClient

client = QdrantClient('http://localhost:6333')

def check_semantic_cache(prompt_vector):
    results = client.search(
        collection_name='prompt_cache',
        query_vector=prompt_vector,
        limit=1,
        score_threshold=0.92
    )
    if results:
        return results[0].payload['response']
    return None

If no result reaches the minimum similarity threshold, the application calls the heavy language API, receives the model response, and writes a new point to Qdrant containing the question vector and the received response, ensuring the next identical query is handled instantly.

Technical verdict and real impact on engineering budgets

Adopting a semantic caching layer with Qdrant transforms the economics of projects dealing with thousands of daily users asking repetitive questions, such as tech support chats, FAQ assistants, and automated customer service tools. The operational cost of running a small local model to generate embeddings is negligible compared to the savings gained by avoiding repeated calls to expensive proprietary models.

However, operational trade-offs must be weighed, such as the need to expire cache entries when business context changes and tuning the similarity threshold to prevent the system from giving incorrect answers to similar-looking questions with distinct intents. For teams looking to scale AI applications with financial responsibility, building this vector barrier is no longer a luxury but a core engineering requirement.