skills/claude-4-6-features/memory-tool/SKILL.md
Use Claude's Memory tool to give agents persistent cross-session memory stored in a client-side file directory. Covers setup, directory layout, reading/writing patterns, and when memory beats context-stuffing. Use this skill when building agents that need to remember across sessions (user preferences, project state, past decisions), or when moving from "stuff everything into context" to persistent memory. Activate when: Claude memory tool, agent memory, persistent memory, memory directory, cross-session state, long-term memory agent.
npx skillsauth add latestaiagents/agent-skills memory-toolInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
The Memory tool lets Claude read and write files in a client-managed directory, giving agents persistent state across sessions without RAG infrastructure.
The Memory tool is a server-side tool (provided by Anthropic) that Claude can call to read, write, and list files in a directory you manage. The API gives the tool calls; your app executes them against real storage (local fs, S3, DB-backed fs).
const response = await client.beta.messages.create({
model: "claude-sonnet-4-6",
max_tokens: 4096,
tools: [{ type: "memory_20250818", name: "memory" }],
messages: [...],
});
When Claude calls the memory tool, your app handles the file operations. Minimal implementation:
import fs from "fs/promises";
import path from "path";
const MEMORY_ROOT = "/var/app/memory";
async function handleMemoryCall(input: any, userId: string) {
const userRoot = path.join(MEMORY_ROOT, userId);
const safe = (p: string) => {
const resolved = path.resolve(userRoot, p.replace(/^\//, ""));
if (!resolved.startsWith(userRoot + path.sep)) throw new Error("Path escapes root");
return resolved;
};
switch (input.command) {
case "view": {
const target = safe(input.path);
const stat = await fs.stat(target).catch(() => null);
if (!stat) return { error: "not found" };
if (stat.isDirectory()) {
const entries = await fs.readdir(target);
return { content: entries.join("\n") };
}
return { content: await fs.readFile(target, "utf-8") };
}
case "create": {
await fs.mkdir(path.dirname(safe(input.path)), { recursive: true });
await fs.writeFile(safe(input.path), input.file_text);
return { content: "created" };
}
case "str_replace": {
const f = safe(input.path);
const text = await fs.readFile(f, "utf-8");
if (!text.includes(input.old_str)) return { error: "old_str not found" };
await fs.writeFile(f, text.replace(input.old_str, input.new_str));
return { content: "replaced" };
}
case "insert": {
const f = safe(input.path);
const lines = (await fs.readFile(f, "utf-8")).split("\n");
lines.splice(input.insert_line, 0, input.insert_text);
await fs.writeFile(f, lines.join("\n"));
return { content: "inserted" };
}
case "delete": {
await fs.rm(safe(input.path), { recursive: true, force: true });
return { content: "deleted" };
}
case "rename": {
await fs.rename(safe(input.old_path), safe(input.new_path));
return { content: "renamed" };
}
}
}
Per-user roots are critical — never share memory across users.
async function runWithMemory(userId: string, userMessage: string) {
const messages = [{ role: "user", content: userMessage }];
while (true) {
const response = await client.beta.messages.create({
model: "claude-sonnet-4-6",
max_tokens: 4096,
tools: [{ type: "memory_20250818", name: "memory" }],
messages,
});
messages.push({ role: "assistant", content: response.content });
if (response.stop_reason !== "tool_use") return response;
const toolResults = [];
for (const block of response.content) {
if (block.type === "tool_use" && block.name === "memory") {
const result = await handleMemoryCall(block.input, userId);
toolResults.push({
type: "tool_result",
tool_use_id: block.id,
content: result.content ?? result.error,
is_error: !!result.error,
});
}
}
messages.push({ role: "user", content: toolResults });
}
}
Claude will structure the memory itself, but seeding a clean layout helps. Example for a coding agent:
/memory/
user_profile.md # who the user is, preferences
current_project.md # what we're working on
decisions/
2026-04-15-auth.md # architectural decisions
conventions.md # code style rules learned
blockers.md # open issues
Prompt the agent with this layout in the system prompt so it uses consistent paths.
You have persistent memory at /memory. Before answering, consult:
- /memory/user_profile.md
- /memory/current_project.md
After significant work, update memory:
- Save user preferences to /memory/user_profile.md
- Save decisions to /memory/decisions/YYYY-MM-DD-topic.md
- Keep /memory/current_project.md up to date with what we're doing
Use `view` to read, `create`/`str_replace` to write. Keep memory concise.
| Approach | Best for | Downside | |---|---|---| | Memory tool | Structured state the agent curates itself | Agent has to remember to read/write | | Large context | One-shot reasoning on full corpus | Expensive per call | | RAG | Huge static corpus | Brittle retrieval; no synthesis across docs |
Memory shines when the agent decides what's worth remembering, not a retrieval system.
../ or absolute pathsdevelopment
Test skills for correct activation, content quality, and regression — both automated checks (frontmatter validity, lint) and manual verification (query-suite activation testing). Covers CI integration and how to catch skill regressions before users do. Use this skill when adding skills to a repo, setting up CI for a skill library, or debugging "the skill exists but doesn't work". Activate when: test skills, validate skills, skill CI, skill linting, skill activation test, skill regression.
documentation
Write the YAML frontmatter for a SKILL.md file so it activates reliably — name, description, and activation keywords that the model matches against. Covers length, tone, and the most common frontmatter mistakes. Use this skill when authoring a new skill, fixing a skill that isn't auto-activating, or reviewing skills for publication. Activate when: SKILL.md frontmatter, skill description, skill activation, skill YAML, write a skill, author a skill.
development
Design skills that fire at the right moment — neither over-eager (noise) nor under-eager (silent). Covers activation specificity, trigger phrases, disambiguation between overlapping skills, and debugging activation. Use this skill when multiple skills could fire on the same query, a skill never fires, or a skill fires too often. Activate when: skill won't activate, skill over-activates, overlapping skills, skill triggers, skill selection, skill disambiguation.
development
Structure SKILL.md content so the model reads just enough — concise summary up front, progressively deeper detail, examples on demand. Covers section ordering, length budgets, when to split into multiple skills. Use this skill when writing or refactoring a skill body, one skill has grown too long, or a skill is wordy but not useful. Activate when: SKILL.md structure, skill content, skill too long, split skill, progressive disclosure, skill body.