LLM Providers Supported in Mem0: How to Configure OpenAI, Anthropic, and Azure

Mem0 supports OpenAI, Azure OpenAI, and Anthropic through a unified factory pattern that maps provider names to specific implementation classes and configuration objects.

The mem0ai/mem0 repository implements a provider-agnostic LLM interface via the LlmFactory class in mem0/utils/factory.py. This architecture allows you to swap between OpenAI, Azure OpenAI, and Anthropic by changing a string identifier and provider-specific configuration, without modifying your application logic.

Supported LLM Providers in Mem0

Mem0 ships with built-in support for the three major cloud LLM providers. The factory maps string identifiers to concrete implementation classes:

Provider Factory Key Implementation Class Configuration Class
OpenAI openai mem0.llms.openai.OpenAILLM OpenAIConfig
Azure OpenAI azure_openai mem0.llms.azure_openai.AzureOpenAILLM AzureOpenAIConfig
Anthropic anthropic mem0.llms.anthropic.AnthropicLLM AnthropicConfig

Each configuration class extends BaseLlmConfig from mem0/configs/llms/base.py, ensuring consistent handling of shared parameters like model, temperature, and api_key.

How the LLM Factory Works

The LlmFactory.create(provider_name, config, **kwargs) method in mem0/utils/factory.py handles three configuration patterns:

  1. Config object: Pass an instance of OpenAIConfig, AzureOpenAIConfig, or AnthropicConfig
  2. Dictionary: Pass a dict that gets merged with kwargs and passed to the appropriate config class
  3. None: Factory instantiates a default config object for the provider

The factory uses importlib to dynamically load the implementation class, then constructs the LLM instance with the normalized configuration.

Configuring OpenAI

OpenAI Configuration Options

The OpenAIConfig class in mem0/configs/llms/openai.py extends the base configuration with OpenAI-specific parameters:

  • openai_base_url: Custom base URL for OpenAI-compatible endpoints
  • models: Model routing configuration
  • route: Routing strategy
  • openrouter_base_url: Base URL for OpenRouter integration
  • site_url and app_name: Identification for OpenRouter
  • store: Whether to store completions
  • response_callback: Callback function for responses

OpenAI Configuration Example

from mem0.utils.factory import LlmFactory
from mem0.configs.llms.openai import OpenAIConfig

# Option A: Using a config object

openai_config = OpenAIConfig(
    model="gpt-4o-mini",
    temperature=0.2,
    api_key="YOUR_OPENAI_KEY",  # Falls back to OPENAI_API_KEY env var

    openai_base_url="https://api.openai.com/v1"
)
llm = LlmFactory.create("openai", openai_config)

# Option B: Using a dictionary (ideal for JSON/YAML configs)

llm = LlmFactory.create(
    "openai",
    {
        "model": "gpt-4o-mini",
        "temperature": 0.2,
        "api_key": "YOUR_OPENAI_KEY"
    }
)

Configuring Azure OpenAI

Azure OpenAI Configuration Options

The AzureOpenAIConfig class in mem0/configs/llms/azure.py handles Azure-specific authentication and endpoint configuration through the azure_kwargs parameter:

  • api_version: Azure API version (e.g., "2023-05-15")
  • resource_name: Azure OpenAI resource name
  • deployment_id: Model deployment ID in Azure

Azure OpenAI Configuration Example

from mem0.utils.factory import LlmFactory
from mem0.configs.llms.azure import AzureOpenAIConfig

azure_config = AzureOpenAIConfig(
    model="gpt-35-turbo",
    api_key="YOUR_AZURE_KEY",  # Falls back to AZURE_OPENAI_API_KEY env var

    azure_kwargs={
        "api_version": "2023-05-15",
        "resource_name": "my-azure-openai",
        "deployment_id": "gpt-35-turbo"
    }
)
llm = LlmFactory.create("azure_openai", azure_config)

Configuring Anthropic

Anthropic Configuration Options

The AnthropicConfig class in mem0/configs/llms/anthropic.py adds Anthropic-specific endpoint configuration:

  • anthropic_base_url: Custom base URL for Anthropic API or compatible endpoints

Anthropic Configuration Example

from mem0.utils.factory import LlmFactory
from mem0.configs.llms.anthropic import AnthropicConfig

anthropic_config = AnthropicConfig(
    model="claude-3-5-sonnet-20240620",
    temperature=0.1,
    api_key="YOUR_ANTHROPIC_KEY",  # Falls back to ANTHROPIC_API_KEY env var

    anthropic_base_url="https://api.anthropic.com"
)
llm = LlmFactory.create("anthropic", anthropic_config)

Using the LLM in Your Application

All three providers implement the same LLMBase interface from mem0/llms/base.py, exposing a unified generate_response method:

messages = [
    {"role": "system", "content": "You are a helpful assistant."},
    {"role": "user", "content": "Explain the difference between a list and a tuple in Python."}
]

# Works identically regardless of provider (openai, azure_openai, or anthropic)

response = llm.generate_response(messages, temperature=0.7)
print(response)  # Returns string (or dict if response_format was set)

The concrete implementations (OpenAILLM in mem0/llms/openai.py, AzureOpenAILLM in mem0/llms/azure_openai.py, and AnthropicLLM in mem0/llms/anthropic.py) handle provider-specific client initialization, authentication, and response parsing internally.

Key Files and Architecture

Role File Description
Factory mem0/utils/factory.py LlmFactory.create() maps provider names to implementations
Base Config mem0/configs/llms/base.py BaseLlmConfig with shared parameters (model, temperature, api_key)
OpenAI Config mem0/configs/llms/openai.py OpenAIConfig with OpenAI-specific options
Azure Config mem0/configs/llms/azure.py AzureOpenAIConfig with azure_kwargs wrapper
Anthropic Config mem0/configs/llms/anthropic.py AnthropicConfig with custom base URL support
Base LLM mem0/llms/base.py LLMBase abstract class defining generate_response
OpenAI LLM mem0/llms/openai.py OpenAILLM implementation
Azure LLM mem0/llms/azure_openai.py AzureOpenAILLM implementation
Anthropic LLM mem0/llms/anthropic.py AnthropicLLM implementation

Summary

  • Mem0 supports OpenAI, Azure OpenAI, and Anthropic through a unified factory pattern in mem0/utils/factory.py.
  • Configuration is provider-specific using OpenAIConfig, AzureOpenAIConfig, or AnthropicConfig, all extending BaseLlmConfig.
  • Factory keys are "openai", "azure_openai", and "anthropic"—pass these to LlmFactory.create().
  • Environment variables are supported for API keys (OPENAI_API_KEY, AZURE_OPENAI_API_KEY, ANTHROPIC_API_KEY).
  • Unified interface: All providers implement generate_response from LLMBase, allowing seamless swapping without code changes.

Frequently Asked Questions

What LLM providers does Mem0 support?

Mem0 supports OpenAI, Azure OpenAI, and Anthropic as first-class providers, along with additional options like Ollama, Groq, AWS Bedrock, and Gemini. The core providers (OpenAI, Azure, Anthropic) are implemented in mem0/llms/openai.py, mem0/llms/azure_openai.py, and mem0/llms/anthropic.py respectively.

How do I switch between LLM providers in Mem0?

Switching providers requires changing the provider string and configuration class passed to LlmFactory.create() in mem0/utils/factory.py. Use "openai" with OpenAIConfig, "azure_openai" with AzureOpenAIConfig, or "anthropic" with AnthropicConfig. The generate_response method signature remains identical across all providers, so no application code changes are needed beyond the factory call.

Can I use environment variables for API keys in Mem0?

Yes. While you can pass api_key directly to configuration classes (OpenAIConfig, AzureOpenAIConfig, AnthropicConfig), the implementations automatically fall back to standard environment variables: OPENAI_API_KEY for OpenAI, AZURE_OPENAI_API_KEY for Azure, and ANTHROPIC_API_KEY for Anthropic.

Where is the LLM factory implemented in Mem0?

The LLM factory is implemented in mem0/utils/factory.py as the LlmFactory class. The create() method maps provider name strings to implementation classes using the provider_to_class dictionary, dynamically imports the correct module via importlib, and normalizes configuration objects using provider-specific config classes from mem0/configs/llms/.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →