DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Building an AI-Powered Incident Triage Agent with .NET Aspire
  • AWS Serverless Lambda Resiliency: Part 1
  • Six Patterns for Building Production-Grade AI Quality Systems
  • Retrieval Augmented Generation With Spring AI 2.0, Claude, and PGvector

Trending

  • How I Run Two AI Coding Agents on One Codebase
  • Memory-First Indexes in SQL Server 2025: Redefining Performance for Hybrid Workloads
  • The Startup Time Trick Hiding Inside Your Docker Build
  • Node.js Microservices Architecture: A Complete Guide
  1. DZone
  2. Data Engineering
  3. AI/ML
  4. Prompt Caching: Overriding Tokenization for Faster and More Cost-Effective AI

Prompt Caching: Overriding Tokenization for Faster and More Cost-Effective AI

Prompt caching allows AI systems to reuse the processing of unchanged token sequences, resulting in faster inference, lower latency, and reduced costs.

By 
Ravi Ranjan Shahi user avatar
Ravi Ranjan Shahi
·
Sep. 11, 26 · Analysis
Likes (0)
Comment
Save
Tweet
Share
89 Views

Join the DZone community and get the full member experience.

Join For Free

As Large Language Models (LLMs) become increasingly integrated into enterprise applications, optimizing response time and reducing operational costs have become critical priorities. One of the most effective techniques for achieving both is Prompt Caching. Instead of processing identical prompt segments repeatedly, prompt caching allows AI systems to reuse previously computed prompt representations, minimizing redundant computation. While tokenization converts text into tokens that the model understands, prompt caching goes a step further by reusing the processing of unchanged token sequences, resulting in faster inference, lower latency, and reduced API costs, especially in applications with repetitive system prompts or recurring contextual information.

 How Prompt Caching Works 

Think of prompt caching as a “memory shortcut” for AI models. Every prompt is first tokenized, but when the same prompt prefix appears again, the model doesn’t need to process those tokens from scratch. Instead, it retrieves the cached computation and only processes the new or modified portion of the prompt.

How Prompt Caching Works


This mechanism is particularly valuable in AI assistants, enterprise chatbots, coding copilots, document analysis platforms, and Retrieval-Augmented Generation (RAG) systems where a significant portion of the prompt remains unchanged across multiple requests.

Best Practices to Maximize Prompt Cache Efficiency

To fully leverage prompt caching, organizations should design prompts strategically. Keep system instructions consistent, place static context before dynamic user inputs, avoid unnecessary formatting changes, and modularize prompt templates. These practices increase cache hit rates, reducing both processing time and infrastructure costs. Monitoring cache performance metrics, such as cache hit ratio, latency improvements, and token savings, helps teams continuously optimize AI workloads while maintaining response quality.

Business Benefits and Real-World Impact

Prompt caching delivers measurable business value beyond technical optimization. Organizations can reduce AI inference costs, improve application responsiveness, support higher request volumes, and enhance the overall user experience. Development teams also benefit from more predictable performance and scalable AI architectures. As enterprise AI adoption grows, prompt caching is becoming an essential optimization technique for building efficient, reliable, and cost-effective generative AI solutions.

Where Prompt Cache Is Stored: Understanding the Architecture

Where a prompt cache is stored depends entirely on which level of the caching architecture you are referring to. To understand where it lives, it is helpful to divide prompt caching into its two primary forms:

Provider-Native Caching (Model-Level)

When you use built-in prompt caching features from providers such as OpenAI, Anthropic (Claude), Google (Gemini), or DeepSeek, the cache is managed internally within the provider’s cloud infrastructure.

What is Stored 

The cache does not store text or responses. Instead, it stores KV Tensors (Key-Value pairs). These are the raw, mathematical attention states that the model's neural network calculated during the "prefill" phase of your prompt 

Where Will it Live?

GPU VRAM / High-Speed RAM: Because these tensors must be accessed instantly to keep latency ultra-low, they are stored directly in the high-speed volatile memory (VRAM) of the AI chips (GPUs/TPUs) or ultra-fast host system memory in the provider's data centers. Internal Distributed Storage: Since GPU memory is highly constrained and expensive, providers use advanced, proprietary cache-eviction systems. If a cache prefix isn't used for a few minutes (the Time-to-Live or TTL), it is automatically evicted (deleted) from the GPU memory to make room for other users

Who Has Access? 

The provider manages this entirely behind the scenes. You cannot download, inspect, or manually move these KV tensors; the system simply checks the memory automatically during your API call and applies a discount if it finds a match.  

Application-Level Caching (User-Controlled Layer) 

If you are building your own caching layer in front of the LLM API to save even more money by bypassing the LLM entirely for repeat queries, you get to choose where it is stored

In-Memory Databases (Most Common)   

Platforms like Redis or Memcached are the industry standard. Because they store data directly in RAM, they can fetch cached prompts in microseconds 

Vector Databases (For Semantic Caching) 

If you want to detect "semantically similar" prompts (e.g., matching "How do I reset my password?" with "I forgot my password"), the cache stores the text embeddings. This is stored in vector databases like Pinecone, Milvus, Qdrant, Weaviate, or pgvector (PostgreSQL)

Relational / NoSQL Databases 

(For Archive/Backup) Standard databases like MongoDB, DynamoDB, or PostgreSQL are used to persistently store historical prompt-response pairs, though they have slightly higher retrieval latency than Redis  

Building a Semantic Cache With Redis

involves upgrading from traditional "exact-match" caching to vector-based similarity caching. Instead of storing raw text, you store the mathematical representation (embeddings) of prompts. When a new prompt comes in, you convert it to an embedding and ask Redis to find the "nearest neighbor" (most similar prompt). 

If the similarity score exceeds your defined threshold (e.g., 95% similar), it's a Cache Hit. 

Here is the step-by-step guide to building a semantic cache using Python, Redis Stack (which includes vector search), and an embedding model (like OpenAI's). 

Prerequisites 

  • Redis Stack: You must use Redis Stack (or Redis Enterprise), as standard Redis does not support vector search. You can run it locally via Docker: docker run -d -p 6379:6379 redis/redis-stack-server:latest. 
  • Python Libraries: Install the required clients. 
  • pip install redis openai numpy: Redis also has a dedicated library called redisvl (Redis Vector Library) built specifically for this, which abstracts a lot of the boilerplate.

Note: Redis also has a dedicated library called redisvl (Redis Vector Library) built specifically for this, which abstracts a lot of the boilerplate.

The workflow follows four steps: 

  1. Embed: Convert the incoming user prompt into a vector embedding. 
  2. Search: Query Redis using a K-Nearest Neighbors (KNN) vector search. 
  3. Evaluate: If the highest similarity score is above your threshold (e.g., > 0.92), return the cached response.  
  4. Fallback and store: If no match is found, send the prompt to the LLM, return the response to the user, and store the new embedding and response in Redis

Conceptual Python Implementation

How the logic flows using standard redis-py and OpenAI:

Python
 
import redis
import numpy as np
from openai import OpenAI
from redis.commands.search.query import Query

# 1. Initialize Clients
redis_client = redis.Redis(host='localhost', port=6379, decode_responses=True)
openai_client = OpenAI(api_key="YOUR_API_KEY")

# Configuration
THRESHOLD = 0.95  # 95% similarity required for a cache hit
INDEX_NAME = "prompt_cache_idx"

def get_embedding(text):
    """Convert text to an embedding vector."""
    response = openai_client.embeddings.create(
        input=text,
        model="text-embedding-3-small"
    )
    return np.array(response.data[0].embedding, dtype=np.float32).tobytes()

def check_semantic_cache(prompt_text):
    """Search Redis for a semantically similar prompt."""
    query_vector = get_embedding(prompt_text)
    
    # Construct a KNN Vector Search Query in Redis
    q = Query(f"*=>[KNN 1 @prompt_vector $vec AS score]")\
        .return_fields("response", "score")\
        .sort_by("score")\
        .dialect(2)
    
    res = redis_client.ft(INDEX_NAME).search(
        q, query_params={"vec": query_vector}
    )
    
    if res.docs:
        # Redis returns distance (0 is perfect match). Convert to similarity.
        similarity = 1 - float(res.docs[0].score)
        
        if similarity >= THRESHOLD:
            print(f"✅ Cache Hit! (Similarity: {similarity:.2f})")
            return res.docs[0].response
            
    print("❌ Cache Miss.")
    return None

def store_in_cache(prompt_text, llm_response):
    """Store the new prompt and response in Redis."""
    prompt_vector = get_embedding(prompt_text)
    
    # Store as a Redis Hash
    doc_id = f"cache:{hash(prompt_text)}"
    redis_client.hset(doc_id, mapping={
        "prompt": prompt_text,
        "response": llm_response,
        "prompt_vector": prompt_vector
    })
    # Optional: Set a Time-To-Live (TTL) so the cache clears old entries
    redis_client.expire(doc_id, 86400) # 24 hours


Best Practices for Production

Use a library: Instead of writing the raw vector math and RediSearch queries yourself, use RedisVL (pip install redisvl) or LangChain's Redis Cache integration. They have built-in SemanticCache classes that handle index creation and threshold tuning with just 3 lines of code. 

Tune your threshold carefully: A threshold that is too low (e.g., 0.80) will cause "false positives" (returning an answer to a question that is only vaguely related). A threshold too high (e.g., 0.99) defeats the purpose, acting almost like an exact-match cache. Test with 0.92 to 0.95 as a baseline. 

Filter by user/tenant: If you are building a multi-tenant app, make sure to add metadata tags (like user_id or tenant_id) to your Redis hashes. Your vector query must pre-filter by the user_id, so User A doesn't accidentally get a cached response meant for User B. 

Cost Savings by Major Provider

LLM providers apply discounts specifically to input tokens that hit the cache (output tokens are always billed at the standard rate)


Real-World Impact and Key Benchmarks

Enterprise scale: One of the big Tech companies, like TikTok, has reported cutting their AI agent inference costs by 50% with minimal code adjustments. 

Agentic architectures: For complex, long-running agentic workflows (where a system prompt and conversation history are repeatedly sent over dozens of steps), prompt caching typically achieves 78% to 81% total cost reductions because the massive system instructions only need to be processed once. 

Break-even point: On platforms like Anthropic (which charge a 25% premium to write to the cache), you only need to hit the cache twice on a given prompt prefix to break even and start saving money. Every subsequent read is essentially 90% off.

In addition to saving money, prompt caching dramatically improves user experience by skipping the heavy "prefill" computation. It reduces Time-to-First-Token (TTFT) by 50% to 85%, meaning long documents or extensive chat histories return responses in a fraction of a second instead of causing a noticeable delay. 

Take Action: Build Smarter AI Applications

Prompt caching is no longer an optional optimization—it’s a competitive advantage for organizations deploying AI at scale. If you’re building enterprise AI applications, evaluate where repetitive prompts exist and redesign your prompt architecture to maximize cache utilization. Small changes in prompt design can lead to significant savings in cost, latency, and compute resources.

AI API Data structure K-nearest neighbors algorithm Time to live User experience Cache (computing) Redis (company) systems large language model

Opinions expressed by DZone contributors are their own.

Related

  • Building an AI-Powered Incident Triage Agent with .NET Aspire
  • AWS Serverless Lambda Resiliency: Part 1
  • Six Patterns for Building Production-Grade AI Quality Systems
  • Retrieval Augmented Generation With Spring AI 2.0, Claude, and PGvector

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook