AI Agents
Agent Patterns
Design patterns for building memory-powered AI agents with MemNexus.
These patterns show how to structure agent behavior around persistent memory. Each pattern is framework-agnostic — implement it with the SDK, MCP tools, or CLI.
Store-and-Recall
The simplest pattern. Save important information during a session, retrieve it in future sessions.
When to use
- Preserving decisions, preferences, and project context
- Building a persistent knowledge base across conversations
- Enabling continuity between agent sessions
How it works
Session 1:
Agent works on task → extracts key information → stores as memory
Session 2:
Agent receives new task → searches relevant memories → uses context
Implementation (SDK)
// During a session — store what matters
await client.memories.create({
content: "User prefers TypeScript over JavaScript. Uses pnpm, not npm.",
topics: ["preferences", "tooling"],
importance: 0.9,
});
// In a future session — recall before acting
const prefs = await client.memories.search({
query: "user preferences for programming languages and tools",
topics: ["preferences"],
limit: 5,
});
Implementation (MCP)
An MCP-connected agent does this naturally when instructed:
System prompt: "Before making technology choices, search MemNexus
for the user's preferences using the memnexus_search tool."
Tips
- Store preferences with high importance (0.8-1.0) so they rank higher in search
- Use consistent topics like
preferences,decisions,contextfor easy filtering - Store the "why" alongside the "what" — future sessions benefit from reasoning
Search-Before-Ask
Before asking the user a question, check if the answer already exists in memory.
When to use
- Reducing repetitive questions across sessions
- Building agents that feel like they "know" the user
- Improving response quality by leveraging past context
How it works
Agent needs information → searches memory first
├── Found relevant memory → uses it (no need to ask)
└── Nothing found → asks user → stores answer for next time
Implementation (SDK)
async function getProjectContext(question: string): Promise<string | null> {
const results = await client.memories.search({
query: question,
limit: 3,
});
// If we found a high-confidence match, use it
if (results.data.length > 0 && results.data[0].score > 0.75) {
return results.data[0].memory.content;
}
return null; // Ask the user
}
// Usage
const dbChoice = await getProjectContext("what database does this project use");
if (dbChoice) {
console.log(`Using known preference: ${dbChoice}`);
} else {
// Ask user and store the answer
const answer = await askUser("What database does this project use?");
await client.memories.create({
content: `Project uses ${answer} as its database.`,
topics: ["project-context", "database"],
importance: 0.8,
});
}
Tips
- Set a score threshold (0.7-0.8) to avoid using irrelevant matches
- When the user corrects a recalled memory, update or create a new one
- Use topic filters to narrow search scope when you know the category
Progressive Learning
Build knowledge incrementally over multiple sessions. The agent gets better the more you use it.
When to use
- Long-running projects where context accumulates
- Agents that need to learn domain-specific knowledge
- Building a shared knowledge base across team members
How it works
Session 1: Agent learns basic project structure
Session 2: Agent learns coding patterns and preferences
Session 3: Agent learns deployment procedures
...
Session N: Agent has deep project knowledge
Implementation (SDK)
// At the end of each session, extract and store learnings
async function saveSessionLearnings(sessionSummary: string, topics: string[]) {
// Store the episodic memory
await client.memories.create({
content: sessionSummary,
topics: [...topics, "session-learning"],
importance: 0.7,
});
// Extract structured facts
// e.g., "Project uses PostgreSQL" → fact triple
await client.facts.create({
subject: "project",
predicate: "uses_database",
object: "PostgreSQL",
confidence: 0.95,
});
}
// At the start of each session, load accumulated knowledge
async function loadProjectKnowledge(projectName: string) {
const memories = await client.memories.search({
query: `${projectName} project context and decisions`,
topics: ["session-learning"],
limit: 20,
});
const facts = await client.facts.search({
query: projectName,
limit: 50,
});
return { memories: memories.data, facts: facts.data };
}
Tips
- Use conversations to group memories by session for timeline reconstruction
- Store both episodic memories (what happened) and facts (what's true)
- Periodically review stored facts — correct anything outdated
- Use importance scores to surface the most valuable learnings first
Combining patterns
These patterns work best together. A typical memory-aware agent:
- Loads context at session start (Progressive Learning)
- Checks memory before asking questions (Search-Before-Ask)
- Stores outcomes at session end (Store-and-Recall)
async function agentSession(userQuery: string) {
// 1. Load accumulated knowledge
const knowledge = await loadProjectKnowledge("my-project");
// 2. Check memory for relevant context
const existingContext = await getProjectContext(userQuery);
// 3. Process query with full context
const response = await processWithContext(userQuery, knowledge, existingContext);
// 4. Store new learnings
await saveSessionLearnings(
`Handled query: "${userQuery}". Key outcome: ${response.summary}`,
["my-project"]
);
return response;
}
Next steps
- Prompt Library — Ready-to-use system prompts
- Platform Examples — Working examples for Claude, Cursor, and more
- MCP Integration — Native agent integration via MCP