AgentSea

Concept

Memory

Memory stores let an agent keep conversation context across calls, for continuity and personalization.

01Memory types

AgentSea provides four memory stores in core. Pass one to the Agent constructor as its fourth argument.

BufferMemory
Keeps the most recent messages in process memory. Best for development, testing and single-process apps.
RedisMemory
Stores conversations in Redis, so several servers can share them. Best for production.
SummaryMemory
Keeps the latest messages verbatim and summarizes older ones. Best for long conversations.
TenantBufferMemory
Buffer memory keyed by tenant, so one tenant's conversations are not returned to another. Best for multi-tenant apps.

02Buffer memory

Keeps the most recent messages. Pass a number to cap how many are stored per conversation.

TS
import { Agent, BufferMemory, AnthropicProvider, ToolRegistry } from '@lov3kaizen/agentsea-core';

// Keep at most 50 messages per conversation
const memory = new BufferMemory(50);

const agent = new Agent(
  {
    name: 'chat-agent',
    description: 'Chat assistant with short-term memory',
    model: 'claude-sonnet-4-6',
    provider: 'anthropic',
    systemPrompt: 'You are a helpful assistant.',
  },
  new AnthropicProvider(),
  new ToolRegistry(),
  memory,
);

const context = { conversationId: 'user-123', sessionData: {}, history: [] };

await agent.execute('My name is Alice', context);

// The agent loads the saved history for user-123
await agent.execute('What is my name?', context); // "Your name is Alice"

03Redis memory

Persists conversations in Redis. Use it when several servers handle the same conversations, or when history must survive a restart.

TS
import { Agent, RedisMemory, AnthropicProvider, ToolRegistry } from '@lov3kaizen/agentsea-core';

const memory = new RedisMemory({
  host: 'localhost',                   // falls back to REDIS_HOST
  port: 6379,                          // falls back to REDIS_PORT
  password: process.env.REDIS_PASSWORD, // falls back to REDIS_PASSWORD
  prefix: 'agent:',                    // optional key prefix
  ttl: 86400,                          // seconds, set on each save
});

const agent = new Agent(config, new AnthropicProvider(), new ToolRegistry(), memory);

await agent.execute('Remember: my favorite color is blue', context);

// Close the connection when the process shuts down
await memory.disconnect();

The constructor takes host, port, password, db, prefix and ttl. It does not take a URL.

04Summary memory

Keeps the latest messages as they are, and summarizes the older ones with a model. On load, the summary comes first as a system message, followed by the recent messages.

TS
import { SummaryMemory, AnthropicProvider } from '@lov3kaizen/agentsea-core';

const memory = new SummaryMemory(
  new AnthropicProvider(), // provider that writes the summaries
  20,                      // maxRecentMessages: keep the last 20 verbatim (default 10)
  'claude-haiku-4-5',      // summaryModel (default)
);

Note

Summary memory keeps its data in process memory, like buffer memory. It is not persisted.

05Tenant buffer memory

Buffer memory where each call also takes a tenant ID. Conversations are stored per tenant, so a lookup under one tenant does not return another tenant's messages.

TS
import { TenantBufferMemory } from '@lov3kaizen/agentsea-core';

const memory = new TenantBufferMemory(50); // maxMessages

// The tenant ID is the last argument of save, load and clear
memory.save('conv-456', [
  { role: 'user', content: 'How do I upgrade my plan?' },
  { role: 'assistant', content: 'I can help you upgrade your plan...' },
], 'tenant-123');

const history = memory.load('conv-456', 'tenant-123');

// Tenant A and tenant B stay apart, even with the same conversation ID
const tenantA = memory.load('conv-1', 'tenant-a');
const tenantB = memory.load('conv-1', 'tenant-b');

memory.clear('conv-456', 'tenant-123');
const conversationIds = memory.getConversationIds('tenant-123');

Warning

TenantBufferMemory methods are synchronous, and they take the tenant ID as an extra argument. The Agent calls its memory with the conversation ID only, so the agent does not pass a tenant ID. For tenant isolation, call save, load and clear with the tenant ID yourself, or use a separate memory store per tenant.

Note

For tenant setup across the platform, see multi-tenancy.

06Memory interface

An agent accepts any object that implements this interface:

TS
interface MemoryStore {
  save(conversationId: string, messages: Message[]): Promise<void>;
  load(conversationId: string): Promise<Message[]>;
  clear(conversationId: string): Promise<void>;
  search?(query: string, limit?: number): Promise<Message[]>; // optional
}

07Custom memory store

Implement MemoryStore to keep history in your own database:

TS
import { Agent, MemoryStore, Message } from '@lov3kaizen/agentsea-core';

export class DatabaseMemory implements MemoryStore {
  constructor(private db: Database) {} // your database client

  async save(conversationId: string, messages: Message[]): Promise<void> {
    await this.db.conversations.upsert({
      where: { id: conversationId },
      update: { messages, updatedAt: new Date() },
      create: { id: conversationId, messages, createdAt: new Date() },
    });
  }

  async load(conversationId: string): Promise<Message[]> {
    const conversation = await this.db.conversations.findUnique({
      where: { id: conversationId },
    });
    return conversation?.messages ?? [];
  }

  async clear(conversationId: string): Promise<void> {
    await this.db.conversations.update({
      where: { id: conversationId },
      data: { messages: [] },
    });
  }
}

const memory = new DatabaseMemory(dbClient);
const agent = new Agent(config, provider, toolRegistry, memory);

08Managing history

You can read and clear a conversation outside an agent run:

TS
// Load the saved history
const history = await memory.load('conversation-123');

// Clear it
await memory.clear('conversation-123');

// List conversation IDs (BufferMemory and RedisMemory)
const ids = await memory.getConversationIds();

When an agent has a memory store, every execute() call loads history from that store by conversationId. The history field of the context is used only when the agent has no memory store, or when loading from the store fails.

09Session data

The context's sessionData field carries application data with a call. The agent loop does not read it, and it does not add it to the prompt. To give the model a value, put it in the system prompt or in the user message:

TS
const context = {
  conversationId: 'user-123',
  sessionData: { userId: '12345', timezone: 'America/Los_Angeles' },
  history: [],
};

const agent = new Agent(
  {
    // ...
    systemPrompt: 'The user is in the America/Los_Angeles timezone. Give times in that zone.',
  },
  provider,
  toolRegistry,
  memory,
);

const response = await agent.execute('What time is it?', context);

10Choosing a store

StoreWhere history livesSurvives restartNeeds
BufferMemoryProcess memoryNoNothing extra
RedisMemoryRedisYesA Redis server
SummaryMemoryProcess memoryNoA provider to write summaries
TenantBufferMemoryProcess memory, per tenantNoNothing extra

11Best practices

  • Pick by deployment: buffer for development and single-process apps, Redis for production and several servers, summary for long conversations.
  • Set limits: a message cap on buffer memory, or a recent-message count on summary memory, keeps token use in check.
  • Expire old data: set ttl on Redis memory, and clean up inactive conversations in other stores.
  • Protect the data: encrypt sensitive content before you store it, set a retention policy, and let users delete their history.
  • Use opaque conversation IDs that do not reveal user information.

12Next steps