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.

Python SDK
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."
JavaScript SDK
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.

Python SDK
# 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
JavaScript SDK
// 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 session

How 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.

Python SDK
# 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%",
    },
)
JavaScript SDK
// 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.

FeatureMemoryKnowledgeData Store
Data typeConversation history, learned factsDocuments, URLs (unstructured)Tables, rows (structured)
SourceAuto-extracted from conversationsUploaded files and URLsAgent-created schemas
RetrievalAutomatic (session + semantic)RAG (vector similarity)SQL queries via tools
PersistenceSession or cross-sessionPermanent until deletedPermanent until deleted
Best forUser preferences, conversation contextReference docs, manuals, FAQsBusiness 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.

Python SDK
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,
})
JavaScript SDK
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,
  },
});
SettingDescriptionDefault
enabledEnable session memory for the agenttrue
semantic_memoryEnable cross-session semantic memorytrue
session_windowNumber of messages kept in session context50