Use storage in your agent

Storage is a critical component of Microsoft 365 Agents SDK. It lets agents persist conversation state, user data, and other information across sessions. The SDK supports various storage options, including:

  • In-memory storage
  • Azure Cosmos DB
  • Azure Blob Storage
  • Custom storage providers

Key storage options

The Agents SDK provides several built-in storage providers, each with its own use cases and benefits. You can choose the one that best fits your agent's needs. You can also implement your own custom storage provider.

  1. Memory storage

    • Suitable for testing and development purposes.
    • Data is cleared when the agent restarts, so it's unsuitable for production.
    • Data is only available on the web app instance, so it's unsuitable when running in a cluster.
  2. Azure Cosmos DB

    • A globally distributed, multimodel database ideal for production agents.
    • Supports partitioned storage for scalability and performance.
  3. Azure Blob Storage

    • Optimized for storing unstructured data like text or binary files.
    • Commonly used for agent state and transcript storage.
  4. Custom storage options by implementing IStorage

Using different storage providers

Memory storage

All samples use MemoryStorage. This storage is volatile and suitable for development and testing only. For production scenarios, use a more durable storage option like Azure Cosmos DB or Azure Blob Storage.

In Program.cs, register MemoryStorage:

builder.Services.AddSingleton<IStorage, MemoryStorage>();

Azure CosmosDb storage

  1. Add a package dependency for Microsoft.Agents.Storage.CosmosDb.

  2. In Program.cs, add (or replace existing) IStorage registration with:

    builder.Services.AddSingleton<IStorage>(sp =>
    {
          var options = new CosmosDbPartitionedStorageOptions()
          {
             CosmosDbEndpoint = "your-cosmosdb-endpoint",
             DatabaseId = "your-database-id",
             ContainerId = "your-container-id",
    
             // Get a TokenCredential from your defined Connections
             TokenCredential = sp.GetService<IConnections>().GetConnection("ServiceConnection").GetTokenCredential()
          };
    
          return new CosmosDbPartitionedStorage(options);
    });
    
  3. Learn more in CosmosDbPartitionedStorageOptions.

Azure blob storage

  1. Add a package dependency for Microsoft.Agents.Storage.Blobs.

  2. In Program.cs, add (or replace existing) IStorage registration with:

    builder.Services.AddSingleton<IStorage>(sp =>
    {
       // Get a TokenCredential from your defined Connections
       var tokenCredential = sp.GetService<IConnections>().GetConnection("ServiceConnection").GetTokenCredential();
    
       return new BlobsStorage(
          new Uri("{{your-blobs-storage-endpoint}}/agent-state"),
          tokenCredential);
    });