Memory Integration
MDWR automatically stores every decision in memory, allowing workflows to learn from history and make better decisions.
Memory Providers
Development (Mock)
Default configuration uses mock memory - no setup required:
{
"memory": {
"provider": "mock",
"collection": "mdwr_decisions"
}
}
Production (Mem0)
- Get Mem0 API key from https://mem0.ai
- Update
mdwr.config.json:{ "memory": { "provider": "mem0", "collection": "mdwr_decisions" } } - Set environment variable:
export MEM0_API_KEY=your-key
Querying Similar Decisions
Use ctx.memory.similar() to find similar historical decisions:
const insights = await ctx.memory.similar({
situation: "refund_decision",
context: {
amount: ctx.input.amount,
userTier: ctx.input.userTier
}
});
Using Insights
if (insights.length > 0) {
const best = insights[0];
// Access decision details
const decision = best.decision.decision;
const reason = best.decision.reason;
const confidence = best.decision.confidence;
// Access outcome (if available)
const success = best.outcome?.success;
const outcomeData = best.outcome?.data;
// Use in your logic
if (success) {
// Follow successful pattern
}
}
Querying Decisions
Use ctx.memory.query() to find specific decisions:
// Query by workflow
const decisions = await ctx.memory.query({
workflowId: "refund_v1",
limit: 10
});
// Query by step
const stepDecisions = await ctx.memory.query({
stepName: "evaluate_risk",
limit: 20
});
// Query by decision value
const approvals = await ctx.memory.query({
decision: "approve",
limit: 50
});
Memory Structure
Each stored decision contains:
{
decision: {
decision: string;
reason: string;
confidence?: number;
};
context: {
input: any;
state: any;
metadata?: any;
};
outcome?: {
success: boolean;
data?: any;
};
timestamp: string;
workflowId: string;
stepName: string;
}
Best Practices
1. Use Specific Situations
// Good: Specific situation
await ctx.memory.similar({
situation: "refund_decision_high_amount",
context: { amount: ctx.input.amount }
});
// Avoid: Too generic
await ctx.memory.similar({
situation: "decision",
context: {}
});
2. Include Relevant Context
await ctx.memory.similar({
situation: "approval_decision",
context: {
amount: ctx.input.amount,
userTier: ctx.input.userTier,
urgency: ctx.input.urgency
}
});
3. Check Outcomes
const insights = await ctx.memory.similar({...});
// Filter by successful outcomes
const successful = insights.filter(i => i.outcome?.success);
if (successful.length > 0) {
// Use successful pattern
}
4. Cross-Workflow Learning
await ctx.memory.similar({
situation: "risk_evaluation",
context: ctx.input,
crossWorkflow: ["refund_v1", "loan_v1"]
});
Automatic Storage
Every decision is automatically stored when a step completes:
- Decision and reason
- Confidence score
- Full context snapshot
- Workflow and step information
- Timestamp
Outcomes can be linked later:
await execution.linkOutcome({
success: true,
data: { refundId: "ref_123" }
});
Next Steps
- Replay Modes - Use memory in replay
- API Reference - Complete API docs
- Examples - See memory in action