Skip to content

Retrieving Memories

import Callout from ’../../../components/Callout.astro’;

The Memory API provides several methods for retrieving memories, each optimized for different use cases. This guide will show you how to efficiently fetch and search memories.

To retrieve a specific memory by its ID, send a GET request to the /memory/{id} endpoint:

// Example of retrieving a specific memory by ID
const response = await fetch('https://api.example.com/memory/mem-123456', {
method: 'GET',
headers: {
'Authorization': 'Bearer YOUR_API_KEY'
}
});
const result = await response.json();
console.log(result.memory); // The retrieved memory
Always include the tenant ID in your requests to ensure you're only accessing memories that belong to the correct tenant.

For more complex retrieval needs, use the /memories/query endpoint to search across your memories:

// Example of querying memories
const response = await fetch('https://api.example.com/memories/query', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer YOUR_API_KEY'
},
body: JSON.stringify({
tenantId: "tenant-123",
type: "long-term",
tags: ["preferences"],
limit: 10,
offset: 0
})
});
const result = await response.json();
console.log(result.memories); // Array of matching memories
ParameterTypeDescription
tenantIdstringID of the tenant to query memories for
typeenumOptional filter by memory type
tagsstring[]Optional filter by tags
sourcestringOptional filter by source
fromDatestringOptional filter for memories created after this date
toDatestringOptional filter for memories created before this date
limitnumberMaximum number of results to return (default: 50)
offsetnumberNumber of results to skip (for pagination)

For long-term and semantic RAG memories, you can perform semantic searches based on content similarity:

// Example of semantic search
const response = await fetch('https://api.example.com/memories/semantic-search', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer YOUR_API_KEY'
},
body: JSON.stringify({
tenantId: "tenant-123",
query: "User preferences for dark mode",
limit: 5,
minScore: 0.7
})
});
const result = await response.json();
console.log(result.memories); // Array of semantically similar memories
Semantic search finds memories that are conceptually similar to your query, even if they don't contain the exact same words.
ParameterTypeDescription
tenantIdstringID of the tenant to search memories for
querystringThe search query text
limitnumberMaximum number of results to return (default: 10)
minScorenumberMinimum similarity score (0-1) for results
typeenumOptional filter by memory type
tagsstring[]Optional filter by tags

To retrieve all memories related to a specific entity, use the /entities/{id}/memories endpoint:

// Example of retrieving memories related to an entity
const response = await fetch('https://api.example.com/entities/person-alice/memories', {
method: 'GET',
headers: {
'Authorization': 'Bearer YOUR_API_KEY'
}
});
const result = await response.json();
console.log(result.memories); // Array of memories related to the entity

This is particularly useful for building context around specific entities in your knowledge graph.

To optimize your memory retrieval:

  1. Use the right retrieval method for your use case:

    • Direct retrieval for known memory IDs
    • Query for filtering by metadata
    • Semantic search for finding conceptually similar content
    • Entity-based retrieval for building context around entities
  2. Limit result sets to improve performance, especially with semantic search

  3. Combine retrieval methods for complex scenarios:

    // Example: First find relevant entities, then get their memories
    const entityResponse = await fetch('https://api.example.com/entities/search?q=conference');
    const entities = await entityResponse.json();
    // Then get memories for each entity
    const memoryPromises = entities.map(entity =>
    fetch(`https://api.example.com/entities/${entity.id}/memories`)
    );
    const memoryResponses = await Promise.all(memoryPromises);
Semantic search operations require more processing than direct or query-based retrieval. Use them judiciously for best performance.

Now that you understand how to retrieve memories, your next steps should be:

  1. Next: Entity Management - Master working with entities and their relationships
  2. Then: Advanced RAG Techniques - Implement semantic search for better context

Following this sequence will build your knowledge systematically and prepare you for advanced implementation.