Blog Post

Microsoft Foundry Blog
6 MIN READ

Teaching AI to Remember: Exploring Memory Store in Microsoft Foundry

vmanasa's avatar
vmanasa
Icon for Microsoft rankMicrosoft
May 21, 2026

Imagine interacting with an AI assistant that remembers your preferences - the tools you use, the projects you're working on, or even how you like your coffee. In many AI systems today, maintaining this kind of long-term memory isn’t straightforward. While models can understand context within a single conversation, remembering important information across multiple interactions requires additional infrastructure.

That’s exactly the problem Memory Store in Microsoft Foundry is designed to solve.

In this blog, we’ll:

  • Understand what Memory Store is 
  • See why it’s powerful for real-world agents
  • Build a tiny but cool demo with Python

What Is Memory Store in Microsoft Foundry?

Memory Store (preview) in Foundry Agent Service is a managed, long‑term memory system for your AI agents.

In simple terms:

Your agent can remember things across conversations, sessions, and even devices.

It’s not just chat history, it’s structured memory that can store:

  • User preferences
  • Important facts
  • Summarized conversation context

Memory Store enables agent continuity and personalization while keeping memories secure and scoped per user.

Key Concepts

Memory Store

  • Its a container where memories live. Memory stores act as persistent storage, defining which types of information are relevant to each agent.
  • Best practice: Create one memory store per agent to keep memory boundaries clean and retrieval efficient.

Scope

Defines who the memory belongs to.

  • Can be a custom ID (user_123)
  • Or auto-mapped using {{$userId}} from authentication headers

This ensures privacy and isolation by design. 

Think of it as saying: "This memory belongs only to this user, not everyone."

Memory Types

  • User profile memory 
    • This captures stable, long‑term user information, such as:

      • Preferences (e.g., dark roast coffee)
      • Habits
      • Repeated choices 
    •  These memories are injected as static context at the start of a conversation.
  • Chat summary memory 
    • This captures condensed insights from conversations.

 

Now that we understand the core concepts, let's see Memory Store in action. We'll build a simple agent that remembers a user's coffee preference and recalls it in a later conversation.

Mini Demo: Teaching an Agent to Remember

We’ll create:

  1. A memory store
  2. An agent with memory
  3. A conversation where the agent learns something
  4. A new conversation where it remembers it 

Prerequisites (Quick Checklist)

You’ll need:

  • An Azure subscription
  • A Microsoft Foundry project
  • Deployed models:
    • chat deployment model like gpt-4.1 or similar
    • text-embedding-3-small (embeddings)
  • Python 3.8+
Environment Setup

First, install the required dependencies: pip install azure-ai-projects azure-identity openai python-dotenv

Create a .env file with your Azure AI Projects connection string: FOUNDRY_PROJECT_ENDPOINT=your_connection_string_here

 

Here's the full implementation that creates a memory-enabled coffee agent:

import os
import time
from dotenv import load_dotenv

from azure.ai.projects import AIProjectClient
from azure.ai.projects.models import (
    MemoryStoreDefaultDefinition,
    MemoryStoreDefaultOptions,
    MemorySearchPreviewTool,
    PromptAgentDefinition,
)
from azure.identity import DefaultAzureCredential

# ---------------------------------------------------
# Load environment variables
# ---------------------------------------------------
load_dotenv()

FOUNDRY_PROJECT_ENDPOINT = os.getenv("FOUNDRY_PROJECT_ENDPOINT")
if not FOUNDRY_PROJECT_ENDPOINT:
    raise ValueError("FOUNDRY_PROJECT_ENDPOINT not set in .env file")

# ---------------------------------------------------
# Create Foundry Project Client
# ---------------------------------------------------
project_client = AIProjectClient(
    endpoint=FOUNDRY_PROJECT_ENDPOINT,
    credential=DefaultAzureCredential(),
)

openai_client = project_client.get_openai_client()

# ---------------------------------------------------
# STEP 1: Create Memory Store
# ---------------------------------------------------
print("Creating memory store...")

options = MemoryStoreDefaultOptions(
    chat_summary_enabled=True,
    user_profile_enabled=True,
    user_profile_details="Avoid sensitive data like age, finance, or credentials",
)

definition = MemoryStoreDefaultDefinition(
    chat_model="gpt-4.1",
    embedding_model="text-embedding-3-small",
    options=options,
)

# Try to create memory store, or get existing one if it already exists
try:
    memory_store = project_client.beta.memory_stores.create(
        name="coffee_memory_store",
        definition=definition,
        description="Memory store for coffee preferences",
    )
    print(f"✅ Memory store created: {memory_store.name}")
except Exception as e:
    if "already exists" in str(e):
        print("Memory store already exists, using existing one...")
        # Get the existing memory store
        memory_stores = project_client.beta.memory_stores.list()
        memory_store = None
        for store in memory_stores:
            if store.name == "coffee_memory_store":
                memory_store = store
                break
        if memory_store:
            print(f"✅ Using existing memory store: {memory_store.name}")
        else:
            print("❌ Could not find existing memory store")
            raise e
    else:
        raise e

# ---------------------------------------------------
# STEP 2: Create Agent with Memory
# ---------------------------------------------------
print("Creating agent with memory...")

memory_tool = MemorySearchPreviewTool(
    memory_store_name="coffee_memory_store",
    scope="user_123",
    update_delay=1,
)

# Try to create agent, or get existing one if it already exists
try:
    agent = project_client.agents.create_version(
        agent_name="CoffeeAgent",
        definition=PromptAgentDefinition(
            model="gpt-4.1",
            instructions="You are a helpful coffee assistant with memory. You can remember customer preferences and order history across conversations. When a customer mentions their preferences, remember them for future orders.",
            tools=[memory_tool],
        ),
    )
    print(f"✅ Agent created: {agent.name}")
except Exception as e:
    if "already exists" in str(e) or "Agent with name" in str(e):
        print("Agent already exists, using existing one...")
        # Get the existing agent
        agents = project_client.agents.list_versions(agent_name="CoffeeAgent")
        if agents:
            agent = agents[0]  # Use the first/latest version
            print(f"✅ Using existing agent: {agent.name}")
        else:
            print("❌ Could not find existing agent")
            raise e
    else:
        raise e

# ---------------------------------------------------
# STEP 3: First Conversation (Learning)
# ---------------------------------------------------
print("Starting first conversation...")

conversation = openai_client.conversations.create()

response = openai_client.responses.create(
    input="I prefer dark roast coffee",
    conversation=conversation.id,
    extra_body={
        "agent_reference": {
            "name": agent.name,
            "type": "agent_reference",
        }
    },
)

print("🧠 Agent response:", response.output_text)

print("Waiting for memory to be stored...")
time.sleep(65)  # allow debounce + memory write

# ---------------------------------------------------
# STEP 4: New Conversation (Recall)
# ---------------------------------------------------
print("Starting new conversation...")

new_conversation = openai_client.conversations.create()

new_response = openai_client.responses.create(
    input="Order my usual coffee",
    conversation=new_conversation.id,
    extra_body={
        "agent_reference": {
            "name": agent.name,
            "type": "agent_reference",
        }
    },
)

print("☕ Agent response:", new_response.output_text)

print("🎉 Memory recall successful!")
Output:
Verifying in Microsoft Foundry

You can also test and verify the memory functionality directly in the Microsoft Foundry:

  1. Navigate to your CoffeeAgent in the Foundry portal interface
  2. Open the **Memory** section — confirm "coffee_memory_store" is connected
  3. Use the Chat interface and ask: "What coffee do I prefer?"
  4. View the response - the agent will correctly recall your dark roast preference

Key Implementation Points

  1. Consistent Scoping: Use consistent user scope in memory tools for proper context association
  2. Resource Management: Handle existing resources gracefully for reusable code
  3. Memory Processing: Allow sufficient time for memory processing between conversations
  4. Agent Instructions: Include memory capabilities in agent instructions for better performance

Customization Options

You can customize the memory store for different use cases:

  1. Change memory scope: Use different user IDs for multi-user applications
  2. Update agent instructions: Tailor personality and memory usage patterns
  3. Configure update delays: Adjust 'update_delay' in the sample code based on your performance needs

Next Steps & Resources

If you want to take this further, here are a few ways to continue exploring Memory Store:

  1. What is Memory in Microsoft Foundry? : What is Memory? - Microsoft Foundry | Microsoft Learn 
  2. Create and use Memory Store : Create and Use Memory - Microsoft Foundry | Microsoft Learn 
  3. Microsoft Foundry documentation (agents, tools, APIs) :Microsoft Foundry documentation | Microsoft Learn 

 Try It Yourself

Take this demo further by:

- Storing multiple preferences (drink, time, location)

- Using dynamic scopes ({{$userId}}) for multi‑user applications

- Observing how memory evolves across multiple conversations and sessions

Build Ideas

Here are a few practical use cases to extend this pattern:

- Personalized onboarding assistant that remembers user preferences 

- Smart recommendation engine (food, tools, learning paths) 

- Customer support agent with long-term context and conversation continuity 

 

By combining memory with agent logic, you move from stateless interactions to truly personalized, context-aware experiences.

Final Thoughts

Giving AI agents memory fundamentally changes how they interact with users. Instead of treating every conversation as a fresh start, agents can build long-term understanding and personalization over time. As AI agents become more capable, memory will play a key role in making them feel less like tools and more like intelligent collaborators. If you're building agents in Foundry, Memory Store is definitely a feature worth exploring!

Happy coding, and have fun building smarter agents!🚀

Updated May 14, 2026
Version 1.0

1 Comment

  • Interesting read! Thank you for the small demo, will make sure to try it.