{"id":12986,"date":"2026-03-22T21:39:18","date_gmt":"2026-03-22T21:39:18","guid":{"rendered":"https:\/\/techtrendfeed.com\/?p=12986"},"modified":"2026-03-22T21:39:18","modified_gmt":"2026-03-22T21:39:18","slug":"immediate-caching-with-the-openai-api-a-full-fingers-on-python-tutorial","status":"publish","type":"post","link":"https:\/\/techtrendfeed.com\/?p=12986","title":{"rendered":"Immediate Caching with the OpenAI API: A Full Fingers-On Python tutorial"},"content":{"rendered":"<p> <br \/>\n<\/p>\n<div>\n<p class=\"wp-block-paragraph\"><a rel=\"nofollow\" target=\"_blank\" data-type=\"link\" data-id=\"https:\/\/towardsdatascience.com\/why-care-about-promp-caching-in-llms\/\" href=\"https:\/\/towardsdatascience.com\/why-care-about-promp-caching-in-llms\/\">In my earlier put up<\/a>,  Immediate Caching \u2014 what it&#8217;s, the way it works, and the way it can prevent some huge cash and time when working AI-powered apps with excessive visitors. In right this moment\u2019s put up, I stroll you thru implementing Immediate Caching particularly utilizing OpenAI\u2019s API, and we focus on some widespread pitfalls.<\/p>\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-dotted\"\/>\n<h2 class=\"wp-block-heading\">A quick reminder on Immediate Caching<\/h2>\n<p class=\"wp-block-paragraph\">Earlier than getting our fingers soiled, let\u2019s briefly revisit what precisely the idea of Immediate Caching is. Immediate Caching is a performance offered in frontier mannequin API companies just like the <a rel=\"nofollow\" target=\"_blank\" data-type=\"link\" data-id=\"https:\/\/developers.openai.com\/api\/docs\/guides\/prompt-caching\" href=\"https:\/\/developers.openai.com\/api\/docs\/guides\/prompt-caching\">OpenAI API<\/a> or <a rel=\"nofollow\" target=\"_blank\" data-type=\"link\" data-id=\"https:\/\/platform.claude.com\/docs\/en\/build-with-claude\/prompt-caching\" href=\"https:\/\/platform.claude.com\/docs\/en\/build-with-claude\/prompt-caching\">Claude\u2019s API,<\/a> that enables caching and reusing components of the LLM\u2019s enter which might be repeated steadily. Such repeated components could also be system prompts or directions which might be handed to the mannequin each time when working an AI app, together with another variable content material, just like the consumer\u2019s question or data retrieved from a information base. To have the ability to hit cache with immediate caching, the repeated components of the immediate should be at the start of it, particularly, a <strong><em>immediate prefix<\/em><\/strong>. As well as, to ensure that immediate caching to be activated, this prefix should exceed a sure <strong><em>threshold <\/em><\/strong>(e.g., for OpenAI the prefix needs to be greater than 1,024 tokens, whereas Claude has totally different minimal cache lengths for various fashions). So far as these two situations are happy \u2014 repeated tokens as a prefix exceeding the dimensions threshold outlined by the API service and mannequin \u2014 caching may be activated to attain economies of scale when working AI apps.<\/p>\n<p class=\"wp-block-paragraph\">In contrast to caching in different elements in a RAG or different AI app, immediate caching operates on the token stage, within the inner procedures of the LLM. Particularly, LLM inference takes place in two steps:<\/p>\n<ul class=\"wp-block-list\">\n<li class=\"wp-block-list-item\"><strong>Pre-fill<\/strong>, that&#8217;s, the LLM takes into consideration the consumer immediate to generate the primary token, and<\/li>\n<li class=\"wp-block-list-item\"><strong>Decoding<\/strong>, that&#8217;s, the LLM recursively generates the tokens of the output one after the other<\/li>\n<\/ul>\n<p class=\"wp-block-paragraph\">Briefly, immediate caching shops the computations that happen within the pre-fill stage, so the mannequin doesn\u2019t must recompute it once more when the identical prefix reappears. Any computations happening within the decoding iterations section, even when repeated, aren\u2019t going to be cached.<\/p>\n<p class=\"wp-block-paragraph\">For the remainder of the put up, I can be focusing solely on using immediate caching within the OpenAI API.<\/p>\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-dotted\"\/>\n<h2 class=\"wp-block-heading\">What concerning the OpenAI API?<\/h2>\n<p class=\"wp-block-paragraph\">In OpenAI\u2019s API, immediate caching was initially launched on the <a rel=\"nofollow\" target=\"_blank\" href=\"https:\/\/openai.com\/index\/api-prompt-caching\/\" data-type=\"link\" data-id=\"https:\/\/openai.com\/index\/api-prompt-caching\/\">1st of October 2024<\/a>. Initially, it supplied a 50% low cost on the cached tokens, however these days, this low cost goes as much as 90%. On prime of this, by hitting their immediate cache, further financial savings on latency may be achived as much as 80%.<\/p>\n<p class=\"wp-block-paragraph\">When immediate caching is activated, the API service makes an attempt to hit the cache for a submitted request by routing the submitted immediate to an acceptable machine, the place the respective cache is predicted to exist. That is known as the Cache Routing, and to do that, the API service usually makes use of a\u00a0hash of the primary 256 tokens of the immediate. <\/p>\n<p class=\"wp-block-paragraph\">Past this, their API additionally permits for explicitly defining a the\u00a0<a rel=\"nofollow\" target=\"_blank\" href=\"https:\/\/developers.openai.com\/api\/docs\/api-reference\/responses\/create#responses-create-prompt_cache_key\"><code>prompt_cache_key<\/code><\/a> parameter within the API request to the mannequin. That may be a single key defining which cache we&#8217;re referring to, aiming to additional enhance the possibilities of our immediate being routed to the right machine and hitting cache.<\/p>\n<p class=\"wp-block-paragraph\">As well as, OpenAI API offers two distinct sorts of caching with reference to length, outlined via the <code>prompt_cache_retention<\/code> parameter.  These are:<\/p>\n<ul class=\"wp-block-list\">\n<li class=\"wp-block-list-item\"><strong>In-memory immediate cache retention<\/strong>: That is basically the default sort of caching, out there for all fashions for which immediate caching is obtainable. With in-memory cache, cached information stay energetic for a interval of 5-10 minutes beteen requests.<\/li>\n<li class=\"wp-block-list-item\"><strong>Prolonged immediate cache retention<\/strong>: This out there for <a rel=\"nofollow\" target=\"_blank\" href=\"https:\/\/developers.openai.com\/api\/docs\/guides\/prompt-caching\" data-type=\"link\" data-id=\"https:\/\/developers.openai.com\/api\/docs\/guides\/prompt-caching\">particular fashions<\/a>. Prolonged cache permits for preserving information in cache for loger and as much as a most of 24 hours.<\/li>\n<\/ul>\n<p class=\"wp-block-paragraph\">Now, with reference to how a lot all these value, OpenAI expenses the identical per enter (non cached) token, both we&#8217;ve immediate caching activated or not. If we handle to hit cache succesfully, we&#8217;re billed for the cached tokens at a drastically discounted value, with a reduction as much as 90%. Furthermore, the value per enter token stays the identical each for the in reminiscence and prolonged cache retention.<\/p>\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-dotted\"\/>\n<h2 class=\"wp-block-heading\">Immediate Caching in Observe<\/h2>\n<p class=\"wp-block-paragraph\">So, let\u2019s see how immediate caching truly works with a easy Python instance utilizing OpenAI\u2019s API service. Extra particularly, we&#8217;re going to do a sensible situation the place a <strong>lengthy system immediate (prefix)<\/strong> is reused throughout a number of requests. In case you are right here, I assume you have already got your OpenAI <a rel=\"nofollow\" target=\"_blank\" href=\"https:\/\/openai.com\/api\/\" data-type=\"link\" data-id=\"https:\/\/openai.com\/api\/\">API key<\/a> in place and have put in the required libraries. So, the very first thing to do could be to import the <code>OpenAI<\/code> library, in addition to <code>time<\/code> for capturing latency, and initialize an occasion of the OpenAI shopper:<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-python\">from openai import OpenAI\nimport time\n\nshopper = OpenAI(api_key=\"your_api_key_here\")<\/code><\/pre>\n<p class=\"wp-block-paragraph\">then we will outline our prefix (the tokens which might be going to be repeated and we&#8217;re aiming to cache):<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-python\">long_prefix = \"\"\"\nYou're a extremely educated assistant specialised in machine studying.\nReply questions with detailed, structured explanations, together with examples when related.\n\n\"\"\" * 200  <\/code><\/pre>\n<p class=\"wp-block-paragraph\">Discover how we artificially enhance the size (multiply with 200) to ensure the 1,024 token caching threshold is met. Then we additionally  arrange a timer in order to measure our latency financial savings, and we&#8217;re lastly able to make our name:<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-python\">begin = time.time()\n\nresponse1 = shopper.responses.create(\n    mannequin=\"gpt-4.1-mini\",\n    enter=long_prefix + \"What's overfitting in machine studying?\"\n)\n\nfinish = time.time()\n\nprint(\"First response time:\", spherical(finish - begin, 2), \"seconds\")\nprint(response1.output[0].content material[0].textual content)<\/code><\/pre>\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/contributor.insightmediagroup.io\/wp-content\/uploads\/2026\/03\/Screenshot-2026-03-18-at-10.44.32-PM.png\" alt=\"\" class=\"wp-image-650856\"\/><\/figure>\n<p class=\"wp-block-paragraph\">So, what can we anticipate to occur from right here? For fashions from gpt-4o and newer, immediate caching is activated by default, and since our 4,616 enter tokens are properly above the 1,024 prefix token threshold, we&#8217;re good to go. Thus, what this request does is that it initially checks if the enter is a cache hit (it&#8217;s not, since that is the primary time we do a request with this prefix), and since it&#8217;s not, it processes your complete enter after which caches it. Subsequent time we ship an enter that matches the preliminary tokens of the cached enter to some extent, we&#8217;re going to get a cache hit. Let\u2019s test this in apply by making a second request with the identical prefix:<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-python\">begin = time.time()\n\nresponse2 = shopper.responses.create(\n    mannequin=\"gpt-4.1-mini\",\n    enter=long_prefix + \"What's regularization?\"\n)\n\nfinish = time.time()\n\nprint(\"Second response time:\", spherical(finish - begin, 2), \"seconds\")\nprint(response2.output[0].content material[0].textual content)<\/code><\/pre>\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/contributor.insightmediagroup.io\/wp-content\/uploads\/2026\/03\/Screenshot-2026-03-18-at-10.52.17-PM.png\" alt=\"\" class=\"wp-image-650861\"\/><\/figure>\n<p class=\"wp-block-paragraph\">Certainly! The second request runs considerably sooner (23.31 vs 15.37 seconds). It is because the mannequin has already made the calculations for the cached prefix and solely must course of from scratch the brand new half, \u201cWhat&#8217;s regularization?\u201d. Consequently, through the use of immediate caching, we get considerably decrease latency and lowered value, since cached tokens are discounted.<\/p>\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-dotted\"\/>\n<p class=\"wp-block-paragraph\">One other factor talked about within the OpenAI documentation we\u2019ve already talked about is the <code>prompt_cache_key<\/code> parameter. Particularly, based on the documentation, we will explicitly outline a immediate cache key when making a request, and on this manner outline the requests that want to make use of the identical cache. Nonetheless, I attempted to incorporate it in my instance by appropriately adjusting the request parameters, however didn\u2019t have a lot luck:<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-python\">response1 = shopper.responses.create(\n    prompt_cache_key = 'prompt_cache_test1',\n    mannequin=\"gpt-5.1\",\n    enter=long_prefix + \"What's overfitting in machine studying?\"\n)<\/code><\/pre>\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/contributor.insightmediagroup.io\/wp-content\/uploads\/2026\/03\/Screenshot-2026-03-19-at-7.32.31-AM-1024x57.png\" alt=\"\" class=\"wp-image-650895\"\/><\/figure>\n<p class=\"has-text-align-left wp-block-paragraph\">\ud83e\udd14<\/p>\n<p class=\"wp-block-paragraph\">It appears that evidently whereas <code>prompt_cache_key<\/code> exists within the API capabilities, it&#8217;s not but uncovered within the Python SDK. In different phrases, we can&#8217;t explicitly management cache reuse but, however it&#8217;s slightly computerized and best-effort.<\/p>\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-dotted\"\/>\n<h2 class=\"wp-block-heading\">So, what can go improper?<\/h2>\n<p class=\"wp-block-paragraph\">Activating immediate caching and truly hitting the cache appears to be sort of easy from what we\u2019ve stated to this point. So, what might go improper, leading to us lacking the cache? Sadly, lots of issues. As easy as it&#8217;s, immediate caching requires lots of totally different assumptions to be in place. Lacking even a kind of conditions goes to end in a cache miss. However let\u2019s take a greater look!<\/p>\n<p class=\"wp-block-paragraph\">One apparent miss is having a prefix that&#8217;s lower than the edge for activating immediate caching, particularly, lower than 1,024 tokens. Nonetheless, that is very simply solvable \u2014 we will at all times simply artificially enhance the prefix token depend by merely multiplying by an acceptable worth, as proven within the instance above.<\/p>\n<p class=\"wp-block-paragraph\">One other factor could be silently breaking the prefix. Particularly, even once we use persistent directions and system prompts of acceptable measurement throughout all requests, we should be exceptionally cautious to not break the prefixes by including any variable content material at the start of the mannequin\u2019s enter, earlier than the prefix. That may be a assured approach to break the cache, irrespective of how lengthy and repeated the next prefix is. Regular suspects for falling into this pitfall are dynamic information, as an example, appending the consumer ID or timestamps at the start of the immediate. Thus, a greatest apply to observe throughout all AI app growth is that any dynamic content material ought to at all times be appended on the finish of the immediate \u2014 by no means at the start.<\/p>\n<p class=\"wp-block-paragraph\">Finally, it&#8217;s price highlighting that immediate caching is simply concerning the pre-fill section \u2014 decoding is rarely cached. Which means even when we impose on the mannequin to generate responses following a particular template, that beggins with sure mounted tokens, these tokens aren\u2019t going to be cached, and we&#8217;re going to be billed for his or her processing as normal.<\/p>\n<p class=\"wp-block-paragraph\">Conversely, for particular use instances, it doesn\u2019t actually make sense to make use of immediate caching. Such instances could be extremely dynamic prompts, like chatbots with little repetition, one-off requests, or real-time customized programs.<\/p>\n<p class=\"has-text-align-center wp-block-paragraph\"><strong>. . .<\/strong><\/p>\n<h2 class=\"wp-block-heading\">On my thoughts<\/h2>\n<p class=\"wp-block-paragraph\">Immediate caching can considerably enhance the efficiency of AI functions each by way of value and time. Particularly when trying to scale AI apps immediate caching comes extremelly useful, for sustaining value and latency in acceptable ranges. <\/p>\n<p class=\"wp-block-paragraph\">For OpenAI\u2019s API immediate caching is activated by default and prices for enter, non-cached tokens are the identical both we activate immediate caching or not. Thus, one can solely win by activating immediate caching and aiming to hit it in each request, even when they don\u2019t succeed.<\/p>\n<p class=\"wp-block-paragraph\">Claude additionally offers intensive performance on immediate caching via their API, which we&#8217;re going to be exploring intimately in a future put up.<\/p>\n<p class=\"wp-block-paragraph\">Thanks for studying! \ud83d\ude42<\/p>\n<p class=\"has-text-align-center wp-block-paragraph\"><strong>. . .<\/strong><\/p>\n<p class=\"wp-block-paragraph\"><em>Cherished this put up? Let\u2019s be mates! Be part of me on: <\/em><\/p>\n<p class=\"wp-block-paragraph\">\ud83d\udcf0<strong><em><a rel=\"nofollow\" target=\"_blank\" href=\"https:\/\/datacream.substack.com\/\" data-type=\"link\" data-id=\"https:\/\/datacream.substack.com\/\">Substack<\/a><\/em><\/strong> \ud83d\udc8c<em>\u00a0<strong><a rel=\"nofollow\" target=\"_blank\" href=\"https:\/\/medium.com\/@m.mouschoutzi\">Medium<\/a><\/strong><\/em> \ud83d\udcbc<em><strong><a rel=\"nofollow\" target=\"_blank\" href=\"https:\/\/www.linkedin.com\/in\/mariamouschoutzi\/\" data-type=\"link\" data-id=\"https:\/\/www.linkedin.com\/in\/mariamouschoutzi\/\">LinkedIn<\/a> <\/strong><\/em>\u2615<em><strong><a rel=\"nofollow\" target=\"_blank\" href=\"http:\/\/buymeacoffee.com\/mmouschoutzi\">Purchase me a espresso<\/a>!<\/strong><\/em><\/p>\n<p class=\"wp-block-paragraph\"><em><strong>All pictures by the creator, besides talked about in any other case.<\/strong><\/em><\/p>\n<\/div>\n\n","protected":false},"excerpt":{"rendered":"<p>In my earlier put up, Immediate Caching \u2014 what it&#8217;s, the way it works, and the way it can prevent some huge cash and time when working AI-powered apps with excessive visitors. In right this moment\u2019s put up, I stroll you thru implementing Immediate Caching particularly utilizing OpenAI\u2019s API, and we focus on some widespread [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":12988,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[55],"tags":[664,7278,1813,660,82,152,1258,3028],"class_list":["post-12986","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-machine-learning","tag-api","tag-caching","tag-full","tag-handson","tag-openai","tag-prompt","tag-python","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/techtrendfeed.com\/index.php?rest_route=\/wp\/v2\/posts\/12986","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techtrendfeed.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techtrendfeed.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techtrendfeed.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/techtrendfeed.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=12986"}],"version-history":[{"count":1,"href":"https:\/\/techtrendfeed.com\/index.php?rest_route=\/wp\/v2\/posts\/12986\/revisions"}],"predecessor-version":[{"id":12987,"href":"https:\/\/techtrendfeed.com\/index.php?rest_route=\/wp\/v2\/posts\/12986\/revisions\/12987"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techtrendfeed.com\/index.php?rest_route=\/wp\/v2\/media\/12988"}],"wp:attachment":[{"href":"https:\/\/techtrendfeed.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=12986"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techtrendfeed.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=12986"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techtrendfeed.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=12986"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}<!-- This website is optimized by Airlift. Learn more: https://airlift.net. Template:. Learn more: https://airlift.net. Template: 69d9690a190636c2e0989534. Config Timestamp: 2026-04-10 21:18:02 UTC, Cached Timestamp: 2026-05-08 08:22:19 UTC -->