← Back
Milan Jovanovic December 1, 2026 20m

Semantic Caching in .NET With Redis Vector Search and Agent Playbooks

Read full transcript 16 segments
  1. I have a prediction for what's going to happen with integrating AI into your applications. We will all have to be more mindful of costs and token spend as everything is going to become more expensive with more usage. So in this video, I want to show you how we can use Redis to control our token spend with some of the very cool features that Redis now offers. Here's a high-level diagram of the system that I built for this video, and I want to walk you through what's happening here so that you can understand the problem that we have and what are some approaches that we can use to solve it. So I'm building a support application where users ask some questions to our system and I'm going to use Redis to improve the performance in terms of token spend for my application. So one of the first things we can do is use the question itself as the cache entry, and this allows us to do an exact match search inside of Redis and figure out if we already saw this question before. If that's the case, then we don't have to spend any tokens and we can just return the cached answer, provided that the question itself is something that we can safely cache and return. If we can't find an exact match, then we can also use Redis, but in this case, I'm going to leverage vector search support that it has and figure out if the question that is being asked is semantically

  2. similar to a question that I already have an answer for. Now if that is true, I'm also going to do another check and that is if the question being asked and the one that I have a cached answer for are semantically the same question. You'll see in a demo later why this is important. And if both of these queries return a positive result, then I know I can safely return a cached answer. And if all else fails, then I can call my AI model which knows how to come up with the answer for the question. But in this case, have to pay the full price for my model call, which is exactly what I'm trying to avoid, but this also allows me to cache the answer for further use if it's safe to reuse. So that is the high-level flow. Now I just want to briefly show you my Redis instance, which is running in Redis Cloud, who is also the sponsor of today's video. Here is my database instance inside of the cloud console, and for this demo I'm going to use the free offering, which gives me 30 MB of memory to use.

  3. However, you can also create a new instance and choose between one of their managed options. And the reason I want to use Redis Cloud is because I want to show you some options that we have when it comes to Redis Iris, which is a set of services that are intended to be used by AI and agents. Inside of Redis Iris, they have a couple of services like Intent Router, Agent Playbooks, Agent Memory, Context Gateway, a couple of services in beta such as Lens and Agent Mesh. And what I want to use is Agent Playbooks. This allows me to create a set of version skills and FAQs that my agent can reuse to reduce the token spend inside of my application. So the whole idea is to use Redis just the same as we were using it when building data-driven applications. We would cache frequently used data to reduce the load on our database, and now we can also use Redis to cache frequent AI prompts and reduce the load on our large language models. And if you want to get started with Agent Playbooks, go ahead and click the link that's going to be in the pinned comment right under this video.

  4. So let me jump into the code and show you how all of this fits together. I'm going to make this whole thing open source, and you're going to find it from the description under this video if you want to give it a try. You can run the whole thing locally. You don't need to have Redis Cloud. It also works with a local Redis instance, and the core of my system is this class called a Chat Service. So how our UI looks like is one of our users asks a question, and then we feed that question into the chat service. Now this has a couple of dependencies and I'm going to walk you through some of the ones that are important, but the core of this entire class can be summed up in this one method called reply async, which takes in the chat request and it returns a chat response. Now the chat response is interesting because this is what flows through the diagram that I showed you earlier and it consists of four separate steps and all of these steps are attempts to fetch the answer and if either step returns null, then we delegate to the next step in the chain.

  5. So we first try to fetch the answer using agent playbook. Now this is something that I'm going to demo near the end of the video. Then we have our exact match inside of the cache and if this has the response we are looking for, then we return that. Then we've got our semantic search option leveraging Redis vector search and finally, if all else fails, then we're going to delegate to my AI model and for this I have a locally running model inside of Ollama and I'm using a open weight model to answer the questions inside of this application. So let me walk you through this entire class. It's almost 300 lines, but I will keep it focused to the most important parts. Now this part here is for telemetry. We're not really interested in that. Let's review the individual methods that we're going to call. So this one is going to send a request to the agent playbook service. When you create a playbook, you also get the connection info, the endpoint where you're going to send the requests and your credentials and you also connect it to your specific database where all of the data is stored. So if this returns the response, we also check it against a local LLM call, which is fairly quick and efficient and then we return a response that we present inside of the user interface. If that fails, then we can do an exact cache lookup. So we just hash the incoming question and if we already cached this question before, we just return it. The third method is the semantic caching approach where we first

  6. have to use our local AI model to perform an embedding on the question. After we embed the question, we do a vector search using the nearest neighbor algorithm and we return exactly one nearest neighbor. Now we also have to check if this is close enough where we can control a configurable value representing a threshold or how close our question is to the one that we have cached in the database. We also do a quick check if this is the same question and I'm going to show you exactly the problem this solves later in the demo and then we can return a response. And finally, if all else fails, we can ask the model and it has a way to come up with the answer because we configure what are the available tools and questions that we can answer using an AI agent and in the end, if we determine that this answer is also reusable, then we're going to create a cache entry and store that for further reuse. So that's the crux of the business logic that I have inside of this application and the rest of it is mostly supporting stuff.

  7. For example, the answer cache class is responsible for interacting with my database. When the application starts, I call this method called create index to define a vector index inside of Redis. And this is going to add an entry every time I add a value for a specific key prefix. And if you're not familiar with this, what it does is it defines an index entry and it allows me to filter based on the couple of keys that I define. For example, I want to be able to filter by tenant, by the revision, and by some configuration value that I set. And then I also want to store vector data so that I can do vector search. These filters here are important because most systems out there are multi-tenant and we really don't have the luxury of returning the wrong question to the wrong tenant. What else is inside of here? There's a method for finding our exact value which uses the incoming question and our scope where the scope consists of our tenant ID, the revision for the tenant's configuration, and it turns all of that into a cache key, and we can check if this key exists inside of our cache or not. We also have this part here, which is responsible for performing vector search, but we also filter the index space based on the tenant revision and configuration values, and then we perform K-nearest neighbors using the vector representing our incoming question, and we return the value as a cosine distance. The smaller the distance is, the more semantically

  8. similar our incoming query vector is to the value that we already have inside of our vector index. And one last thing I have is a helper method for storing the value inside of my cache, and what I'm doing here is storing the value and also giving it a preconfigured expiration time, and this helps me to naturally remove cached values from the cache. Then we have this same question check, and what this solves is semantically similar questions that may not have the same answer. So I've got a system prompt here that tells the model to compare two questions and determine if they would be satisfied with the same answer. I tell it to reply with yes or no, and in the response I check if I got a yes, and I return this back inside of our decision chain, which is inside of our chat service that I covered at the start. And lastly, I have the support agent, which is what covers the expensive path where we interact with our model running in Ollama. How I implemented this is it has to ask the system for any available tools and then use those tools to fetch the data and produce an answer based on the data it gets back. So the model isn't allowed to query my database or anything like that. Instead, it delegates this question to my system, and based on the system's response, it determines what is the most appropriate answer. And this is configured inside of support tools where I have two essentially, and the one that we are

  9. interested in is getting the support policy. And this configures some common support questions that we expect to get. For example, what is the number of rows that our tenants are allowed to export, what is the log retention, the API key rotation policy, and we have this other topic that's used to flag anything that doesn't fall into the previous free. So I showed you the code. Now let's look at a demo to see how all of these pieces fit together. Here's the simple UI I scaffolded for this. And let me just briefly explain what you're actually looking at here. First, we've got our cache mode, which is if we are caching or not, if we are doing exact match search, or if we are doing semantic search. Then I can choose between one of the two preconfigured dummy tenants. I can also configure the maximum distance that I want to support for vector search. And remember that the smaller distances, the closer these answers are to each other. And then if I want to use the yes no check using an AI model. And finally, if I want to use Redis agent playbooks to produce my answer out of a set of predefined answers that I've set up inside of the agent playbook service.

  10. So let's first turn off caching and I want to ask how many rows can I export? Now this is configured inside of the tenant policy, which says that the export limit is 10,000 and the retention period is 30 days for any data that they can produce a report for. So if I send this question, you'll see that it takes a couple of moments to get the response. And in the end, we get the response back that we can export up to 10,000 rows. And the limit specifically applies to the current tenant. Now the part to the right is what I actually want to show you. You can see the individual traces that comprise our API request. And we have the first query here to Redis to get the revision for the tenant's policy. Then we are calling our large language model and I'm using Qwen 3 with 4 billion parameters. And we can see any tool calls that are being performed. Once this completes, you can see a couple of additional calls and what we want to do with everything that we implemented so far is reduce the cost of this query. So we have two chat calls to the large language model, one tool call, and overall the costs are 848 input tokens and 48 output tokens. So if I turn on semantic caching, tick off the check match checkbox, and I repeat the same question, how many rows can I export? You'll see that we get the answer in a couple of moments, but in this case the answer is generated, which means that we don't have it in the cache. And the costs are mostly the

  11. same, except we also have an additional embedding call because we are now using semantic search, which costs us 13 embedding tokens. And now if I repeat the same request and send the question, you'll see that we get an exact hit back because we can hash the question and determine that we already have the same value cached inside of Redis. If I take a look at the cache entries here, you can actually see what is the metadata that I'm storing. And this contains the key hash, the question that was being asked, and the answer produced by the model, also which tenant and which revision. And if any of these values changes or hash changes, so we never return the same question. And also this is naturally going to expire after the time to live expires. Now what happens if somebody comes up and paraphrases this question? For example, they ask what is the maximum number of rows in an export? This is essentially the same question. You and I can figure that out in milliseconds, but for a machine this isn't quite the case unless we use semantic search, which is what we are doing here. So if I send this question, you'll see that I get a response fairly quickly, and this is now a semantic hit.

  12. So why this happens is because these questions are semantically similar. We determine that using vector search, and the distance in this case is 0.08, which is fairly close to zero. So we can assume that these are similar questions. So in this case I'm only paying one embedding call to do the vector search, and this costs me 17 embedding tokens, and the rest of the costs are inside of my Redis request, which returns fairly quickly and costs little. Now, what happens if somebody asks a semantically similar question, but it's not something that we want to answer? For example, how many columns can I export? Semantically, rows and columns are somewhat related, and if I send this question to my system, we're going to get back a semantic hit because they are similar. However, these are not the same questions, and we definitely don't want to serve this answer for this question. And this is where my match check comes in, and if I send the same prompt again, you'll see that this time we don't get an answer because we don't have this information, right? We don't know how many columns are in an export. We only configure how many rows we can export, and we expose a tool for our model to also know about this. So in this case, we are saved by the simple yes-no check that we ask our model after performing the semantic search. And with our check turned on, and if we were to send a paraphrase question, we would still get a semantic hit because these questions are similar. And in this case, we only pay 94 additional tokens for the yes-no

  13. question that I asked the model. And finally, I want to show you how agent playbooks comes into play, pun intended. So let me jump into my Redis console. So inside of agent playbooks, you can create a new one and connect it to one of your databases, and you're going to get your access credentials so that you can actually use it. And you can choose an embedding model. In this case, they're using OpenAI's text embedding free small as a specialized model for performing the embeddings for the questions that we ask and using it for vector search. Now, I already created my playbook called support assistant and connected it to my database. This gives me an access endpoint that I use to send my queries, and they have a simple API that we call to get the answers. And inside of the registry, we can store FAQs or skills. FAQs store simple questions and answers, and skills can store more complex workflows that you want your agents to reuse, we'll be using FAQs, but let me show you how.

  14. Back in the UI, I'm going to ask this question. How do I rotate an API key? I'm going to send this, and because we don't have this cache that's going to be generated, we pay the full cost, and we now store this inside of Redis. Now, if I open up my cache entries, I can see my newly stored question and its answer, and this is a candidate for reuse because that's how I configured it in the backend. So, if I hit propose to agent playbooks, I'm going to get a new proposal there that a human can review, and then if they approve it, that becomes part of our available FAQs, and we can reuse them in the future to improve the performance of our AI prompts. And the reason this exists is because people ask many questions which can often be similar, and we can only rely on semantic similarity to determine if we want to give the same answer. So, I'm going to click propose to agent playbooks. I have the logic inside of my agent playbook client, and this exposes the search functionality which allows us to query for any published frequently asked questions, but we're actually calling the propose method, which is going to send a proposal request that a human would have to review before approving it, and this is particularly useful for reusable questions and answers. If I go into my agent playbooks on Redis Cloud and then into proposals, I can see there is a proposal inside of my inbox for the question, how do I rotate an API key? This is really a frequently asked question, and the

  15. answer that we give is to rotate an API key, create a new API key, blah blah blah. So, pretty standard stuff. We also append some metadata that we want to go along with it, and if we are happy, we can accept this as a draft FAQ, and then we have to publish it for it to actually be available from the agent playbooks API. So, now that we have one FAQ, let me show you how we actually benefit from it. If I go ahead and change tenants and I ask the same question, how do I rotate an API key, you will see that the answer is generated. And that's because I'm not using agent playbooks. However, if I check this and I resend the same question, this time we get an answer from our playbook FAQ, which also means that this is reusable between all our tenants because it's a pretty generic question. And we only pay with one playbook call and 92 tokens for the yes no check that we still keep. And because we are using semantic search on the actual question, if I were to ask a semantically similar question, for example, what's the process for rotating my API key, I would still get this satisfied from the playbook's FAQ. So the same answer also answers this question, even though it's different from the one that we have in the FAQ.

  16. They are semantically similar, and you can also see the similarity score that Redis calculated. In this case, it's 0.952 with the value of one equaling semantically identical questions. So the closer to one we are, the semantically closer those questions are and the higher the likelihood that we return an answer. Inside of agent playbooks, we can configure what is the minimum acceptable score, and by default this value is 0.7. And that is how we can use Redis to reduce the token spend inside our applications. Now I only scratched what we can do with Redis Iris. It also has a couple of other services that I invite you to explore in your own time. You can also take a look at the pinned comment under this video if you want to learn more. And the overall idea is leveraging caching, in this case caching questions that we ask to an AI model. And we improved on the semantic search implementation using the agent playbook service that allows us to give pre-approved answers to commonly asked questions. If you enjoy this video, go ahead and gently tap the like button. I would really appreciate it. Thanks a lot for watching, and until next time, stay awesome.

No summary available yet.

View original episode ↗