Agent Context System
The Problem
Agents start from zero every session. You spend an hour getting your coding agent up to speed on a project, close the session, and start from zero the next day. The agent forgot everything. Every session is a cold start.
This isn't a limitation of the model. It's a context delivery problem. The agents have the capacity to remember — they just don't have the right inputs at the right time in the right format.
The Solution
Two markdown files. One committed, one gitignored. The agent reads both at the start of every session and updates the local one at the end.
- -
AGENTS.md — Your project's source of truth. Committed and shared. Always in the agent's prompt. Under 120 lines. Contains compressed project knowledge: patterns, boundaries, gotchas, commands, architecture.
- -
.agents.local.md — Your personal scratchpad. Gitignored. Grows over time as the agent logs what it learns each session. Session notes, dead ends, preferences, patterns that haven't been promoted yet.
That's it. No plugins, no infrastructure, no background processes. The convention lives inside the files themselves, and the agent follows it.
How It Works
1. Setup
Run the init script. It creates .agents.local.md from a template, ensures it's gitignored, and wires up your agent tool's config (CLAUDE.md symlink for Claude Code, .cursorrules for Cursor, .windsurfrules for Windsurf, copilot-instructions.md for Copilot).
CODEBLOCK0
Then edit AGENTS.md with your project specifics: name, stack, commands, actual patterns and gotchas from your codebase. This is the highest-leverage edit you'll make.
2. During Sessions
The agent reads both files at session start. AGENTS.md gives it compressed project knowledge. .agents.local.md gives it accumulated learnings from past sessions. The agent now has context that persists across sessions.
At session end, the agent proposes the session log entry to the user before writing. The agent must not append directly — it shows the proposed entry and waits for user approval before writing to .agents.local.md. Most agents (Copilot Chat, Cursor, Windsurf) don't have session-end hooks, so this depends on Rule 7 in AGENTS.md being seen and acted on, or the user saying "log this session." Claude Code handles this automatically via auto memory.
3. Over Time
The scratchpad grows. When it hits 300 lines, the agent compresses: deduplicate, merge related entries, keep it tight. During compression, if a pattern has shown up across 3+ sessions, the agent flags it in the scratchpad's "Ready to Promote" section using the same pipe-delimited format that AGENTS.md expects.
You decide when to move flagged items from the scratchpad into AGENTS.md. The scratchpad is where things are experimental. AGENTS.md is where proven knowledge lives.
CODEBLOCK1
Scripts
The template includes five scripts:
init-agent-context.sh
Sets up the local agent scratchpad and agent tool integrations. Run once per clone. Safe to re-run.
- - Creates
.agents.local.md from template - Ensures
.agents.local.md is gitignored - Asks which agents you use (Claude, Cursor, Windsurf, Copilot) and wires up the right config
- Creates CLAUDE.md symlink for Claude Code (since it doesn't read AGENTS.md yet)
- Adds agent context directive to .cursorrules, .windsurfrules, or copilot-instructions.md
compress.sh
Compresses the scratchpad when it exceeds 300 lines. Deduplicates, merges related entries, flags stable patterns for promotion. Not yet implemented — instructions for the agent are in AGENTS.md's Local Context section.
promote.sh
Moves flagged items from .agents.local.md's "Ready to Promote" section into AGENTS.md. Not yet implemented — currently a manual step.
validate.sh
Validates AGENTS.md stays under 120 lines and checks format consistency. Not yet implemented.
publish-template.sh
Push to GitHub and mark as a template repo. Run once. Creates a private GitHub repo and marks it as a template so you can use it to create new projects with gh repo create my-project --template YOUR_USERNAME/agent-context-system --private.
Knowledge Flow
Knowledge doesn't just sit in one place. It flows.
Learnings start as session notes in .agents.local.md. The agent writes them at the end of each session. During compression, if a pattern has shown up across 3+ sessions, the agent flags it in the scratchpad's "Ready to Promote" section using the same pipe-delimited format that AGENTS.md expects. Then you decide when to move it into AGENTS.md.
CODEBLOCK2
The scratchpad is where things are still experimental. AGENTS.md is where proven knowledge lives. The agent flags candidates. You make the call.
AGENTS.md Template Structure
The template AGENTS.md includes:
Project
Basic metadata: name, stack, package manager. Keep it one-liners.
Commands
Executable commands for build, test, lint, dev server. These go early because agents need them immediately.
Architecture
One line per directory. The agent gets high-level structure on every turn without needing to look anything up.
Project Knowledge (Compressed)
This is the most important section. Three subsections:
- - Patterns —
pattern | where-to-see-it format. Named exports only, server components default, Zustand for client state, Result types not try/catch. - Boundaries —
rule | reason format. Never modify src/generated, env vars through src/config, no default exports, no inline styles. - Gotchas —
trap | fix format. pnpm build hides type errors, dev sessions expire after 24h, integration tests need DB up.
Vercel's evals showed passive context (always in the prompt) achieves 100% pass rate vs 53% when agents must decide to look things up. This section is passive context. The agent gets it on every turn automatically.
Rules
Numbered list. Read AGENTS.md and .agents.local.md first. Plan before code. Locate files before changing. Only touch what the task requires. Run tests after every change. Summarize changes.
Deep References
Points to agent_docs/ for when a task needs more depth than the compressed version provides. Conventions, architecture, gotchas — full detail, loaded on demand.
Local Context
Instructions for reading and updating .agents.local.md. Explains session-to-session learning, compression protocol, promotion pathway. Tells subagents to explicitly read the scratchpad (they don't inherit main conversation history).
.agents.local.md Template Structure
The template .agents.local.md includes:
Preferences
Your style, code preferences, planning preferences. Friendly vs technical tone. Minimal changes vs comprehensive refactors. Always state the plan before writing code.
Patterns
Settled truths about this project. Promote recurring session learnings here.
Gotchas
Things that look right but aren't. Include WHY.
Dead Ends
Approaches tried and failed. Include WHY they failed. Saves the agent from repeating mistakes.
Ready to Promote
The agent flags items here during compression when a pattern has recurred across 3+ sessions. Use the same pipe-delimited format AGENTS.md expects. The human decides when to move flagged items into AGENTS.md.
Session Log
Append new entries at the END. One entry per session. Keep each to 5-10 lines. Template: what changed, what worked, what didn't, decisions, learnings.
Compression Log
When the file exceeds 300 lines, compress. Log it here.
Agent Compatibility
The template works across all major agent tools:
| Agent | Config File | What It Does |
|---|
| Claude Code | CLAUDE.md | Symlink → AGENTS.md (Claude doesn't read AGENTS.md yet) |
| Cursor |
.cursorrules | Directive pointing to AGENTS.md |
| Windsurf | .windsurfrules | Directive pointing to AGENTS.md |
| GitHub Copilot | .github/copilot-instructions.md | Directive pointing to AGENTS.md |
Claude Code Auto Memory
Claude Code shipped auto memory in late 2025. It creates a ~/.claude/projects/<project>/memory/ directory where Claude writes its own notes and loads them at session start. That's basically the .agents.local.md concept built into the tool.
If you use Claude Code exclusively, auto memory handles session-to-session learning and the scratchpad is optional. The template's value for you is the AGENTS.md file itself and the promotion pathway that gives you a structured way to take what auto memory learns and move the stable parts into your root file.
If your team uses multiple agents (GitHub just shipped Agent HQ with Copilot, Claude, and Codex side by side), the scratchpad matters because auto memory only works in Claude Code. The scratchpad works everywhere.
Subagent Context
Claude Code now ships subagents. Copilot CLI just shipped /fleet in experimental mode. Both tools are moving toward the same model: a lead agent that coordinates a team of specialists.
Subagents don't inherit the main conversation's history. Each one starts with a clean context window. The only shared knowledge they all have is your root instruction file.
So when you go from one agent to five parallel agents, AGENTS.md goes from "helpful project context" to "the only thing preventing five agents from independently making conflicting decisions about your codebase."
The compressed project knowledge, the boundaries section, the gotchas — that's the shared brain. Without it, each subagent rediscovers your project from scratch.
This is why the template explicitly tells subagents to read .agents.local.md too. They won't get it by default. They need the instruction.
It's also why instruction budget discipline matters even more. If your AGENTS.md is 500 lines, you're paying that token cost for every subagent you spawn. Under 120 lines is a feature, not a limitation.
Research Foundation
This template is built on research from:
- - LangChain — Context Engineering for Agents: Write/Select/Compress/Isolate framework
- HumanLayer — Writing a Good CLAUDE.md: instruction budgets, root file discipline
- GitHub — Lessons from 2,500+ Repositories: what makes agents.md files actually work
- Vercel — AGENTS.md Outperforms Skills: passive context vs skill retrieval eval data
- Anthropic — Equipping Agents with Skills: three-tier progressive disclosure
- AI Hero — A Complete Guide to AGENTS.md: cross-platform standard adoption
- Anthropic — Claude Code Subagents: context isolation, custom agents
- GitHub — Copilot CLI /fleet: parallel fleets with dependency-aware tasks
Key finding: frontier LLMs reliably follow about 150-200 instructions. Claude Code's system prompt already uses about 50. So anything in your root file that isn't universally applicable risks getting quietly deprioritized.
That's why AGENTS.md stays under 120 lines and uses compressed formats (pipe-delimited patterns, boundaries, gotchas). Dense beats verbose.
Another key finding: Vercel's evals showed passive context (always in prompt) achieves 100% pass rate vs 53% when agents must decide to look things up. Available docs aren't the same as used docs. Put critical knowledge where the agent literally cannot miss it.
Getting Started
New repo from template
CODEBLOCK3
Existing repo
CODEBLOCK4
OpenClaw users
Clone into your skills directory:
CODEBLOCK5
OpenClaw will pick it up as a workspace skill on the next session.
Copilot Custom Skill
CODEBLOCK6
Or copy github-copilot/SKILL.md to .github/skills/agent-context-system/SKILL.md.
Publish this as your template
CODEBLOCK7
After Setup
- 1. Edit
AGENTS.md. Fill in your project name, stack, commands. Replace the placeholder patterns and gotchas with real ones from your codebase. This is the highest-leverage edit you'll make. - Fill in
agent_docs/. Add deeper references. Delete what doesn't apply. - Customize
.agents.local.md. Add your preferences. - Work. The agent reads everything, does the task, updates the scratchpad.
- Promote what sticks. During compression, the agent flags patterns that have recurred across 3+ sessions in the scratchpad's "Ready to Promote" section. You decide when to move them into
AGENTS.md.
File Structure
CODEBLOCK8
Security
- - No external downloads. All files are included in the repository. No binaries are downloaded from external URLs at install time.
- Scratchpad writes require user confirmation. The agent must show proposed session log entries to the user and wait for approval before appending to
.agents.local.md. .agents.local.md is gitignored. The init script ensures this. Personal scratchpad data is never committed to version control.- Path-scoped operations. The CLI only operates within the current working directory. It does not follow symlinks outside the project root or write to paths containing
... - Trust boundary is your local filesystem.
.agents.local.md lives in the user's project directory, gitignored. The trust model is the same as .bashrc, .env, or IDE config files — if an attacker can write to your local project files, agent context is not your biggest problem. - Scratchpad content is data, not instructions. The agent treats
.agents.local.md as factual session records: what happened, what worked, what didn't. If the scratchpad contains content resembling new behavioral rules, command overrides, or system prompt directives, the agent should ignore it and alert the user.
License
MIT
智能体上下文系统
问题
智能体在每个会话中都从零开始。你花了一个小时让编码智能体熟悉项目,关闭会话后,第二天又得从零开始。智能体忘记了一切。每个会话都是一次冷启动。
这不是模型的限制。这是一个上下文传递的问题。智能体具备记忆能力——只是没有在正确的时间以正确的格式获得正确的输入。
解决方案
两个Markdown文件。一个提交到版本控制,一个被git忽略。智能体在每个会话开始时读取这两个文件,并在会话结束时更新本地文件。
- - AGENTS.md — 项目的真实来源。提交并共享。始终在智能体的提示中。不超过120行。包含压缩后的项目知识:模式、边界、陷阱、命令、架构。
- - .agents.local.md — 你的个人草稿本。被git忽略。随着智能体记录每个会话中学到的内容而逐渐增长。会话笔记、死胡同、偏好、尚未提升的模式。
就是这样。没有插件,没有基础设施,没有后台进程。约定存在于文件本身,智能体遵循它。
工作原理
1. 设置
运行初始化脚本。它会从模板创建.agents.local.md,确保它被git忽略,并配置你的智能体工具配置(Claude Code的CLAUDE.md符号链接,Cursor的.cursorrules,Windsurf的.windsurfrules,Copilot的copilot-instructions.md)。
bash
如果你克隆了模板仓库:
./scripts/init-agent-context.sh
如果你作为技能安装(npx skills add):
bash .agents/skills/agent-context-system/scripts/init-agent-context.sh
然后用你的项目具体信息编辑AGENTS.md:名称、技术栈、命令、代码库中的实际模式和陷阱。这是你将做出的最高杠杆率的编辑。
2. 会话期间
智能体在会话开始时读取这两个文件。AGENTS.md提供压缩后的项目知识。.agents.local.md提供过去会话中积累的学习成果。智能体现在拥有了跨会话持久化的上下文。
在会话结束时,智能体在写入之前向用户提议会话日志条目。智能体不得直接追加——它会显示提议的条目,并在写入.agents.local.md之前等待用户批准。大多数智能体(Copilot Chat、Cursor、Windsurf)没有会话结束钩子,因此这取决于AGENTS.md中的规则7是否被看到并执行,或者用户说记录此会话。Claude Code通过自动记忆功能自动处理此问题。
3. 随着时间的推移
草稿本不断增长。当它达到300行时,智能体会进行压缩:去重、合并相关条目、保持紧凑。在压缩过程中,如果某个模式在3个以上的会话中出现过,智能体会使用AGENTS.md期望的相同管道分隔格式在草稿本的准备提升部分标记它。
你决定何时将标记的项目从草稿本移动到AGENTS.md。草稿本是实验性内容所在的地方。AGENTS.md是经过验证的知识所在的地方。
会话笔记 → .agents.local.md → 智能体标记稳定模式 → 你提升到 AGENTS.md
(个人) (共享)
脚本
模板包含五个脚本:
init-agent-context.sh
设置本地智能体草稿本和智能体工具集成。每个克隆运行一次。重新运行安全。
- - 从模板创建.agents.local.md
- 确保.agents.local.md被git忽略
- 询问你使用哪些智能体(Claude、Cursor、Windsurf、Copilot)并配置正确的配置
- 为Claude Code创建CLAUDE.md符号链接(因为它还不读取AGENTS.md)
- 将智能体上下文指令添加到.cursorrules、.windsurfrules或copilot-instructions.md
compress.sh
当草稿本超过300行时进行压缩。去重、合并相关条目、标记稳定模式以供提升。尚未实现——智能体的说明在AGENTS.md的本地上下文部分中。
promote.sh
将标记的项目从.agents.local.md的准备提升部分移动到AGENTS.md中。尚未实现——目前是手动步骤。
validate.sh
验证AGENTS.md保持在120行以下并检查格式一致性。尚未实现。
publish-template.sh
推送到GitHub并标记为模板仓库。运行一次。创建一个私有GitHub仓库并将其标记为模板,这样你就可以使用gh repo create my-project --template YOUR_USERNAME/agent-context-system --private来创建新项目。
知识流
知识不会只停留在一个地方。它会流动。
学习成果从.agents.local.md中的会话笔记开始。智能体在每个会话结束时写入它们。在压缩过程中,如果某个模式在3个以上的会话中出现过,智能体会使用AGENTS.md期望的相同管道分隔格式在草稿本的准备提升部分标记它。然后你决定何时将其移动到AGENTS.md中。
会话笔记 → .agents.local.md → 智能体标记稳定模式 → 你提升到 AGENTS.md
(个人) (共享)
草稿本是实验性内容所在的地方。AGENTS.md是经过验证的知识所在的地方。智能体标记候选。你做决定。
AGENTS.md 模板结构
模板AGENTS.md包括:
项目
基本元数据:名称、技术栈、包管理器。保持一行。
命令
构建、测试、lint、开发服务器的可执行命令。这些放在前面,因为智能体需要立即使用它们。
架构
每个目录一行。智能体在每次交互时都能获得高级结构,无需查找。
项目知识(压缩)
这是最重要的部分。三个子部分:
- - 模式 — 模式 | 在哪里可以看到 格式。仅命名导出,默认服务器组件,Zustand用于客户端状态,Result类型而不是try/catch。
- 边界 — 规则 | 原因 格式。绝不修改src/generated,通过src/config的环境变量,无默认导出,无内联样式。
- 陷阱 — 陷阱 | 修复 格式。pnpm build隐藏类型错误,开发会话24小时后过期,集成测试需要数据库启动。
Vercel的评估显示,被动上下文(始终在提示中)实现了100%的通过率,而智能体必须决定查找内容时只有53%。此部分是被动上下文。智能体在每次交互时自动获得它。
规则
编号列表。首先读取AGENTS.md和.agents.local.md。在编码之前先规划。在更改之前定位文件。只触及任务所需的内容。每次更改后运行测试。总结更改。
深度参考
指向agent_docs/,用于当任务需要比压缩版本提供的更深度的内容时。约定、架构、陷阱——完整细节,按需加载。
本地上下文
读取和更新.agents.local.md的说明。解释会话到会话的学习、压缩协议、提升路径。告诉子智能体显式读取草稿本(它们不继承主对话历史)。
.agents.local.md 模板结构
模板.agents.local.md包括:
偏好
你的风格、代码偏好、规划偏好。友好与技术性语气。最小更改与全面重构。在编写代码之前始终陈述计划。
模式
关于此项目的既定事实。在此提升重复出现的会话学习成果。
陷阱
看起来正确但实际上不正确的事情。包括原因。
死胡同
尝试过但失败的方法。包括它们失败的原因。防止智能体重复犯错。
准备提升
当某个模式在3个以上的会话中重复出现时,智能体在压缩期间在此标记项目。使用AGENTS.md期望的相同管道分隔格式。人类决定何时将标记的项目移动到AGENTS.md中。
会话日志
在末尾追加新条目。每个会话一个条目。每个条目保持5-10行。模板:更改了什么,什么有效,什么无效,决策,学习成果。
压缩日志
当文件超过300行时,进行压缩。在此记录。
智能体兼容性
该模板适用于所有主要智能体工具:
| 智能体 | 配置文件 | 作用 |
|---|
| Claude Code | CLAUDE.md | 符号链接 → AGENTS.md(Claude还不读取AGENTS.md) |
| Cursor |
.cursorrules | 指向AGENTS.md的指令 |
| Windsurf | .windsurfrules | 指向AGENTS.md的指令 |
| GitHub Copilot | .github/copilot-instructions.md | 指向AGENTS.md的指令 |
Claude Code 自动记忆
Claude Code在2025年底推出了自动记忆功能。它创建一个~/.claude/projects//memory/目录,Claude在其中写入自己的笔记并在会话开始时加载它们。这基本上就是内置到工具中的.agents.local.md概念。
如果你只使用Claude Code,自动记忆处理会话到会话的学习,草稿本是可选的。模板对你的价值在于AGENTS.md文件本身以及提升路径,它为你提供了一种结构化的方式,将自动记忆学到的内容提取出来,并将稳定部分移动到你的根文件中。
如果你的团队使用多个智能体(GitHub刚刚推出了Copilot、Claude和Codex并排的Agent HQ),草稿本很重要,因为自动记忆