Skip to main content

Overview

The Memory system in elizaOS stores and retrieves agent memories, messages, documents, and other data. Memories can be searched by embedding similarity, filtered by metadata, and organized into different tables.

Memory Type

interface Memory {
  id?: UUID;
  createdAt?: number;
  content: Content;
  embedding?: number[];
  entityId: UUID;
  agentId?: UUID;
  roomId: UUID;
  worldId?: UUID;
  metadata?: MemoryMetadata;
  sessionId?: string;
  sessionKey?: string;
}
id
UUID
Unique identifier for the memory
createdAt
number
Unix timestamp (milliseconds) when memory was created
content
Content
required
Memory content with text, actions, and metadata
embedding
number[]
Vector embedding for semantic search
entityId
UUID
required
ID of the entity that created this memory
agentId
UUID
ID of the agent associated with this memory
roomId
UUID
required
ID of the room/conversation this memory belongs to
worldId
UUID
ID of the world/server this memory belongs to
metadata
MemoryMetadata
Type-specific metadata (message, document, fragment, etc.)
sessionId
string
Session UUID for filtering memories by conversation session
sessionKey
string
Full session key for routing (e.g., “agent:123:telegram:+14155551234”)

MemoryType

Built-in memory types:
enum MemoryType {
  DOCUMENT = "document",
  FRAGMENT = "fragment",
  MESSAGE = "message",
  DESCRIPTION = "description",
  CUSTOM = "custom"
}
DOCUMENT
string
Represents a whole document or large piece of text
FRAGMENT
string
A chunk/segment of a DOCUMENT (for embedding and search)
MESSAGE
string
A conversational message from user or agent
DESCRIPTION
string
Descriptive information about an entity or concept
CUSTOM
string
Custom memory type for extensions

MemoryMetadata

Memory metadata varies by type:

MessageMetadata

interface MessageMetadata {
  type?: "message";
  timestamp?: number;
  scope?: "shared" | "private" | "room";
  sessionKey?: string;
  sender?: SenderIdentity;
  provider?: string;
  chatType?: MessageChatType;
  wasMentioned?: boolean;
  // ... platform-specific fields
}
sessionKey
string
Session key for conversation routing
sender
SenderIdentity
Sender information (id, name, username)
provider
string
Platform name (telegram, discord, slack)
chatType
string
Type of chat (dm, group, channel, thread)

DocumentMetadata

interface DocumentMetadata {
  type?: "document";
  source?: string;
  sourceId?: UUID;
  scope?: MemoryScope;
}

FragmentMetadata

interface FragmentMetadata {
  type?: "fragment";
  documentId: UUID;
  position: number;
}
documentId
UUID
required
ID of the parent document
position
number
required
Position of fragment within document

Functions

createMessageMemory

Create a message memory.
import { createMessageMemory } from "@elizaos/core";

const memory = createMessageMemory({
  entityId: userId,
  agentId: agentId,
  roomId: roomId,
  content: {
    text: "Hello, world!",
    source: "telegram"
  },
  embedding: [0.1, 0.2, ...] // optional
});
params
object
required
Memory creation parameters
params.entityId
UUID
required
Entity that created the memory
params.agentId
UUID
Associated agent ID
params.roomId
UUID
required
Room/conversation ID
params.content
Content
required
Memory content with text
params.embedding
number[]
Vector embedding
Returns: MessageMemory with metadata set.

Type Guards

isDocumentMemory

import { isDocumentMemory } from "@elizaos/core";

if (isDocumentMemory(memory)) {
  // memory.metadata is DocumentMetadata
}

isFragmentMemory

import { isFragmentMemory } from "@elizaos/core";

if (isFragmentMemory(memory)) {
  console.log(memory.metadata.documentId);
  console.log(memory.metadata.position);
}

isMessageMetadata

import { isMessageMetadata } from "@elizaos/core";

if (memory.metadata && isMessageMetadata(memory.metadata)) {
  console.log(memory.metadata.sender);
}

getMemoryText

Safely extract text from a memory.
import { getMemoryText } from "@elizaos/core";

const text = getMemoryText(memory, "default text");
memory
Memory
required
Memory object
defaultValue
string
Default value if no text found (default: "")

Runtime Memory Operations

The AgentRuntime provides methods for memory operations:

createMemory

await runtime.createMemory(memory: Memory, tableName: string): Promise<void>
Store a memory in the database.
memory
Memory
required
Memory to store
tableName
string
required
Table name: “messages”, “documents”, “fragments”, etc.
Example:
await runtime.createMemory(
  {
    entityId: userId,
    roomId: roomId,
    content: { text: "Hello!" }
  },
  "messages"
);

getMemories

await runtime.getMemories(params: {
  roomId: UUID;
  count?: number;
  unique?: boolean;
  tableName: string;
  agentId?: UUID;
  start?: number;
  end?: number;
}): Promise<Memory[]>
Retrieve memories from the database.
roomId
UUID
required
Room to get memories from
count
number
Maximum number of memories to return
unique
boolean
Only return unique memories (by entity)
tableName
string
required
Table name to query
start
number
Start timestamp (Unix milliseconds)
end
number
End timestamp (Unix milliseconds)
Example:
const messages = await runtime.getMemories({
  roomId: roomId,
  count: 10,
  tableName: "messages"
});

searchMemories

await runtime.searchMemories(params: {
  tableName: string;
  roomId: UUID;
  embedding: number[];
  match_threshold?: number;
  match_count?: number;
  unique?: boolean;
}): Promise<Memory[]>
Search memories by embedding similarity.
tableName
string
required
Table to search
roomId
UUID
required
Room to search in
embedding
number[]
required
Query embedding vector
match_threshold
number
Similarity threshold (0-1, default: 0.7)
match_count
number
Maximum results to return (default: 10)
Example:
const queryEmbedding = await runtime.embed("search query");

const similar = await runtime.searchMemories({
  tableName: "documents",
  roomId: roomId,
  embedding: queryEmbedding,
  match_threshold: 0.8,
  match_count: 5
});

Example: Working with Memories

import { 
  AgentRuntime, 
  createMessageMemory, 
  getMemoryText 
} from "@elizaos/core";

// Create a message memory
const memory = createMessageMemory({
  entityId: userId,
  roomId: roomId,
  content: {
    text: "What is TypeScript?",
    source: "telegram"
  }
});

// Store it
await runtime.createMemory(memory, "messages");

// Retrieve recent messages
const recentMessages = await runtime.getMemories({
  roomId: roomId,
  count: 10,
  tableName: "messages"
});

// Extract text safely
for (const msg of recentMessages) {
  const text = getMemoryText(msg);
  console.log(text);
}

// Search by similarity
const queryEmbed = await runtime.embed("programming languages");
const relevantDocs = await runtime.searchMemories({
  tableName: "documents",
  roomId: roomId,
  embedding: queryEmbed,
  match_count: 3
});

Build docs developers (and LLMs) love