Skip to content

Give your agent memory

Your agent has persistent memory it can read and write over the Agent API. In this tutorial you’ll store a fact and then recall it by meaning — not by exact wording — using the official client. It’s a short Node script you can run end to end.

Set your agent’s key in the environment so the script can read it. Use the setup token from the first tutorial. The memory API only accepts an agent’s key, not a key from a browser alfe login.

Terminal window
export ALFE_API_KEY=<AGENT_API_KEY>
Terminal window
npm i @alfe.ai/agent-api-client

Create memory-demo.mjs:

memory-demo.mjs
import { AgentApiClient } from "@alfe.ai/agent-api-client";
const client = new AgentApiClient({
apiKey: process.env.ALFE_API_KEY,
apiUrl: "https://api.alfe.ai",
});
const { memoryId } = await client.memoryStore(
"Acme prefers a single invoice at the end of the month.",
{ topic: "acme", subtopic: "invoicing", tag: "preference", importance: 0.9 },
);
console.log("Stored memory:", memoryId);

Run it:

Terminal window
node memory-demo.mjs
# Stored memory: mem_…

Only the text is required. topic, subtopic, tag, and importance are optional hints that make later recall sharper.

Add a search to the same script. Notice the query doesn’t share wording with the stored fact — memory matches on meaning:

memory-demo.mjs
const { memories, facts } = await client.memorySearch(
"how does Acme want to be billed?",
{ limit: 5 },
);
for (const m of memories) {
console.log(m.score.toFixed(2), m.text);
}
for (const f of facts) {
console.log(`${f.subject} → ${f.predicate} → ${f.object}`);
}

Run it again:

Terminal window
node memory-demo.mjs
# 0.84 Acme prefers a single invoice at the end of the month.
# Acme → prefers → single monthly invoice

Your output will vary. Each memory comes back with a relevance score, where higher is closer. Alongside the raw memories, facts are concise subject → predicate → object statements the agent has distilled. To leave them out, pass includeKnowledge: false.

Remove the demo memory by its id:

const { deleted } = await client.memoryDelete(memoryId);
console.log("Deleted:", deleted); // true
  • memoryStore writes a fact; memorySearch recalls by meaning; memoryDelete removes one by id.
  • Because identity is resolved from the token, the script only ever touched this agent’s memory.
  • The full method surface — memoryLearn, memoryNavigate, memoryStats, and more — is in the Memory reference.
  • Learn how automatic memory forms from conversations in How memory works.
  • Calling from another language? Every method maps to an HTTPS request under /agent/memory/; see Authentication.