You are building a Python support assistant with Microsoft Agent Framework and a chat model deployed in Microsoft Foundry.
- An Azure Cosmos DB for NoSQL account, database and container for chat history already exist.
- The assistant must keep the full message transcript of every conversation in that existing container.
- Saving and restoring session identifiers between application restarts is handled by separate application code and is out of scope.
- Extracting long-term facts about users is not required.
- The application must authenticate to Azure Cosmos DB with Microsoft Entra ID. Account keys are not allowed.
Which actions should you perform, in order, to have the assistant persist its conversation history in Azure Cosmos DB?
You didn't answer this one.
CosmosHistoryProvider persists complete conversation messages, whereas CosmosMemoryContextProvider supplies extracted long-term knowledge. Initialize the history provider with an Azure credential, then attach it to the agent through context_providers. Create the session from that agent and await each run with the same session to persist the conversation. The provider, agent and credential remain within their asynchronous context managers during these operations.
- Step 1: Open an async context for CosmosHistoryProvider as history_provider, using the account endpoint, database name, container name and an async-managed AzureCliCredential. The open context makes the initialized provider available for the agent.
- Step 2: Create an Agent with a FoundryChatClient, set context_providers=[history_provider] and default_options={"store": False}, and enter the agent's async context manager. The agent needs the provider initialized in step 1. These settings attach Cosmos DB history and disable service-side response storage.
- Step 3: Call agent.create_session() to create a session. create_session is a method of the agent built in step 2, so the agent must already exist.
- Step 4: For each user message, execute await agent.run(message, session=session) while the context managers remain open. Each run needs the session from step 3; awaiting the run allows the agent and history provider to complete their work.
- Not needed: Configure CosmosMemoryContextProvider to persist the full message transcript. The memory provider extracts and retrieves long-term knowledge. CosmosHistoryProvider persists complete messages.
- Not needed: Pass the Azure Cosmos DB account key as the credential argument of CosmosHistoryProvider. The Python provider accepts an account key, but the scenario forbids keys and requires Microsoft Entra ID authentication.
- Azure Cosmos DBMicrosoft Learn