
Tech • AI • Robotics • Game
ADK memory can be built in three layers: short-term session context, persistent session and user-profile storage in a database, and long-term multimodal recall with Vertex AI Memory Bank.
A session is the active thread of a conversation, comparable to a messaging chat. With InMemorySessionService, creating a new session makes the agent behave as if it has never met the user before. In a travel-planning example, an agent could plan day one in Lisbon, then forget the city and prior preferences when asked to plan day two if a new session is started instead of reusing the original one.
Two core elements structure session memory: events and state. Events act like a transcript, capturing user messages, agent replies, and even media such as images, while state is a structured notebook for key values such as a restaurant name or booking number. Using state avoids forcing the model to scan many earlier messages just to recover one specific fact.
Session state can also be used to pass information between agents efficiently. In one example, Agent A identifies a restaurant and stores its output under a key such as destination, then Agent B automatically uses that variable to produce directions. This lets agents collaborate without reprocessing the full conversation history each time.
Directly modifying a session object inside tools is discouraged because those changes may not be captured in the event log and may not persist correctly. The safer pattern is to write through context.state, which allows ADK to record the change alongside the event. That design becomes critical once memory is moved from RAM into a database or longer-term storage.
Replacing in-memory storage with DatabaseSessionService lets conversations continue after an application restart. Backed by a database such as PostgreSQL, the service stores each user message, each agent response, and the latest session state on disk. When the same session ID returns, a helper such as get_or_create_session can reload the full history without changing the agent’s main logic.
InMemorySessionService is fast and simple for local testing but loses data on restart. DatabaseSessionService preserves conversations in infrastructure controlled by the developer, while VertexAiSessionService stores session data in managed Google Cloud infrastructure. The session layer is designed to be swappable, allowing the same agent behavior to run on different persistence backends.
Persistent sessions solve continuity for one conversation, but a new session ID still starts with no transcript. To handle this, a separate user-profile table can store structured preferences keyed to a user ID, such as dietary choices, interests, or transport habits. An agent can then greet a returning user in a brand-new conversation and still remember that the person is vegetarian or prefers museums.
A typical implementation exposes two tools: one to retrieve saved preferences and another to insert or update them. Clear instructions can make the agent call the recall tool at the start of a conversation, personalize its planning from the returned data, and then save any new relevant fact before finishing. In testing, a user who asked the agent to remember being vegetarian received a personalized travel response even a week later in a new session.
For richer recall, VertexAiMemoryBankService acts as a long-term archive rather than an active session log. It stores memories persistently in the cloud and supports semantic similarity search, so a query such as “two-wheeled vehicle” can retrieve a memory about a bicycle. The service can use a Gemini model to extract important facts and an embeddings model to index those memories by meaning rather than exact wording.
Memory Bank can ingest a full session with add_session_to_memory or receive content directly, including text, images, video, and audio with supporting context. With PreloadMemoryTool, the agent automatically searches for relevant memories at the start of each turn and injects them into context before answering. In one scenario, earlier media sharing led the system to infer interests in historic architecture, coastal areas, and city visits, allowing a later session to generate a tailored cultural destination recommendation.
The architecture separates working memory, persistent continuity, and long-term semantic recall into distinct layers. Together, those layers allow an AI agent to remain coherent across turns, survive restarts, and personalize future interactions using both structured preferences and multimodal past experience.
Ask a question