Support agent with memory
A pattern for a chat support agent, built from calls the TypeScript client has today. The example is the help desk of Larkspur Robotics, a fictional company that sells warehouse robots.
Goals
- Recall what is known about the customer on every message.
- Store stable preferences and facts as they come up.
- Record how the ticket ended, so the outcome feeds back into memory.
Decide where each customer’s memory lives
The API key decides the workspace, and there is no workspaceId argument on
recall. Inside one workspace, namespace each customer with scopeRef:
const customerRef = `customer:${customerId}`;scopeRef is a filter, not a security boundary. If customers must be isolated
from each other in the database, give each one its own workspace and key, as
in Multi-tenant SaaS.
Start a session per ticket
const session = await memory.createSession({
agentId: "support-bot",
label: `ticket ${ticketId}`,
});On every message, recall first
async function answer(sessionId: string, userMessage: string) {
await memory.appendTurn(sessionId, { role: "user", content: userMessage });
const customer = await memory.recall({
query: userMessage,
topK: 5,
kinds: ["fact", "decision"],
scopeRef: customerRef,
sessionId,
});
const playbook = await memory.recall({
query: userMessage,
topK: 3,
kinds: ["strategy"],
sessionId,
});
// generateReply is your own model call.
const reply = await generateReply({
knownFacts: customer.hits.map((hit) => hit.content),
strategies: playbook.hits.map((hit) => hit.content),
userMessage,
});
await memory.appendTurn(sessionId, { role: "assistant", content: reply });
return reply;
}Passing sessionId to recall is what lets the feedback step find the facts
this session used.
Remember durable observations
// A stable fact about this customer
await memory.remember({
statement: "Acme runs firmware 2.8 on its aisle robots.",
tags: ["preference"],
confidence: 0.9,
scopeRef: customerRef,
sessionId,
});
// A way of solving a class of problem, shared by every customer
const strategy = await memory.createStrategy({
trigger: "A robot stops mid-aisle after a firmware update",
action: "Ask for the firmware version, then walk through sensor recalibration",
});Record the outcome when the ticket closes
async function onTicketClosed(
sessionId: string,
ticketId: string,
status: "resolved" | "escalated",
strategyId?: string
) {
await memory.createEvent({
type: "external",
name: status === "resolved" ? "ticket.resolved" : "ticket.escalated",
description: `Ticket ${ticketId} closed with status ${status}`,
occurredAt: new Date().toISOString(),
source: "helpdesk",
metadata: { ticketId, status },
});
await memory.sessionFeedback(sessionId, status === "resolved" ? "up" : "down");
if (strategyId) {
await memory.rateStrategy(
strategyId,
status === "resolved" ? "success" : "failure"
);
}
await memory.collapseSession(sessionId);
}What each call does
createEventneedstype,name,description,occurredAtandsource. Events are not returned byrecallunless you ask for them withkinds: ["event"].sessionFeedbackstores the rating on the session and updates the worth of the facts that were recalled with thatsessionId.rateStrategyrecords one success or failure and recomputes the strategy’s worth.collapseSessionturns the session’s turns into durable memory with an LLM. It needs an LLM provider configured on the server.
Worth is a small adjustment on top of relevance in recall. It does not
move an unrelated memory above a relevant one.