Memory & Context
AgentBackend agents can maintain context within and across conversations. Session memory tracks the current conversation, semantic memory persists learned facts across sessions, and context injection lets you pass user-specific data with each request.
Session Memory
Session memory automatically maintains conversation history within a session. Each message and response is stored so the agent can reference earlier parts of the conversation. Sessions are identified by a session_id that you provide.
from agentbackend import AgentBackend
ab = AgentBackend("ak_your_api_key")
conv = ab.agent("agent_id").conversation(session_id="user_123")
# First message
conv.send("My name is Sarah and I work at Acme Corp.")
# Later in the same session — agent remembers the context
conv.send("What company do I work at?")
# Agent responds: "You work at Acme Corp."import { AgentBackend } from "agentbackend";
const ab = new AgentBackend("ak_your_api_key");
const conv = ab.agent("agent_id").conversation({ sessionId: "user_123" });
// First message
await conv.send("My name is Sarah and I work at Acme Corp.");
// Later in the same session — agent remembers the context
await conv.send("What company do I work at?");
// Agent responds: "You work at Acme Corp."Semantic Memory
Semantic memory extracts key facts, preferences, and context from conversations and stores them long-term. When a new session starts, relevant memories are automatically retrieved and injected into the agent's context. This allows agents to remember users across separate conversations.
# Semantic memory is automatically extracted from conversations
# and persisted across sessions for the same user
# Session 1
conv1 = ab.agent("agent_id").conversation(session_id="user_123_s1")
conv1.send("I prefer dark mode and metric units.")
# Session 2 (new session, same user context)
conv2 = ab.agent("agent_id").conversation(session_id="user_123_s2")
conv2.send("Show me the weather forecast.")
# Agent remembers preference for metric units from previous session// Semantic memory is automatically extracted from conversations
// and persisted across sessions for the same user
// Session 1
const conv1 = ab.agent("agent_id").conversation({ sessionId: "user_123_s1" });
await conv1.send("I prefer dark mode and metric units.");
// Session 2 (new session, same user context)
const conv2 = ab.agent("agent_id").conversation({ sessionId: "user_123_s2" });
await conv2.send("Show me the weather forecast.");
// Agent remembers preference for metric units from previous sessionHow it works: After each conversation turn, the platform analyzes the exchange and extracts noteworthy facts (preferences, names, relationships, decisions). These are stored as vector embeddings and retrieved by semantic similarity in future sessions.
Context Injection
Pass user-specific context with each request using the context parameter. This is useful for injecting business data that the agent should know about the current user, such as their role, account status, or active deals.
# Pass user-specific context with each request
response = ab.run.create(
agent_id="agent_id",
message="What deals should I follow up on?",
session_id="user_123",
context={
"user_name": "Sarah Chen",
"role": "Sales Manager",
"region": "APAC",
"active_deals": 12,
"quota_attainment": "78%",
},
)// Pass user-specific context with each request
const response = await ab.run.create({
agentId: "agent_id",
message: "What deals should I follow up on?",
sessionId: "user_123",
context: {
userName: "Sarah Chen",
role: "Sales Manager",
region: "APAC",
activeDeals: 12,
quotaAttainment: "78%",
},
});Memory vs Knowledge vs Data Store
AgentBackend offers three ways to give agents access to information. Choose the right one based on the type of data and how it should be accessed.
| Feature | Memory | Knowledge | Data Store |
|---|---|---|---|
| Data type | Conversation history, learned facts | Documents, URLs (unstructured) | Tables, rows (structured) |
| Source | Auto-extracted from conversations | Uploaded files and URLs | Agent-created schemas |
| Retrieval | Automatic (session + semantic) | RAG (vector similarity) | SQL queries via tools |
| Persistence | Session or cross-session | Permanent until deleted | Permanent until deleted |
| Best for | User preferences, conversation context | Reference docs, manuals, FAQs | Business data, CRM, inventory |
See also: Knowledge and Agent Data Store.
Configuration
Enable or disable memory per agent and configure the session window size (number of messages kept in context). Semantic memory can be toggled independently.
agent = ab.agents.create(
name="Support Agent",
instructions="You are a helpful support agent.",
memory={
"enabled": True,
"semantic_memory": True,
"session_window": 50, # Number of messages to keep in session context
},
)
# Update memory settings
ab.agents.update(agent.agent_id, memory={
"enabled": True,
"semantic_memory": False, # Disable cross-session memory
"session_window": 20,
})const agent = await ab.agents.create({
name: "Support Agent",
instructions: "You are a helpful support agent.",
memory: {
enabled: true,
semanticMemory: true,
sessionWindow: 50, // Number of messages to keep in session context
},
});
// Update memory settings
await ab.agents.update(agent.agentId, {
memory: {
enabled: true,
semanticMemory: false, // Disable cross-session memory
sessionWindow: 20,
},
});| Setting | Description | Default |
|---|---|---|
| enabled | Enable session memory for the agent | true |
| semantic_memory | Enable cross-session semantic memory | true |
| session_window | Number of messages kept in session context | 50 |