agentmemory:给AI Coding Agent装上持久记忆,让每次会话都认识你的项目

引言:AI Coding Agent的"失忆症"

用过Claude Code、Cursor、Cline的朋友,有没有这种感觉:

这不是AI不够聪明,是它压根没有记忆能力——每次会话结束,上下文全丢了。

agentmemory就是来解决这个问题的:给AI Coding Agent装上持久化记忆,让它"记住"项目的历史、决策、偏好。

什么是agentmemory?

agentmemory是一个专门为AI Coding Agent设计的持久化记忆系统,基于真实世界基准测试打造(不是拍脑袋造的)。

核心特点:

多级记忆架构

agentmemory不是简单的key-value存储,它有层级设计:

project_context/     # 项目级:项目是什么、技术栈、架构设计
  ├── overview/     # 总体概述
  ├── architecture/  # 架构决策
  └── decisions/    # 重要决定记录
codebase_memory/    # 代码级:具体代码的意图、坑点
  ├── patterns/     # 代码模式记录
  ├── conventions/  # 代码规范
  └── hotspots/    # 容易出问题的区域
session_history/    # 会话历史
  ├── completed/    # 已完成的任务
  └── failed/      # 失败的尝试(同样重要)
preferences/        # 用户偏好
  ├── coding_style/ # 编码风格偏好
  └── tool_prefs/  # 工具选择偏好

智能检索

不是简单的关键词匹配,是语义理解级别的检索:

import { AgentMemory } from 'agentmemory';
const memory = new AgentMemory();
const result = await memory.recall(
  "为什么之前没用这个API",
  { scope: 'codebase_memory', topK: 5, threshold: 0.7 }
);
// result: [
//   "第3会话:因为那个API在v2版本deprecated了,改用xxx替代",
//   "第7会话:这里有循环引用问题,已用xxx方案绕过",
// ]

自动上下文注入

新会话开始时自动注入相关记忆:

async function getContextPrompt(agent) {
  const context = await memory.getRelevantContext(
    agent.currentSession.projectPath,
    { maxTokens: 4000, prioritize: ['recent', 'relevant'] }
  );
  return `项目上下文:
${context}
以上是你之前的工作记录,请继续。`;
}

与同类工具对比

对比项agentmemoryMemGPTMem0Zep
专注领域✅ Coding Agent专用通用LLM通用LLMAI对话
语言✅ TypeScript原生Python多语言Python
Agent集成✅ Cursor/Cline/Copilot⚠️ 有限⚠️ 有限
基准测试✅ 真实场景驱动
团队共享✅ 支持⚠️ 企业版⚠️ 企业版
安装复杂度⭐ 低(npm包)⭐ 中⭐ 中⭐ 中

实战安装与使用

安装

npm install agentmemory
# 或 yarn add agentmemory 或 pnpm add agentmemory

基本使用

import { AgentMemory } from 'agentmemory';

const memory = new AgentMemory({ projectPath: '/path/to/your/project', storage: './.agentmemory' });

// 开启新会话时恢复上下文 await memory.initSession(); const context = await memory.getContext();

// 完成任务后保存记录 await memory.remember({ type: 'task_completed', content: '完成了订单模块的开发,包括创建/查询/取消订单API', tags: ['order', 'api', 'fastapi'] });

// 遇到问题时记录"坑" await memory.remember({ type: 'gotcha', content: '这里有循环引用问题,用EventEmitter模式绕过了', importance: 'high' });

// 关闭会话时总结 await memory.closeSession({ summary: '完成了订单模块,修复了3个bug' });

Claude Code集成

import { AgentMemory } from 'agentmemory';
const memory = new AgentMemory({ projectPath: process.cwd() });
memory.injectIntoPrompt({ maxLength: 3000, includeTypes: ['overview', 'gotcha'] });

Cursor集成

// .mcp.json
{
  "mcpServers": {
    "agentmemory": { "command": "npx", "args": ["agentmemory", "server"] }
  }
}

避坑指南

⚠️ 坑点1:存储空间无限增长

问题:记忆文件越来越大的,最后几MB的上下文根本装不下。

解决:定期压缩和清理

await memory.compact({ maxSize: '10MB', keepRecent: 50, keepHighImportance: true });
// 或设置自动压缩
const memory = new AgentMemory({ autoCompact: true, maxSize: '10MB' });

⚠️ 坑点2:敏感信息泄露

问题:代码库里的密码、API密钥被记到记忆里了。

解决:敏感信息过滤

const memory = new AgentMemory({
  sensitivePatterns: [/api[_-]?key/i, /password/i, /secret/i, /token/i],
  redactSensitive: true
});

⚠️ 坑点3:跨项目记忆混淆

问题:多个项目用同一个记忆,导致上下文混乱。

解决:严格的项目隔离

const memory = new AgentMemory({
  storage: `./.agentmemory/${projectName}`,
  projectId: projectName
});

⚠️ 坑点4:Token爆炸

解决:控制上下文长度

await memory.getContext({ maxTokens: 2000, prioritizeBy: 'recency' });

适用场景

不适用场景

总结

agentmemory解决的是一个真实存在的痛点——AI Coding Agent的失忆症。它不是通用记忆库,而是专门为Coding场景设计的,从多级记忆架构到智能检索,从自动上下文注入到多Agent同步,每个功能都直击痛点。

如果你每天都在用Cursor/Claude Code/Cline开发,重度用户强烈建议试试。它不能让你少写代码,但能让AI每次都"认识"你的项目,省去大量重复解释的时间。

项目地址https://github.com/rohitg00/agentmemory

Star:4,408 | License:MIT | 语言:TypeScript

/*]]>*/