Is Prompt Caching the new RAG?

<p>

recently, Anthropic, the company behind Claude, has announced a remarkable new feature called <a href="https://docs.anthropic.com/en/docs/build-with-claude/prompt-caching?ref=airabbit.blog" rel="nofollow">Prompt Caching</a>. This breakthrough development makes the processing of lengthy documents more affordable than ever before, and it has the potential to revolutionize how we handle vast amounts of static information in AI conversations!<br/>Let's delve into the exciting implications this has for AI applications.</p>
<h1 class="relative group flex items-center">
<a class="block pr-1.5 text-lg md:absolute md:p-1.5 md:opacity-0 md:group-hover:opacity-100 md:right-full" href="#what-is-prompt-caching" id="what-is-prompt-caching" rel="nofollow">
<span class="header-link"><svg aria-hidden="true" class="text-gray-500 hover:text-black dark:hover:text-gray-200 w-4" height="1em" preserveaspectratio="xMidYMid meet" role="img" viewbox="0 0 256 256" width="1em" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span>
</a>
<span>
		What is Prompt Caching?
	</span>
</h1>
<p>Prompt Caching involves storing the system prompt --- the static part of the conversation. This system prompt can include substantial content such as entire books, long research papers, or large codebases. Here's how it works:</p>
<ol>
<li>The system prompt is cached on the first request, incurring a one-time cost.</li>
<li>Subsequent user queries only process the dynamic user input against this cached context.</li>
<li>This approach dramatically speeds up interactions and reduces costs for repeated queries.</li>
</ol>
<h1 class="relative group flex items-center">
<a class="block pr-1.5 text-lg md:absolute md:p-1.5 md:opacity-0 md:group-hover:opacity-100 md:right-full" href="#key-points-about-prompt-caching" id="key-points-about-prompt-caching" rel="nofollow">
<span class="header-link"><svg aria-hidden="true" class="text-gray-500 hover:text-black dark:hover:text-gray-200 w-4" height="1em" preserveaspectratio="xMidYMid meet" role="img" viewbox="0 0 256 256" width="1em" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span>
</a>
<span>
		Key Points About Prompt Caching
	</span>
</h1>
<ul>
<li>System Prompt vs. User Input: The system prompt (static, cached) is separate from the user's input (dynamic, varies with each query).</li>
<li>Initial Caching Cost: The first time you cache the system prompt, it costs approximately 25% more than standard input pricing.</li>
<li>Subsequent Query Savings: After caching, processing new queries against the cached context costs only about 10% of the usual input pricing.</li>
<li>Time Limitation: The cache lasts for 5 minutes. After this period, the system prompt needs to be cached again if you want to continue using it.</li>
</ul>
<h1 class="relative group flex items-center">
<a class="block pr-1.5 text-lg md:absolute md:p-1.5 md:opacity-0 md:group-hover:opacity-100 md:right-full" href="#examples" id="examples" rel="nofollow">
<span class="header-link"><svg aria-hidden="true" class="text-gray-500 hover:text-black dark:hover:text-gray-200 w-4" height="1em" preserveaspectratio="xMidYMid meet" role="img" viewbox="0 0 256 256" width="1em" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span>
</a>
<span>
		Examples
	</span>
</h1>
<p>I made a Gradio app on HuggingFace with <a href="https://huggingface.co/spaces/airabbitX/claude_caching?ref=airabbit.blog">a simple chat interface</a> that uses the new caching API.</p>
<p>In this example, I uploaded a comprehensive manual from a Github Repo (LLAMFactory) and asked some questions.</p>
<p><a href="https://miro.medium.com/v2/resize:fit:1400/0*hZ3pHBP5ALftlKxy.png" rel="nofollow"><img alt="" src="https://miro.medium.com/v2/resize:fit:1400/0*hZ3pHBP5ALftlKxy.png"/></a></p>
<p>The system prompt is cached after the first question, so the cache is still zero.</p>
<p><a href="https://miro.medium.com/v2/resize:fit:1400/0*q-6fRy61W-2xj5Wp.png" rel="nofollow"><img alt="" src="https://miro.medium.com/v2/resize:fit:1400/0*q-6fRy61W-2xj5Wp.png"/></a></p>
<p>After that, the cached version is used, and the response is much faster and cheaper (10% of the usual cost for input tokens).</p>
<p><a href="https://miro.medium.com/v2/resize:fit:1400/0*v8ao71oDv_BAlgcZ.png" rel="nofollow"><img alt="" src="https://miro.medium.com/v2/resize:fit:1400/0*v8ao71oDv_BAlgcZ.png"/></a></p>
<h1 class="relative group flex items-center">
<a class="block pr-1.5 text-lg md:absolute md:p-1.5 md:opacity-0 md:group-hover:opacity-100 md:right-full" href="#possible-use-cases-for-prompt-caching" id="possible-use-cases-for-prompt-caching" rel="nofollow">
<span class="header-link"><svg aria-hidden="true" class="text-gray-500 hover:text-black dark:hover:text-gray-200 w-4" height="1em" preserveaspectratio="xMidYMid meet" role="img" viewbox="0 0 256 256" width="1em" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span>
</a>
<span>
		Possible Use Cases for Prompt Caching
	</span>
</h1>
<ol>
<li>Document Analysis: Cache entire books or long documents. Users can ask multiple questions about the content without reprocessing the whole text each time.</li>
<li>Code Review: Store large codebases in the cache. Developers can query about different parts of the code quickly and cheaply.</li>
<li>Research Assistance: Cache comprehensive research papers or datasets. Researchers can explore various aspects of the data without repeated processing costs.</li>
<li>Legal Document Processing: Store entire legal codes or case law databases. Lawyers can quickly query for relevant information at a fraction of the usual cost.</li>
<li>Educational Tools: Cache textbooks or course materials. Students can ask numerous questions about the content, making interactive learning more feasible and affordable.</li>
</ol>
<p>Please note that there are some limitations to keep in mind with prompt caching. The cache only remains valid for 5 minutes, and it's not yet compatible with all Claude models.</p>
<h1 class="relative group flex items-center">
<a class="block pr-1.5 text-lg md:absolute md:p-1.5 md:opacity-0 md:group-hover:opacity-100 md:right-full" href="#conclusion" id="conclusion" rel="nofollow">
<span class="header-link"><svg aria-hidden="true" class="text-gray-500 hover:text-black dark:hover:text-gray-200 w-4" height="1em" preserveaspectratio="xMidYMid meet" role="img" viewbox="0 0 256 256" width="1em" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span>
</a>
<span>
		Conclusion
	</span>
</h1>
<p>Prompt Caching is a major step forward in making AI interactions more efficient and cost-effective, particularly in applications dealing with large, static datasets. By dramatically cutting the time and cost of subsequent queries, it unlocks new possibilities for AI-driven analysis, learning, and information processing across various industries.</p>
