Integrate Mem0 with Google Agent Development Kit for persistent memory across multi-agent workflows.
Integrate Mem0 with Google ADK (Agent Development Kit), an open-source framework for building multi-agent workflows. This integration enables agents to access persistent memory across conversations, enhancing context retention and personalization.
The following example demonstrates creating an ADK agent with automatic Mem0 memory:
import asynciofrom google.adk.agents import LlmAgentfrom google.adk.runners import Runnerfrom google.adk.sessions import InMemorySessionServicefrom google.adk.tools import load_memoryfrom google.genai.types import Content, Partfrom mem0_memory_service import Mem0MemoryServicefrom memory_callbacks import save_session_to_memorymemory_service = Mem0MemoryService()session_service = InMemorySessionService()agent = LlmAgent( name="personal_assistant", model="gemini-2.0-flash", instruction="""You are a helpful personal assistant. Relevant memories from past conversations are provided to you automatically. Use them to personalize your responses.""", description="A personal assistant that remembers user preferences and past interactions", tools=[load_memory], after_agent_callback=save_session_to_memory,)runner = Runner( agent=agent, session_service=session_service, memory_service=memory_service, app_name="memory_assistant",)async def chat(user_input: str, user_id: str) -> str: session = await session_service.create_session( app_name="memory_assistant", user_id=user_id, ) content = Content(role="user", parts=[Part(text=user_input)]) async for event in runner.run_async(user_id=user_id, session_id=session.id, new_message=content): if event.is_final_response() and event.content and event.content.parts: return event.content.parts[0].text return "No response generated"if __name__ == "__main__": print(asyncio.run(chat( "I love Italian food and I'm planning a trip to Rome next month", user_id="alice", ))) print(asyncio.run(chat( "Any food recommendations for my trip?", user_id="alice", )))
Because memory_service is passed to the Runner, every agent in the hierarchy shares the same memory automatically. Only the root coordinator needs the auto-save callback — ADK fires it once when the full turn completes:
import asynciofrom google.adk.agents import LlmAgentfrom google.adk.runners import Runnerfrom google.adk.sessions import InMemorySessionServicefrom google.adk.tools.agent_tool import AgentToolfrom google.adk.tools import load_memoryfrom google.genai.types import Content, Partfrom mem0_memory_service import Mem0MemoryServicefrom memory_callbacks import save_session_to_memorymemory_service = Mem0MemoryService()session_service = InMemorySessionService()travel_agent = LlmAgent( name="travel_specialist", model="gemini-2.0-flash", instruction="""You are a travel planning specialist. Relevant memories about the user's travel preferences are provided automatically. Use them to make personalized recommendations.""", description="Specialist in travel planning and recommendations", tools=[load_memory],)health_agent = LlmAgent( name="health_advisor", model="gemini-2.0-flash", instruction="""You are a health and wellness advisor. Relevant memories about the user's health goals are provided automatically. Use them to give personalized advice.""", description="Specialist in health and wellness advice", tools=[load_memory],)coordinator = LlmAgent( name="coordinator", model="gemini-2.0-flash", instruction="""You are a coordinator that delegates requests to specialist agents. For travel-related questions, delegate to the travel specialist. For health-related questions, delegate to the health advisor. Relevant memories about the user are provided automatically.""", description="Coordinates requests between specialist agents", tools=[ load_memory, AgentTool(agent=travel_agent, skip_summarization=False), AgentTool(agent=health_agent, skip_summarization=False), ], after_agent_callback=save_session_to_memory,)runner = Runner( agent=coordinator, session_service=session_service, memory_service=memory_service, app_name="specialist_system",)async def chat_with_specialists(user_input: str, user_id: str) -> str: session = await session_service.create_session( app_name="specialist_system", user_id=user_id, ) content = Content(role="user", parts=[Part(text=user_input)]) async for event in runner.run_async(user_id=user_id, session_id=session.id, new_message=content): if event.is_final_response() and event.content and event.content.parts: return event.content.parts[0].text return "No response generated"if __name__ == "__main__": response = asyncio.run(chat_with_specialists("Plan a healthy meal for my Italy trip", user_id="alice")) print(response)
Automatic Memory Injection: ADK’s built-in load_memory tool searches Mem0 at the start of each turn and injects relevant memories directly into the agent context — no prompt instructions needed.
Automatic Session Saving: The save_session_to_memory callback persists every completed turn to Mem0 without any manual calls.
Native ADK Integration: Mem0MemoryService implements ADK’s BaseMemoryService and integrates via the Runner — works natively across the entire agent hierarchy.
User Scoping: user_id is passed automatically from the ADK session context, ensuring memories are always scoped to the correct user.
Multi-Agent Support: A single Mem0MemoryService instance shared through the Runner gives all agents — coordinators and specialists — access to the same user memory.
InMemorySessionService stores sessions in memory and is intended for prototyping. For production, use a persistent session service and clean up sessions when they are no longer needed.
By implementing Mem0MemoryService as an ADK BaseMemoryService, you get persistent, user-scoped memory across single agents and complex multi-agent hierarchies with minimal code. Memory injection and session saving happen automatically, keeping your agent prompts clean and your token usage efficient.
Healthcare Agent Cookbook
Build HIPAA-compliant healthcare agents with Google ADK