Close Menu
    Facebook LinkedIn YouTube WhatsApp X (Twitter) Pinterest
    Trending
    • Brain-inspired AI chip could save 70% energy
    • Liquid Instruments jags more taxpayer funding in $70 million Series C
    • MAGA Is Confused About ‘Animal Farm’
    • Meta says it might be forced to withdraw its apps from New Mexico if a judge orders it to adopt the state’s proposed safety features (Thomas Barrabi/New York Post)
    • Samsung Chip Profits Soar Amid the Tech World’s RAM Shortages
    • DAIMON Robotics Wants to Give Robot Hands a Sense of Touch
    • A Gentle Introduction to Stochastic Programming
    • This startup’s new mechanistic interpretability tool lets you debug LLMs
    Facebook LinkedIn WhatsApp
    Times FeaturedTimes Featured
    Thursday, April 30
    • Home
    • Founders
    • Startups
    • Technology
    • Profiles
    • Entrepreneurs
    • Leaders
    • Students
    • VC Funds
    • More
      • AI
      • Robotics
      • Industries
      • Global
    Times FeaturedTimes Featured
    Home»Artificial Intelligence»Why Care About Prompt Caching in LLMs?
    Artificial Intelligence

    Why Care About Prompt Caching in LLMs?

    Editor Times FeaturedBy Editor Times FeaturedMarch 13, 2026No Comments12 Mins Read
    Facebook Twitter Pinterest Telegram LinkedIn Tumblr WhatsApp Email
    Share
    Facebook Twitter LinkedIn Pinterest Telegram Email WhatsApp Copy Link


    , we’ve talked quite a bit about what an incredible tool RAG is for leveraging the facility of AI on customized information. However, whether or not we’re speaking about plain LLM API requests, RAG functions, or extra complicated AI brokers, there’s one frequent query that continues to be the identical. How do all these items scale? Particularly, what occurs with price and latency because the variety of requests in such apps grows? Particularly for extra superior AI brokers, which may contain multiple calls to an LLM for processing a single person question, these questions change into of explicit significance.

    Fortuitously, in actuality, when making calls to an LLM, the identical enter tokens are normally repeated throughout a number of requests. Customers are going to ask some particular questions far more than others, system prompts and directions built-in in AI-powered functions are repeated in each person question, and even for a single immediate, fashions carry out recursive calculations to generate a complete response (bear in mind how LLMs produce textual content by predicting phrases one after the other?). Much like different functions, the usage of the caching idea can considerably assist optimize LLM request prices and latency. As an illustration, in response to OpenAI documentation, Immediate Caching can scale back latency by as much as a formidable 80% and enter token prices by as much as 90%.


    What about caching?

    Basically, caching in computing is not any new concept. At its core, a cache is a element that shops information briefly in order that future requests for a similar information might be served sooner. On this means, we will distinguish between two primary cache states – a cache hit and a cache miss. Particularly:

    • A cache hit happens when the requested information is discovered within the cache, permitting for a fast and low cost retrieval.
    • A cache miss happens when the info just isn’t within the cache, forcing the appliance to entry the unique supply, which is dearer and time-consuming.

    One of the vital typical implementations of cache is in internet browsers. When visiting an internet site for the primary time, the browser checks for the URL in its cache reminiscence, however finds nothing (that might be a cache miss). For the reason that information we’re in search of isn’t regionally obtainable, the browser has to carry out a dearer and time-consuming request to the online server throughout the web, to be able to discover the info within the distant server the place they initially exist. As soon as the web page lastly hundreds, the browser sometimes copies that information into its native cache. If we attempt to reload the identical web page 5 minutes later, the browser will search for it in its native storage. This time, it should discover it (a cache hit) and cargo it from there, with out reaching again to the server. This makes the browser work extra rapidly and eat fewer sources.

    As it’s possible you’ll think about, caching is especially helpful in programs the place the identical information is requested a number of instances. In most programs, information entry isn’t uniform, however moderately tends to comply with a distribution the place a small fraction of the info accounts for the overwhelming majority of requests. A big portion of real-life functions follows the Pareto principle, which means that about of 80% of the requests are about 20% of the info. If not for the Pareto precept, cache reminiscence would should be as massive as the first reminiscence of the system, rendering it very, very costly.


    Immediate Caching and a Little Bit about LLM Inference

    The caching idea – storing ceaselessly used information someplace and retrieving it from there, as an alternative of acquiring it once more from its main supply – is utilized in an identical method for enhancing the effectivity of LLM calls, permitting for considerably decreased prices and latency. Caching might be utilised in varied components which may be concerned in an AI software, most essential of which is Immediate Caching. Nonetheless, caching can even present nice advantages by being utilized to different elements of an AI app, reminiscent of, as an illustration, caching in RAG retrieval or query-response caching. Nonetheless, this publish goes to solely deal with Immediate Caching.


    To grasp how Immediate Caching works, we should first perceive somewhat bit about how LLM inference – utilizing a skilled LLM to generate textual content – features. LLM inference just isn’t a single steady course of, however is moderately divided into two distinct levels. These are:

    • Pre-fill, which refers to processing the whole immediate directly to provide the primary token. This stage requires heavy computation, and it’s thus compute-bound. We could image a really simplified model of this stage as every token attending to all different tokens, or one thing like evaluating each token with each earlier token.
    • Decoding, which appends the final generated token again into the sequence and generates the subsequent one auto-regressively. This stage is memory-bound, because the system should load the whole context of earlier tokens from reminiscence to generate each single new token.

    For instance, think about we have now the next immediate:

    What ought to I prepare dinner for dinner? 

    From which we could then get the primary token:

    Right here

    and the next decoding iterations:

    Right here 
    Listed here are 
    Listed here are 5 
    Listed here are 5 straightforward 
    Listed here are 5 straightforward dinner 
    Listed here are 5 straightforward dinner concepts

    The difficulty with that is that to be able to generate the whole response, the mannequin must course of the identical earlier tokens over and over to provide every subsequent phrase throughout the decoding stage, which, as it’s possible you’ll think about, is extremely inefficient. In our instance, which means that the mannequin would course of once more the tokens ‘What ought to I prepare dinner for dinner? Listed here are 5 straightforward‘ for producing the output ‘concepts‘, even when it has already processed the tokens ‘What ought to I prepare dinner for dinner? Listed here are 5′ some milliseconds in the past.

    To unravel this, KV (Key-Value) Caching is utilized in LLMs. Which means intermediate Key and Worth tensors for the enter immediate and beforehand generated tokens are calculated as soon as after which saved on the KV cache, as an alternative of recomputing from scratch at every iteration. This ends in the mannequin performing the minimal wanted calculations for producing every response. In different phrases, for every decoding iteration, the mannequin solely performs calculations to foretell the latest token after which appends it to the KV cache.

    Nonetheless, KV caching solely works for a single immediate and for producing a single response. Immediate Caching extends the rules utilized in KV caching for using caching throughout completely different prompts, customers, and periods.


    In observe, with immediate caching, we save the repeated components of a immediate after the primary time it’s requested. These repeated components of a immediate normally have the type of massive prefixes, like system prompts, directions, or retrieved context. On this means, when a brand new request accommodates the identical prefix, the mannequin makes use of the computations made beforehand as an alternative of recalculating from scratch. That is extremely handy since it could possibly considerably scale back the working prices of an AI software (we don’t need to pay for repeated inputs that include the identical tokens), in addition to scale back latency (we don’t have to attend for the mannequin to course of tokens which have already been processed). That is particularly helpful in functions the place prompts include massive repeated directions, reminiscent of RAG pipelines.

    It is very important perceive that this caching operates on the token degree. In observe, which means that even when two prompts differ on the finish, so long as they share the identical token prefix, the cached computations for that shared portion can nonetheless be reused, and solely carry out new calculations for the tokens that differ. The tough half right here is that the frequent tokens need to be in the beginning of the immediate, so how we type our prompts and directions turns into of explicit significance. In our cooking instance, we will think about the next consecutive prompts.

    Immediate 1
    What ought to I prepare dinner for dinner? 

    after which if we enter the immediate:

    Immediate 2
    What ought to I prepare dinner for launch? 

    The shared tokens ‘What ought to I prepare dinner’ needs to be a cache hit, and thus one ought to count on to eat considerably decreased tokens for Immediate 2.

    Nonetheless, if we had the next prompts…

    Immediate 1
    Supper time! What ought to I prepare dinner? 

    after which

    Immediate 2
    Launch time! What ought to I prepare dinner? 

    This may be a cache miss, because the first token of every immediate is completely different. For the reason that immediate prefixes are completely different, we can not hit cache, even when their semantics are basically the identical.

    Consequently, a primary rule of thumb on getting immediate caching to work is to at all times append any static info, like directions or system prompts, in the beginning of the mannequin enter. On the flip aspect, any sometimes variable info like timestamps or person identifications ought to go on the finish of the immediate.


    Getting our palms soiled with the OpenAI API

    These days, many of the frontier basis fashions, like GPT or Claude, present some type of Immediate Caching performance straight built-in into their APIs. Extra particularly, within the talked about APIs, Immediate Caching is shared amongst all customers of a corporation accessing the identical API key. In different phrases, as soon as a person makes a request and its prefix is saved in cache, for another person inputting a immediate with the identical prefix, we get a cache hit. That’s, we get to make use of precomputed calculations, which considerably scale back the token consumption and make the response technology sooner. That is notably helpful when deploying AI functions within the enterprise, the place we count on many customers to make use of the identical software, and thus the identical prefixes of inputs.

    On most up-to-date fashions, Immediate Caching is mechanically activated by default, however some degree of parametrization is offered. We will distinguish between:

    • In-memory immediate cache retention, the place the cached prefixes are maintained for like 5-10 minutes and as much as 1 hour, and
    • Prolonged immediate cache retention (solely obtainable for particular fashions), permitting for an extended retention of the cached prefix, as much as a most of 24 hours.

    However let’s take a better look!

    We will see all these in observe with the next minimal Python instance, making requests to the OpenAI API, utilizing Immediate Caching, and the cooking prompts talked about earlier. I added a moderately massive shared prefix to my prompts, in order to make the results of caching extra seen:

    from openai import OpenAI
    api_key = "your_api_key"
    shopper = OpenAI(api_key=api_key)
    
    prefix = """
    You're a useful cooking assistant.
    
    Your activity is to recommend easy, sensible dinner concepts for busy folks.
    Comply with these tips fastidiously when producing strategies:
    
    Normal cooking guidelines:
    - Meals ought to take lower than half-hour to arrange.
    - Substances needs to be straightforward to seek out in a daily grocery store.
    - Recipes ought to keep away from overly complicated methods.
    - Favor balanced meals together with greens, protein, and carbohydrates.
    
    Formatting guidelines:
    - All the time return a numbered listing.
    - Present 5 strategies.
    - Every suggestion ought to embody a brief rationalization.
    
    Ingredient tips:
    - Favor seasonal greens.
    - Keep away from unique components.
    - Assume the person has primary pantry staples reminiscent of olive oil, salt, pepper, garlic, onions, and pasta.
    
    Cooking philosophy:
    - Favor easy dwelling cooking.
    - Keep away from restaurant-level complexity.
    - Give attention to meals that individuals realistically prepare dinner on weeknights.
    
    Instance meal types:
    - pasta dishes
    - rice bowls
    - stir fry
    - roasted greens with protein
    - easy soups
    - wraps and sandwiches
    - sheet pan meals
    
    Eating regimen concerns:
    - Default to wholesome meals.
    - Keep away from deep frying.
    - Favor balanced macronutrients.
    
    Extra directions:
    - Hold explanations concise.
    - Keep away from repeating the identical components in each suggestion.
    - Present selection throughout the meal strategies.
    
    """ * 80   
    # big prefix to ensure i get the 1000 one thing token threshold for activating immediate caching
    
    prompt1 = prefix + "What ought to I prepare dinner for dinner?"

    after which for the immediate 2

    prompt2 = prefix + "What ought to I prepare dinner for lunch?"
    
    response2 = shopper.responses.create(
        mannequin="gpt-5.2",
        enter=prompt2
    )
    
    print("nResponse 2:")
    print(response2.output_text)
    
    print("nUsage stats:")
    print(response2.utilization)

    So, for immediate 2, we’d be solely billed the remaining, non-identical a part of the immediate. That might be the enter tokens minus the cached tokens: 20,014 – 19,840 = solely 174 tokens, or in different phrases, 99% much less tokens.

    In any case, since OpenAI imposes a 1,024 token minimal threshold for activating immediate caching and the cache might be preserved for a most of 24 hours, it turns into clear that these price advantages might be obtained in observe solely when working AI functions at scale, with many lively customers performing many requests day by day. Nonetheless, as defined for such instances, the Immediate Caching characteristic can present substantial price and time advantages for LLM-powered functions.


    On my thoughts

    Immediate Caching is a robust optimization for LLMs that may considerably enhance the effectivity of AI functions each when it comes to price and time. By reusing earlier computations for equivalent immediate prefixes, the mannequin can skip redundant calculations and keep away from repeatedly processing the identical enter tokens. The result’s sooner responses and decrease prices, particularly in functions the place massive components of prompts—reminiscent of system directions or retrieved context—stay fixed throughout many requests. As AI programs scale and the variety of LLM calls will increase, these optimizations change into more and more essential.


    Cherished this publish? Let’s be pals! Be part of me on:

    📰Substack 💌 Medium 💼LinkedIn ☕Buy me a coffee!

    All pictures by the creator, besides talked about in any other case.



    Source link

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Editor Times Featured
    • Website

    Related Posts

    A Gentle Introduction to Stochastic Programming

    April 30, 2026

    Proxy-Pointer RAG: Multimodal Answers Without Multimodal Embeddings

    April 30, 2026

    DeepSeek’s new AI model is rolling out quietly, not to the Wall Street market shock

    April 30, 2026

    System Design Series: Apache Flink from 10,000 Feet, and Building a Flink-powered Recommendation Engine

    April 30, 2026

    Agentic AI: How to Save on Tokens

    April 29, 2026

    4 YAML Files Instead of PySpark: How We Let Analysts Build Data Pipelines Without Engineers

    April 29, 2026

    Comments are closed.

    Editors Picks

    Brain-inspired AI chip could save 70% energy

    April 30, 2026

    Liquid Instruments jags more taxpayer funding in $70 million Series C

    April 30, 2026

    MAGA Is Confused About ‘Animal Farm’

    April 30, 2026

    Meta says it might be forced to withdraw its apps from New Mexico if a judge orders it to adopt the state’s proposed safety features (Thomas Barrabi/New York Post)

    April 30, 2026
    Categories
    • Founders
    • Startups
    • Technology
    • Profiles
    • Entrepreneurs
    • Leaders
    • Students
    • VC Funds
    About Us
    About Us

    Welcome to Times Featured, an AI-driven entrepreneurship growth engine that is transforming the future of work, bridging the digital divide and encouraging younger community inclusion in the 4th Industrial Revolution, and nurturing new market leaders.

    Empowering the growth of profiles, leaders, entrepreneurs businesses, and startups on international landscape.

    Asia-Middle East-Europe-North America-Australia-Africa

    Facebook LinkedIn WhatsApp
    Featured Picks

    When Will the US Finally Get $15K EVs?

    November 15, 2025

    Best Internet Providers in Davenport, Iowa

    August 15, 2024

    As startups flood the market with AI shopping agents, Amazon is playing defense by blocking agents’ access to its site and investing heavily in its own tools (Annie Palmer/CNBC)

    December 26, 2025
    Categories
    • Founders
    • Startups
    • Technology
    • Profiles
    • Entrepreneurs
    • Leaders
    • Students
    • VC Funds
    Copyright © 2024 Timesfeatured.com IP Limited. All Rights.
    • Privacy Policy
    • Disclaimer
    • Terms and Conditions
    • About us
    • Contact us

    Type above and press Enter to search. Press Esc to cancel.