Tutorials ASP.NET Core with Agentic AI Tutorial
AI Memory — Complete Guide
AI Memory — 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 25 of 100
AI Memory
AI basics → Agents
AI basics · 1 — Setup · ~6 min · Module 3: Semantic Kernel
What is this?
AI memory stores conversation history, user preferences, or retrieved facts across turns. AgentNest uses SK ChatHistory plus optional vector memory for long CRM relationships.
Why should you care?
ERP assistants referencing prior invoices need persisted thread state, not stateless POST bodies.
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.Agents/Memory/ThreadMemoryStore.cs
public sealed class ThreadMemoryStore(IDistributedCache cache)
{
public async Task<ChatHistory> LoadAsync(string threadId, CancellationToken ct)
{
var json = await cache.GetStringAsync($"thread:{threadId}", ct);
return json is null ? new ChatHistory() : JsonSerializer.Deserialize<ChatHistory>(json)!;
}
public Task SaveAsync(string threadId, ChatHistory history, CancellationToken ct) =>
cache.SetStringAsync($"thread:{threadId}", JsonSerializer.Serialize(history),
new DistributedCacheEntryOptions { SlidingExpiration = TimeSpan.FromHours(8) }, ct);
}
What happened?
- ThreadMemoryStore persists SK ChatHistory in Redis with sliding expiration for active deal conversations.
- Follow the steps below — typing the code yourself is the fastest way to learn.
Practice next
- Add StackExchangeRedis cache in Program.cs.
- Generate threadId per user conversation in API.
- Load history before agent call, append assistant reply, save.
- Encrypt serialized history with IDataProtector per tenant.
- Add memory summary job when messages exceed 40.
Remember
Persist ChatHistory externally for multi-turn AgentNest chat. Use sliding expiration aligned with session policy. Summarize or truncate old turns proactively.
Long CRM deal threads
AEs chat about the same enterprise deal for two weeks.
Outcome: ThreadMemoryStore restores context; summarization keeps tokens under budget.
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!