Spring AI Cosmos DB Documentation GitHub

Azure Cosmos DB Chat Memory Auto-Configuration

Spring Boot auto-configuration for the Azure Cosmos DB Chat Memory Repository. This module provides zero-config setup for persistent conversation storage when using Spring Boot.

Dependency

<dependency>
    <groupId>com.azure.spring.ai</groupId>
    <artifactId>spring-ai-autoconfigure-model-chat-memory-repository-cosmos-db</artifactId>
    <version>1.0.0-SNAPSHOT</version>
</dependency>

This transitively brings in the spring-ai-model-chat-memory-repository-cosmos-db module.

Configuration Properties

Add these to your application.properties or application.yml:

spring.ai.chat.memory.repository.cosmosdb.endpoint=https://your-account.documents.azure.com:443/
spring.ai.chat.memory.repository.cosmosdb.database-name=springai
spring.ai.chat.memory.repository.cosmosdb.container-name=chat_memory
spring.ai.chat.memory.repository.cosmosdb.connection-mode=gateway

Authentication

Key-based (set the key property):

spring.ai.chat.memory.repository.cosmosdb.key=your-cosmos-db-key

Azure Identity (recommended for production — omit the key property):

When no key is provided, the auto-configuration uses DefaultAzureCredential for authentication via managed identity, service principal, or other Azure credential sources.

Usage

Once configured, simply inject ChatMemoryRepository:

import org.springframework.ai.chat.memory.ChatMemoryRepository;
import org.springframework.beans.factory.annotation.Autowired;

@Autowired
private ChatMemoryRepository chatMemoryRepository;
💡 Tip: The auto-configuration will create the database and container if they don't exist. It also registers before Spring AI's default ChatMemoryAutoConfiguration so the Cosmos DB repository takes precedence.

Auto-Configured Beans

The following beans are auto-configured (all @ConditionalOnMissingBean, so you can override any of them):

BeanDescription
CosmosAsyncClientCosmos DB async client (created when endpoint property is set)
CosmosDBChatMemoryRepositoryConfigContainer configuration (database, container, partition key)
CosmosDBChatMemoryRepositoryThe ChatMemoryRepository implementation

Full Property Reference

PropertyDescriptionDefault
spring.ai.chat.memory.repository.cosmosdb.endpointAzure Cosmos DB endpoint URI(required)
spring.ai.chat.memory.repository.cosmosdb.keyPrimary or secondary key(uses DefaultAzureCredential if absent)
spring.ai.chat.memory.repository.cosmosdb.connection-modeConnection mode (gateway or direct)gateway
spring.ai.chat.memory.repository.cosmosdb.database-nameDatabase namespringai
spring.ai.chat.memory.repository.cosmosdb.container-nameContainer namechat_memory
spring.ai.chat.memory.repository.cosmosdb.partition-key-pathPartition key path/conversationId

Manual Configuration

If you prefer to configure the chat memory repository manually (without auto-configuration), see the Chat Memory page.