| Name | Type | Description |
|---|---|---|
store_name* | str | Azure AI Memory store name. |
scope* | str | Memory scope identifier. |
update_every_n_turns | int | Default: 1Number of agent turns to buffer before flushing. |
roles | Sequence[Literal['user', 'assistant']] | Default: ('user',) |
project_endpoint | Optional[str] | Default: None |
credential | Optional[TokenCredential] | Default: None |
update_delay | Optional[int] | Default: 0 |
messages_key | str | Default: 'messages' |
Middleware that periodically extracts memories from turns into Azure AI Memory.
This middleware collects messages from the agent's state after each turn and
sends them to Azure AI Memory in batches. It supports both user and assistant
messages, configurable by the roles parameter. Use the memory store
configuration to control which memories are stored and how. To retrieve those
memories back into your agent, use the :class:AzureAIMemoryRetrieverTool
in your agent.
Example usage:
from langchain.agents import create_agent
from langchain.chat_models import init_chat_model
from langchain_azure_ai.agents.middleware import AzureAIMemoryMiddleware
memory_middleware = AzureAIMemoryMiddleware(
store_name="my-memory-store",
scope="my-scope",
update_every_n_turns=1,
roles=["user", "assistant"],
project_endpoint="https://resource.services.ai.azure.com/api/projects/my-project",
credential=DefaultAzureCredential(),
)
tools = [memory_middleware.get_retriever_tool()]
agent = create_agent(
model=init_chat_model("azure_ai:gpt-5"),
tools=tools,
middleware=[memory_middleware],
)
This class doesn't create the memory store itself, so make sure to create the specified memory store before using the middleware.
Message roles to remember. Allowed values are "user" and
"assistant". Defaults to ("user",). Pass
["user", "assistant"] to remember both roles.
Azure AI Foundry project endpoint.
Token credential used to authenticate to Azure services.
Optional update delay for memory store updates.
Key in the agent state where the message list is stored. Defaults to "messages".