mastra-semantic-recall
Skill2 starsChanged 29 days agoApache-2.0View on GitHub
Configure semantic recall in Mastra memory: embed every stored message, then pull the most similar older messages into context with topK, messageRange, thread or resource scope, and metadata filters. Use this skill whenever the user wants an agent to remember details that scrolled out of recent history, search past conversations by meaning, pick an embedder and vector store for memory, tune recall quality, latency, or embedding cost, or fix recall that returns nothing. Triggers on semanticRecall, topK, messageRange, scope resource, vectorSearchString, embedder, ModelRouterEmbeddingModel, openai/text-embedding-3-small, fastembed, embeddingDimension, MASTRA_MEMORY_EMBEDDING_DIMENSION_MISMATCH, LibSQLVector, PgVector indexConfig hnsw, MongoDB autoEmbed. Also triggers for questions like what did we decide about X last week in a Mastra chat app.
- Reads credentials
In this repository46 other files it gives its agents.
- mastra-a2a-acpSkill
- mastra-agent-approval-hitlSkill
- mastra-agentic-uiSkill
- mastra-agent-skillsSkill
- mastra-agentsSkill
- mastra-authSkill
- mastra-browserSkill
- mastra-build-playbookSkill
- mastra-channelsSkill
- mastra-classifierSkill
- mastra-cli-apiSkill
- mastra-client-sdkSkill
- mastra-datasets-experimentsSkill
- mastra-deploymentSkill
- mastra-evalsSkill
- mastra-file-based-agentsSkill
- mastra-framework-integrationSkill
- mastra-guideSkill
- mastra-harnessSkill
- mastra-knowledgeSkill
- mastra-mcpSkill
- mastra-memorySkill
- mastra-migrationsSkill
- mastra-modelsSkill
- mastra-multi-agentSkill
- mastra-observabilitySkill
- mastra-observational-memorySkill
- mastra-platformSkill
- mastra-processors-guardrailsSkill
- mastra-project-setupSkill
- mastra-ragSkill
- mastra-schedulesSkill
- mastra-serverSkill
- mastra-signalsSkill
- mastra-storageSkill
- mastra-streamingSkill
- mastra-structured-outputSkill
- mastra-studio-editorSkill
- mastra-tool-integrationsSkill
- mastra-toolsSkill
- mastra-troubleshootingSkill
- mastra-vector-storesSkill
- mastra-voiceSkill
- mastra-workflowsSkill
- mastra-workflow-suspend-resumeSkill
- mastra-workspace-sandboxSkill
The file
We split this skill into the rules it gives an agent
6 rules. About 0 are common, 1 is distinctive, and 5 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈1
Distinctive · estimate
This author's own choices. Worth a look.
5
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 90
Verify the API: If node_modules/@mastra/ exists, read node_modules/@mastra/memory/dist/docs/ and the installed Memory types.
- Very specific: line 91
Decide if recall is needed: If history plus working memory answers the follow-ups, stop.
- Very specific: line 95
Enable and size recall: Start with topK: 3, messageRange: 2.
- Very specific: line 97
Verify: Ask about something from an older thread, then open the trace in Studio.
- Distinctive: line 103
Every turn costs one embedding call for the input, one vector query, and one embedding call per saved message.
- Very specific: line 126
Recall is slow on PostgreSQL: Default IVFFlat index. Switch to indexConfig: { type: 'hnsw' } on pgvector 0.8.0+.
≈ “Common” is an estimate: at least 1 in 5 of the 408,460 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Mastra Semantic Recall
Find earlier messages by meaning and add them to the agent's context, so the agent can use details that are no longer in recent history.
When to use
- The user refers back to something said many turns or many threads ago
lastMessagesis not enough, but you still want raw message wording- Searching a user's past conversations by meaning from code with
recall() - Choosing an embedder and vector store for agent memory
- Tuning
topK,messageRange,scope, or a metadatafilter - Fixing recall errors, dimension mismatches, or slow vector queries
When NOT to use
- Basic threads, history, and working memory (use
mastra-memory) - Long agentic sessions where context must stay small and cacheable (use
mastra-observational-memory, which the docs rate as more accurate and cheaper) - Retrieval over documents, PDFs, or a knowledge base (use
mastra-rag) - Choosing between vector databases in depth (use
mastra-vector-stores) - Embedding model IDs and provider keys in general (use
mastra-models) - Realtime voice where every millisecond counts (leave recall off, see
mastra-voice)
Required inputs
- The storage adapter, and whether it supports
scope: 'resource'(libSQL, OracleDB, PostgreSQL, MongoDB, Upstash) - A vector store (often the same database as storage) or a self-embedding store
- An embedder ID and its output dimension (for example
openai/text-embedding-3-small, 1536) - Recall scope: this thread only, or all threads of the resource
- Any metadata the recall must be limited by (project, tenant, category)
- Latency budget per turn
The framework: embed on save, search on input
Semantic recall is a pair of processors. On output, new messages (user, assistant, tool calls and results) are embedded and stored in the vector store. On input, the new message is embedded, the store returns the topK closest messages, and each match is added with messageRange neighbours.
import { Agent } from '@mastra/core/agent'
import { Memory } from '@mastra/memory'
import { LibSQLStore, LibSQLVector } from '@mastra/libsql'
import { ModelRouterEmbeddingModel } from '@mastra/core/llm'
export const supportAgent = new Agent({
id: 'support-agent',
name: 'SupportAgent',
instructions: 'You are a helpful support agent.',
model: 'openai/gpt-6.1-sol',
memory: new Memory({
storage: new LibSQLStore({ id: 'agent-storage', url: 'file:./local.db' }),
vector: new LibSQLVector({ id: 'agent-vector', url: 'file:./local.db' }),
embedder: new ModelRouterEmbeddingModel('openai/text-embedding-3-small'),
options: {
lastMessages: 10,
semanticRecall: { topK: 3, messageRange: 2, scope: 'resource' },
},
}),
})| Option | Default | Effect |
topK | 4 | Number of matched messages |
messageRange | { before: 1, after: 1 } | Neighbours added around each match. A number sets both sides |
scope | 'resource' | 'thread' searches the current thread only |
filter | none | Metadata filter on stored embeddings ($eq, $in, $and, ...) |
indexConfig | store default | Vector index type and metric (PgVector: ivfflat default, hnsw recommended) |
Where recalled messages land: matches from the current thread interleave with history by timestamp. Matches from other threads go into a system message.
Workflow
- Verify the API. If
node_modules/@mastra/exists, readnode_modules/@mastra/memory/dist/docs/and the installedMemorytypes. Otherwise fetchhttps://mastra.ai/docs/memory/semantic-recall.mdandhttps://mastra.ai/reference/memory/memory-class.md. Trust these over memory. - Decide if recall is needed. If history plus working memory answers the follow-ups, stop. If threads run long, compare with
mastra-observational-memoryfirst. - Pick the vector store. Reuse the storage database when possible:
LibSQLVectorwithLibSQLStore,PgVectorwithPostgresStore,MongoDBVectorwithMongoDBStore. Other options inmastra-vector-stores. - Pick the embedder. A router string such as
'openai/text-embedding-3-small'ornew ModelRouterEmbeddingModel(...), localfastembedfrom@mastra/fastembed, or none when the store embeds server-side (MongoDBautoEmbed). - Set
embeddingDimension. It skips the startup probe call and fails fast on a mismatch. - Enable and size recall. Start with
topK: 3,messageRange: 2. Choosescope. Addfilterif threads carry metadata such asprojectId. - Tune the index. On PostgreSQL use
indexConfig: { type: 'hnsw', metric: 'dotproduct' }for OpenAI embeddings. - Verify. Ask about something from an older thread, then open the trace in Studio. Recalled messages appear in the LLM span. Or call
memory.recall({ vectorSearchString, threadConfig: { semanticRecall: true } }).
Deep dive: tuning and cost
Every turn costs one embedding call for the input, one vector query, and one embedding call per saved message. That is the latency and spend to budget.
| Lever | Raise when | Lower when |
topK | Answers miss facts the user stated | Context fills with loosely related turns |
messageRange | Matches lack the surrounding question or answer | Token usage per turn is too high |
scope: 'resource' | Facts live in other conversations | Threads are unrelated tasks; leakage confuses the model |
filter | One resource mixes projects or tenants | Never needed |
- Index type matters at scale. Recall always filters by thread or resource. PgVector uses the index for those searches only with HNSW on pgvector 0.8.0 or newer. With IVFFlat, each search compares against every message of the thread or resource.
- Filters read embedding metadata from save time. Changing thread metadata later does not update existing embeddings until they are saved or indexed again.
- FastEmbed truncates at 512 tokens. Text past the window cannot be recalled. Multilingual E5 is 1024 dimensions; use one model (
multilingualE5LargePassage) for both storing and searching in memory. - Server-side embeddings are eventually consistent. MongoDB embeds new messages in the background, so a message saved moments ago may not be recallable yet.
- Thread cloning re-embeds.
cloneThread()andcopyThread()embed copied messages in batches when recall is on.
Failure patterns
- "Embedder required" error on startup. Recall is on with neither
embeddernor a self-embedding store. Add an embedder or use MongoDBautoEmbed. MASTRA_MEMORY_EMBEDDING_DIMENSION_MISMATCH.embeddingDimensiondiffers from the model output. Update it whenever you change the embedder.- Switched embedder and recall returns junk. Old vectors came from another model. Vectors from different embedders cannot share an index; re-index or start a new index.
scope: 'resource'finds nothing across threads. The storage adapter does not support resource scope, or calls pass differentresourcevalues. Use a listed adapter and a stable resource ID.- Recall is slow on PostgreSQL. Default IVFFlat index. Switch to
indexConfig: { type: 'hnsw' }on pgvector 0.8.0+. - Missing API key at construction.
ModelRouterEmbeddingModelthrows ifOPENAI_API_KEY(orGOOGLE_API_KEY,OPENROUTER_API_KEY) is unset. recall()withvectorSearchStringreturns recent messages only.threadConfig: { semanticRecall: true }was not passed.- Using recall to hold user profile facts. Similarity search is not guaranteed to return them. Put stable facts in working memory (
mastra-memory). - Recall enabled on a realtime voice agent. Adds embedding and query latency each turn. Disable it there.
Output format
A finished semantic recall delivery contains:
Memorywithvector,embedder(or a self-embedding store), andembeddingDimensionsemanticRecallwith explicittopK,messageRange, andscope, plusfilterif used- Index configuration for PostgreSQL deployments
- Embedder API key listed in
.env.example - Evidence: a trace or
recall()call showing an older message retrieved by meaning
Reference files
references/api.md-semanticRecalloptions, embedder forms, embedding dimensions,recall()search parameters, filter operators.references/examples.md- libSQL recall, PostgreSQL with HNSW and fastembed, MongoDB server-side embeddings, programmatic search.references/checklist.md- Pre-production checklist for semantic recall.
Live docs
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

