Edit

Valkey

ValkeyChatHistoryProvider persists .NET agent conversation history in Valkey lists. It works with Valkey and compatible Redis OSS servers without requiring a search module.

This integration uses the conversation-storage pattern: it reloads exact messages rather than extracting or retrieving semantic memories.

This integration stores the full transcript; it doesn't extract semantic memories or provide vector retrieval.

Install the packages

dotnet add package Microsoft.Agents.AI.Valkey --prerelease
dotnet add package Valkey.Glide

Configure persistent history

Create the Valkey connection, choose a conversation key in the state initializer, and attach the provider through ChatHistoryProvider.

var endpoint = Environment.GetEnvironmentVariable("FOUNDRY_PROJECT_ENDPOINT") ?? throw new InvalidOperationException("FOUNDRY_PROJECT_ENDPOINT is not set.");
var deploymentName = Environment.GetEnvironmentVariable("FOUNDRY_MODEL") ?? "gpt-5.4-mini";
var valkeyConnection = Environment.GetEnvironmentVariable("VALKEY_CONNECTION") ?? "localhost:6379";

var connection = await ConnectionMultiplexer.ConnectAsync(valkeyConnection);

Console.WriteLine("=== ValkeyChatHistoryProvider — Persistent Chat History ===\n");

var historyProvider = new ValkeyChatHistoryProvider(
    connection,
    _ => new ValkeyChatHistoryProvider.State($"sample-{Guid.NewGuid():N}"),
    new ValkeyChatHistoryProviderOptions
    {
        KeyPrefix = "sample_chat",
        MaxMessages = 20
    });

AIAgent historyAgent = new AIProjectClient(new Uri(endpoint), new DefaultAzureCredential())
    .AsAIAgent(new ChatClientAgentOptions()
    {
        ChatOptions = new() { ModelId = deploymentName, Instructions = "You are a helpful assistant that remembers our conversation." },
        ChatHistoryProvider = historyProvider
    });

AgentSession session1 = await historyAgent.CreateSessionAsync();
Console.WriteLine(await historyAgent.RunAsync("Hello! My name is Alex and I'm a software engineer.", session1));
Console.WriteLine(await historyAgent.RunAsync("I'm working on a project using Valkey for caching.", session1));
Console.WriteLine(await historyAgent.RunAsync("What do you remember about me?", session1));

var messageCount = await historyProvider.GetMessageCountAsync(session1);
Console.WriteLine($"\n  Stored {messageCount} messages in Valkey.\n");

KeyPrefix separates application data, and MaxMessages bounds the retained transcript. Use an application-owned stable conversation ID when history must be resumed after a restart.

Production considerations

  • Use encrypted connections, authenticated users, and network isolation.
  • Define persistence and eviction policies that match your durability requirements.
  • Store conversation identifiers in trusted server-side state and verify ownership before loading history.
  • Use separate key prefixes or deployments when tenant isolation requires it.

Next steps