How to build an AI Agent with an intelligent memory using the Azure AI Foundry agent service.
When we think about human intelligence, memory is one of the first things that comes to mind. It’s what enables us to learn from our experiences, adapt to new situations, and make more informed decisions over time. Similarly, AI Agents become smarter with memory. For example, an agent can remember your past purchases, your budget, your preferences, and suggest gifts for your friends based on the learning from the past conversations.
Agents usually break tasks into steps (plan → search → call API → parse → write), but then they might forget what happened in earlier steps without memory. Agents repeat tool calls, fetch the same data again, or miss simple rules like “always refer to the user by their name.” As a result of repeating the same context over and over again, the agents can spend more tokens, achieve slower results, and provide inconsistent answers. You can read my other article about why memory is important for AI Agents.
In this article, we’ll explore why memory is so important for AI Agents and walk through an example of a Smart Shopping Assistant to see how memory makes it more helpful and personalized. You will learn how to integrate Memori with the Azure AI Foundry AI Agent service.
Smart Shopping Experience With Memory for an AI Agent
This demo showcases an Agent that remembers customer preferences, shopping behavior, and purchase history to deliver personalized recommendations and experiences. The demo walks through five shopping scenarios where the assistant remembers customer preferences, budgets, and past purchases to give personalized recommendations. From buying Apple products and work setups to gifts, home needs, and books, the assistant adapts to each need and suggests complementary options.
- Learns Customer Preferences: Remembers past purchases and preferences
- Provides Personalized Recommendations: Suggests products based on shopping history
- Budget-Aware Shopping: Considers customer budget constraints
- Cross-Category Intelligence: Connects purchases across different product categories
- Gift Recommendations: Suggests gifts based on the customer's history
- Contextual Conversations: Maintains shopping context across interactions
Check the GitHub repo with the full agent source code and try out the live demo.
How Smart Shopping Assistant Works
We use the Azure AI Foundry Agent Service to build the shopping assistant and added Memori, an open-source memory solution, to give it persistent memory. You can check out the Memori GitHub repo here: https://github.com/GibsonAI/memori.
We connect Memori to a local SQLite database, so the assistant can store and recall information.
You can also use any other relational databases like PostgreSQL or MySQL.
Note that it is a simplified version of the actual smart shopping assistant implementation. Check out the GitHub repo code for the full version.
from azure.ai.agents.models import FunctionTool
from azure.ai.projects import AIProjectClient
from azure.identity import DefaultAzureCredential
from dotenv import load_dotenv
from memori import Memori, create_memory_tool
# Constants
DATABASE_PATH = "sqlite:///smart_shopping_memory.db"
NAMESPACE = "smart_shopping_assistant"
# Create Azure provider configuration for Memori
azure_provider = ProviderConfig.from_azure(
api_key=os.environ["AZURE_OPENAI_API_KEY"],
azure_endpoint=os.environ["AZURE_OPENAI_ENDPOINT"],
azure_deployment=os.environ["AZURE_OPENAI_DEPLOYMENT_NAME"],
api_version=os.environ["AZURE_OPENAI_API_VERSION"],
model=os.environ["AZURE_OPENAI_DEPLOYMENT_NAME"],
)
# Initialize Memori for persistent memory
memory_system = Memori(
database_connect=DATABASE_PATH,
conscious_ingest=True,
auto_ingest=True,
verbose=False,
provider_config=azure_provider,
namespace=NAMESPACE,
)
# Enable the memory system
memory_system.enable()
# Create memory tool for agents
memory_tool = create_memory_tool(memory_system)
def search_memory(query: str) -> str:
"""Search customer's shopping history and preferences"""
try:
if not query.strip():
return json.dumps({"error": "Please provide a search query"})
result = memory_tool.execute(query=query.strip())
memory_result = (
str(result) if result else "No relevant shopping history found"
)
return json.dumps(
{
"shopping_history": memory_result,
"search_query": query,
"timestamp": datetime.now().isoformat(),
}
)
except Exception as e:
return json.dumps({"error": f"Memory search error: {str(e)}"})
...
This setup records every conversation, and user preferences are saved under a namespace called smart_shopping_assistant.
We plug Memori into the Azure AI Foundry agent as a function tool. The agent can call search_memory() to look at the shopping history each time.
...
functions = FunctionTool(search_memory)
# Get configuration from environment
project_endpoint = os.environ["PROJECT_ENDPOINT"]
model_name = os.environ["MODEL_DEPLOYMENT_NAME"]
# Initialize the AIProjectClient
project_client = AIProjectClient(
endpoint=project_endpoint, credential=DefaultAzureCredential()
)
print("Creating Smart Shopping Assistant...")
instructions = """You are an advanced AI shopping assistant with
memory capabilities. You help customers find products,
remember their preferences, track purchase history, and
provide personalized recommendations.
"""
agent = project_client.agents.create_agent(
model=model_name,
name="smart-shopping-assistant",
instructions=instructions,
tools=functions.definitions,
)
thread = project_client.agents.threads.create()
print(f"Created shopping assistant with ID: {agent.id}")
print(f"Created thread with ID: {thread.id}")
...
This integration makes the Azure-powered agent memory-aware: it can search customer history, remember preferences, and use that knowledge when responding.
Setting Up and Running AI Foundry Agent with Memory
Go to the Azure AI Foundry portal and create a project by following the guide in the Microsoft docs. Deploy a model like GPT-4o. You will need the Project Endpoint and Model Deployment Name to run the example.
1. Before running the demo, install the required libraries:
pip install memorisdk azure-ai-projects azure-identity python-dotenv
2. Set your Azure environment variables:
# Azure AI Foundry Project Configuration
export PROJECT_ENDPOINT="https://your-project.eastus2.ai.azure.com"
# Azure OpenAI Configuration
export AZURE_OPENAI_API_KEY="your-azure-openai-api-key-here"
export AZURE_OPENAI_ENDPOINT="https://your-resource.openai.azure.com/"
export AZURE_OPENAI_DEPLOYMENT_NAME="gpt-4o"
export AZURE_OPENAI_API_VERSION="2024-12-01-preview"
3. Run the demo:
python smart_shopping_demo.py
The script runs predefined conversations to show how the assistant works in real life.
Example:
Hi! I'm looking for a new smartphone. I prefer Apple products and my budget is around $1000.
The assistant responds by considering previous preferences, suggesting iPhone 15 Pro and accessories,
and remembering your price preference for the future. So next time, it might suggest AirPods Pro too.
The assistant responds by considering previous preferences, suggesting iPhone 15 Pro and accessories, and remembering your price preference for the future. So next time, it might suggest AirPods Pro too.
How Memori Helps
Memori decides which long-term memories are important enough to promote into short-term memory, so agents always have the right context at the right time. Memori adds powerful memory features for AI Agents:
- Structured memory: Learns and validates preferences using Pydantic-based logic
- Short-term vs. long-term memory: You decide what’s important to keep
- Multi-agent memory: Shared knowledge between different agents
- Automatic conversation recording: Just one line of code
- Multi-tenancy: Achieved with namespaces, so you can handle many users in the same setup.
What You Can Build with This
You can customize the demo further by:
- Expanding the product catalog with real inventory and categories that matter to your store.
- Adding new tools like “track my order,” “compare two products,” or “alert me when the price drops.”
- Connecting to a real store API (Shopify, WooCommerce, Magento, or a custom backend) so recommendations are instantly shoppable.
- Enabling cross-device memory, so the assistant remembers the same user whether they’re on web, mobile, or even a voice assistant.
- Integrating with payment and delivery services, letting users complete purchases right inside the conversation.
Final Thoughts
AI agents become truly useful when they can remember. With Memori + Azure AI Founder, you can build assistants that learn from each interaction, gets smarter over time, and deliver delightful, personal experiences.