Skip to content

Caching

CachingConfig enables distributed caching of LLM responses. When a request matches a cached entry, the agent returns the cached response without calling the provider — reducing latency and cost for repeated or deterministic queries.

Caching is opt-in and requires an IDistributedCache implementation registered in the service provider (e.g., services.AddStackExchangeRedisCache(...) or services.AddDistributedMemoryCache()).


Configuration

Via AgentConfig

csharp
var config = new AgentConfig
{
    Caching = new CachingConfig
    {
        Enabled = true,
        CacheExpiration = TimeSpan.FromMinutes(30)
    }
};

Via builder

csharp
// Defaults (30 min TTL)
.WithCaching()

// Custom TTL
.WithCaching(TimeSpan.FromHours(1))

// Allow caching for stateful (multi-turn) conversations
.WithCaching(TimeSpan.FromHours(1), cacheStatefulConversations: true)

Properties

PropertyTypeDefaultDescription
EnabledboolfalseEnable response caching (opt-in)
CacheExpirationTimeSpan?30 minutesTTL for cached entries. null = never expire
CoalesceStreamingUpdatesbooltrueStore the final assembled response (true) rather than the raw streaming chunks
CacheStatefulConversationsboolfalseAllow caching when a ConversationId is present (stateful/multi-turn conversations)

Bypassing the Cache at Runtime

To skip the cache for a specific run, use AgentRunConfig:

csharp
var options = new AgentRunConfig { UseCache = false };
await foreach (var evt in agent.RunAsync("...", branch, options)) { }

JSON Example

json
{
    "Caching": {
        "Enabled": true,
        "CacheExpiration": "00:30:00",
        "CoalesceStreamingUpdates": true,
        "CacheStatefulConversations": false
    }
}

Setup

Register a distributed cache in your DI container before calling BuildAsync():

csharp
// In-memory (development / single-process)
services.AddDistributedMemoryCache();

// Redis (production / multi-process)
services.AddStackExchangeRedisCache(opts =>
{
    opts.Configuration = "localhost:6379";
});

var agent = await new AgentBuilder(config)
    .WithServiceProvider(services.BuildServiceProvider())
    .WithCaching(TimeSpan.FromHours(1))
    .BuildAsync();

See Also

Released under the MIT License.