Tutorials ASP.NET Core with Agentic AI Tutorial
AI Caching — Complete Guide
AI Caching — Complete Guide: free step-by-step lesson with examples, common mistakes, and interview tips — part of ASP.NET Core with Agentic AI Tutorial on Toolliyo Academy.
On this page
ASP.NET Core with Agentic AI Tutorial · Lesson 84 of 100
AI Caching
AI basics ✓ → Agents
Agents · 2 — Build · ~10 min · Module 9: AI System Design and Architecture
What is this?
AI caching stores embeddings, RAG answers, and chat completions to cut latency and cost. AgentNest caches deterministic CRM FAQs and embedding vectors for unchanged KB chunks.
Why should you care?
Reps ask the same discount policy question daily — caching saves thousands of identical GPT calls.
See it live — copy this example
Paste into an ASP.NET Core 8+ / AgentNest project, then run with dotnet run (set your API keys in user-secrets).
// AgentNest.Caching/SemanticAnswerCache.cs
public sealed class SemanticAnswerCache(IDistributedCache cache, IEmbeddingGenerator embedder)
{
public async Task<string?> TryGetAsync(string tenantId, string question, CancellationToken ct)
{
var hash = Convert.ToHexString(SHA256.HashData(Encoding.UTF8.GetBytes(tenantId + question)));
return await cache.GetStringAsync($"ans:{hash}", ct);
}
public Task SetAsync(string tenantId, string question, string answer, CancellationToken ct)
{
var hash = Convert.ToHexString(SHA256.HashData(Encoding.UTF8.GetBytes(tenantId + question)));
return cache.SetStringAsync($"ans:{hash}", answer,
new DistributedCacheEntryOptions { SlidingExpiration = TimeSpan.FromHours(6) }, ct);
}
}
What happened?
- SemanticAnswerCache keys by tenant plus question hash.
- RagPipeline checks cache before embedding search and LLM call.
Practice next
- Insert cache lookup at start of RagPipeline.AskAsync.
- TTL cache by content type — policies 6h, live CRM data 60s.
- Invalidate on KB chunk update events.
- Add semantic near-duplicate match via embedding similarity before exact hash.
- Use HybridCache for L1 in-memory plus Redis L2.
Remember
Cache RAG answers and embeddings with tenant-scoped keys. Invalidate on knowledge base updates. Track hit rate to validate savings.
Policy FAQ savings
500 reps query enterprise discount policy daily.
Outcome: Answer cache hits 92%; OpenAI spend on that FAQ drops to near zero.
Interview prep for this lesson
Practice these questions aloud after reading—each links to a full structured answer.
Sign in to ask a question or upvote helpful answers.
No questions yet — be the first to ask!