Prompt caching can reduce LLM token costs by up to 90% and latency by up to 80%, but it only works when the beginning of the prompt matches exactly; to maximize cache hits, developers should structure prompts with the most stable content (system instructions, few-shot examples) at the top, followed by user-specific information, and place dynamic or session-specific content (working memory, last n messages) at the bottom, as changes to earlier tokens invalidate the entire cache while changes to later tokens only invalidate the portion after the change point.
Deep Dive
Prerequisite Knowledge
- No data available.
Where to go next
- No data available.
Deep Dive
Make Your Mastra Agent Cheaper and Faster with Prompt CachingAdded:
Prompt caching can reduce token cost by 90% but here's the thing most people get it wrong. They assume that because prompt caching works automatically with open AI, you don't have [music] to think about it. But what if I told you you can optimize your agents to hit the cash more often, save money, [music] and reduce latency by up to 80%.
I'm going to show you how with Mastra but first we need to understand the fundamentals of prompt caching.
Providers like open AI charge based on the number of tokens they process. With 5.5 you pay five bucks per million in and 30 bucks per million out. It makes sense to pay for output that obviously takes computes and creates the value we're willing to pay for but why charge for inputs separately? Turns out input tokens also use computes because every token has to be preprocessed through the model to produce the KV representations used by the models attention mechanism.
The good news is if open AI has already seen and processed the exact same prompt prefix in the last 10 minutes, it may be able to skip doing that expensive work again because the result of processing those tokens has already been computed.
And to their credit open AI pass those savings back to us and charge 90% less for cached input tokens. This also reduces latency especially for longer prompts.
So how does prompt caching work and what does it look like in practice? It depends on your provider. With open AI caching is enabled automatically for prompts that are 1024 tokens or longer.
Here for example, I have a Mastra agent with a long knowledge base system instruction. It's about 10,000 tokens.
When I run the agent from a cold start, I pay for all 10,000 input tokens.
OpenAI hasn't seen this exact prefix recently, so there's nothing in the cache to reuse.
That said, if I send a follow-up message, I only pay full price for the new input tokens in that last message.
The roughly 10,000 tokens of context from earlier are cached, and they are reused at a much lower cost. Now, here's the most important part, and really my whole motivation for this video.
Prompt caching only works when the beginning of the prompt matches exactly.
That is to say, the cache only applies up to the first changed token. So, if you change something right at the beginning of the prefix, you invalidate the cache entirely.
If you change something later in the prefix, you may still get a partial cache hit for everything before that point, while everything after it still has to be processed as new. Project Discovery, the team behind Neqo, an autonomous security testing platform, were only getting a 7% cache hit because their working memory changed on nearly every step, which kept invalidating the bulk of their cache. Once they fixed that, their cache hit rate and savings increased dramatically.
So, how do you get this right the first time and avoid wasting money? My first tip is to avoid putting dynamic values like time or the request ID at the top of the system prompt. In this case, as soon as the clock ticks just 1 minute, the beginning of the prompt changes, and that effectively invalidates the cache every 60 seconds. Additionally, I recommend you structure your prompts so that the most static context goes at the top, and the most variable context goes at the bottom, maximizing cache reuse.
Suppose you insert dynamic data that changes occasionally, like working memory near the top of the prompt. When that memory changes, everything after it is no longer cached. So, in my case, the large stable knowledge base contacts comes after the working memory, which means that when the working memory changes, I invalidate the knowledge base contacts and with it the bulk of my cache.
That's no good.
The takeaway here is to always put the most stable content first and the more variable content towards the end.
By making this small change, when the working memory updates, only the cache from that point onward is invalidated and we preserve the bulky parts of the cache we really care about.
Zooming out a little bit here, it is helpful to think about your prompt as a series of blocks.
At the top, you have ultra stable contacts, like system instructions or few-shot examples that never change. In theory, this block should fill the cache and stay warm during business hours as users keep hitting it, so you only need to pay for it once. In practice, cache hits are not guaranteed. There are lots of reasons you might miss the cache that are out of your control, but we are going to control what we can. So, next, add stable user-specific information in this next block. These values change from user to user, but they don't change over time, so they can be cached across user sessions.
Towards the bottom, add session or task-specific contacts, like memories, retrieval data, and intermediate state that changes frequently.
Of course, you'll likely want to include the last n messages as well, maybe the last 20 or something, but make sure that does go at the bottom because it is a sliding window. Once you pass that n message threshold, maybe 20, this block gets invalidated with every new message, so it's a bit volatile. I hope you enjoyed this quick video and now have a solid understanding of prompt caching and the most important optimizations to consider.
Prompt caching is not unique to OpenAI, okay? Anthropic, for example, supports it, too. Although each provider have their own mechanism, [music] their own pricing structure, and their own nuance as well.
The good thing is, now you understand the fundamentals, you can refer to their documentation or interrogate an LLM about the nitty-gritty.
I've been Alex Booker at Maestro. Thank you for watching.
Related Videos
Agentforce NOW AMA: Build with React and Salesforce Multi-Framework
SalesforceDevs
490 viewsโข2026-05-28
How agent o11y differs from traditional o11y โ Phil Hetzel, Braintrust
aiDotEngineer
450 viewsโข2026-05-28
WEB TECHNOLOGIES UNIT-2 | Degree 4th sem BCOM Computers web technologies unit-2 full explanation๐ฏโ
LearnwithSahera
1K viewsโข2026-05-29
More tests are always better? How to use AI to identify tests that bring little value
Alliance4Qualification
335 viewsโข2026-05-29
Search Algorithms Explained in 60 Seconds! ๐ค๐จ
samarthtuliofficial
218 viewsโข2026-06-01
People of Game of Thrones using JavaScript DOM
AltCampus
296 viewsโข2026-05-30
Introduction to Problem Solving Part - 1 | Lecture 1 | Intermediate DSA
ascensionix
107 viewsโข2026-05-29
So What's Odin Lang Even Good For
TechOverTea
131 viewsโข2026-06-01











