AI agents become more useful when they can maintain session information between requests.
A user might start a conversation, ask a follow-up question several minutes later, or return to the application after a longer period. If the agent cannot recover the required session state, it has to reconstruct the conversation from the beginning.
Google Agent Development Kit, commonly called Google ADK, provides tools for building and managing agent-based applications. MongoDB Atlas can be used as a persistent data store for agent session information.
This combination is useful when an application needs agent sessions to survive application restarts, work across multiple application instances, and retain structured conversation state.
The important design decision is to separate the agent's execution logic from the persistence layer. The agent handles reasoning and tasks, while MongoDB stores the information that needs to survive between requests.
What Is an Agent Session?
An agent session represents the state of an interaction between a user and an agent.
It can contain information such as:
Session ID
User ID
Current conversation
Agent state
Tool results
Workflow state
Session metadataFor example:
{
"sessionId": "session-1001",
"userId": "user-501",
"state": {
"currentTask": "order-support",
"orderId": "ORD-1024"
}
}The exact information stored depends on the application.
A simple chatbot may only need conversation history, while a business workflow may need several additional fields.
Why Store Agent Sessions in MongoDB Atlas?
An application can keep session state in application memory, but that approach becomes unreliable when the application runs across multiple instances.
Consider this architecture:
User
|
v
Load Balancer
|
+----> Application Instance A
|
+----> Application Instance BIf the session is stored only inside Instance A, the next request may reach Instance B and lose access to that state.
MongoDB provides a shared persistence layer:
+-------------------+
| Application A |
+---------+---------+
|
+---------v---------+
| MongoDB Atlas |
| Agent Sessions |
+---------^---------+
|
+---------+---------+
| Application B |
+-------------------+Both application instances can retrieve the same session.
This is especially useful for applications deployed in distributed environments.
How Google ADK and MongoDB Fit Together
The responsibilities can be separated into three layers:
User Request
|
v
Google ADK Agent
|
+---- Agent logic
+---- Tools
+---- Session handling
|
v
Session Persistence
|
v
MongoDB AtlasGoogle ADK manages the agent workflow.
MongoDB Atlas provides persistent storage for session-related information.
This separation allows the application to scale its agent runtime independently from its session data.
Designing the MongoDB Session Document
MongoDB uses documents, which makes it possible to store related session information together.
A basic document might look like this:
{
"_id": "session-1001",
"userId": "user-501",
"agentId": "support-agent",
"state": {
"currentTask": "order-support",
"orderId": "ORD-1024"
},
"messages": [
{
"role": "user",
"content": "Where is my order?"
},
{
"role": "assistant",
"content": "Your order is currently being shipped."
}
],
"createdAt": "2026-11-05T10:00:00Z",
"updatedAt": "2026-11-05T10:05:00Z"
}This structure keeps session information associated with a single session.
However, conversation history can become large. For long-running applications, it may be better to separate session metadata from message history instead of allowing one document to grow indefinitely.
Connecting to MongoDB Atlas
A Node.js application can connect to MongoDB using the MongoDB driver.
A simplified example looks like this:
import { MongoClient } from "mongodb";
const client = new MongoClient(process.env.MONGODB_URI);
await client.connect();
const db = client.db("agentdb");
const sessions = db.collection("sessions");The connection string should be stored securely in the application's environment or secret-management system.
Do not place database credentials directly inside source code.
Creating a Session
When a new agent interaction starts, the application can create a session document.
const session = {
_id: crypto.randomUUID(),
userId: userId,
agentId: "support-agent",
state: {
currentTask: "general-support"
},
messages: [],
createdAt: new Date(),
updatedAt: new Date()
};
await sessions.insertOne(session);The generated session ID can then be returned to the client or associated with the authenticated user session.
The client sends the session ID with later requests.
Loading a Session
When a user sends another request, the application retrieves the session.
const session = await sessions.findOne({
_id: sessionId,
userId: userId
});Checking both the session ID and user ID is important.
The application should not assume that possession of a session identifier automatically grants access to that session.
If the session does not exist, the application can create a new session or return an appropriate response.
Updating Agent State
Agent state changes as the workflow progresses.
For example:
await sessions.updateOne(
{
_id: sessionId,
userId: userId
},
{
$set: {
"state.currentTask": "order-tracking",
"state.orderId": "ORD-1024",
updatedAt: new Date()
}
}
);The application does not need to replace the entire document.
Updating only the required fields reduces unnecessary data movement.
Storing Conversation Messages
If the application needs recent conversation history, messages can be stored with the session.
For example:
await sessions.updateOne(
{
_id: sessionId,
userId: userId
},
{
$push: {
messages: {
role: "user",
content: userMessage,
createdAt: new Date()
}
},
$set: {
updatedAt: new Date()
}
}
);The same pattern can be used for the agent response.
However, continuously appending messages to one document is not always suitable for long-running sessions.
Avoiding Oversized Session Documents
A long-running AI agent may generate hundreds or thousands of messages.
Storing all messages inside one MongoDB document can eventually create an oversized document and make every session read more expensive.
A better architecture can separate the data:
sessions
|
+---- Session metadata
+---- Current agent state
session_messages
|
+---- Individual conversation messagesA session document might contain:
{
"_id": "session-1001",
"userId": "user-501",
"agentId": "support-agent",
"state": {
"currentTask": "order-tracking"
}
}
Join the conversation! Your thoughts help the community grow.