Hello--we're having an issue with duplicate messages being saved to mastra_messages and would love any insight you might be able to share.
thanks!
wayne
Context
Agentic app with SMS/web chat using Mastra:
- Main agent with memory (conversation history, semantic recall, working memory)
- Editor sub-agent for tone refinement
- Workflow orchestrating both agents
Architecture
export const daveAgent = new Agent({
name: 'main-agent',
model: openai.chat('gpt-4o-mini'),
tools: { /* various */ },
memory: daveMemory,
});
export const daveMemory = new Memory({
storage: new PostgresStore({ connectionString: process.env.DATABASE_URL }),
vector: new PgVector({ connectionString: process.env.DATABASE_URL }),
embedder: openai.embedding('text-embedding-3-small'),
options: {
workingMemory: { enabled: true, scope: 'resource' },
lastMessages: 10,
semanticRecall: { topK: 5, messageRange: 3 },
},
});
Flow Causing Duplicates
1. Streaming call - with memory:
await daveAgent.stream([{ role: 'user', content: userMessage }], {
memory: { thread: { id: threadId }, resource: resourceId },
maxSteps: 5,
});
2. Workflow call - NO memory options:
await agent.generate([{ role: 'user', content: prompt }], { maxSteps: 5 });
Problem Both calls save to mastra_messages, creating duplicates ~0.3s apart:
{ "thread_id": "user-123", "seconds_apart": "0.351" }
What We Tried Per docs: "agents won't store or recall information unless both thread and resource are provided." We removed thread/resource from workflow's generate(). Duplicates still occur.
Questions:
1. Does memory at agent level auto-save even without thread/resource in generate()?
2. Way to call generate() WITHOUT saving? e.g., memory: false?
3. Recommended pattern for same agent in streaming (save) vs workflow (no save)?
Versions @mastra/core: 0.24.9, @mastra/memory: 0.15.13, @mastra/pg: 0.17.10