拆解 Cursor 的上下文工程架构——从动态发现到智能压缩的完整技术方案
编码 Agent 正在快速改变软件构建方式。其能力提升来自两方面:更强的 Agent 模型+更好的上下文工程(Context Engineering)。
Cursor 的 Agent 框架(agent harness)会针对每个前沿模型单独优化,但有一类上下文工程改进是模型无关的——如何收集上下文、如何在长轨迹中优化 token 使用。
Cursor 团队发现:随着模型变强,预先提供更少细节、让 Agent 自主拉取相关上下文,效果反而更好。
这就是动态上下文发现(Dynamic Context Discovery)的核心思想,与传统的静态上下文(Static Context)(始终包含在 prompt 中)形成对比。
| 静态上下文 | ||
| 动态上下文 |
动态上下文发现的优势:
Cursor 实现动态上下文发现的核心载体是文件(Files):
为什么选择文件?
官方博客原文:
It's not clear if files will be the final interface for LLM-based tools. But as coding agents quickly improve,files have been a simple and powerful primitiveto use, and a safer choice than yet another abstraction that can't fully account for the future.
文件的优势:
tail、grep、rg、jq等标准工具开箱即用问题:工具调用(Shell 命令、MCP 调用)可能返回巨大的 JSON 响应,显著膨胀上下文窗口。
常见做法:截断长输出 → 可能丢失重要信息。
Cursor 做法:
关键细节:
tail检查末尾(多数结论在末尾),按需读取更多收益:减少接近上下文上限时触发的不必要摘要。
问题:上下文窗口填满后触发摘要(summarization),但摘要是有损压缩,Agent 可能遗忘关键细节。
Cursor 做法:
transcript.txt)图示:Agent 完成实验后上传结果到 S3,触发上下文摘要。后续用户请求更新元数据时,Agent 发现摘要中缺失 S3 路径,通过搜索
agent transcript恢复了完整信息。
触发时机:
/summarizeAgent Skills是由Anthropic 提出的开放标准,用于为编码 Agent 扩展专用能力。Cursor支持(Supporting)该标准。
注:Agent Skills 标准已被 Cursor、Claude Code、GitHub Copilot、VS Code 等主流工具采用,具备跨平台可移植性。
Skills 的构成(遵循 Anthropic 规范):
<!-- SKILL.md -->
---
name: deploy-k8s
description: 将应用部署到 Kubernetes 集群
---
# Deploy to Kubernetes
## 使用说明
...
## 可执行脚本
-./scripts/deploy.sh
动态发现机制:
grep或 Cursor 的语义搜索拉取完整 Skill 定义问题:
Cursor 的设计哲学:
We believe it's the responsibility of thecoding agentsto reduce context usage.(减少上下文使用是编码 Agent 的责任,而非期望每个 MCP 服务器自己优化)
Cursor 做法:
关键设计决策(来自官方脚注):
We considered a tool search approach, but that would scatter tools across a flat index. Instead, we createone folder per server, keeping each server's tools logically grouped. When the model lists a folder, it sees all tools from that server together and can understand them as a cohesive unit. Files also enable more powerful searching. The agent can use full
rgparameters or evenjqto filter tool descriptions.
rg参数、jq过滤实测效果:
在调用 MCP 工具的运行中,该策略将 Agent **总 token 消耗减少 46.9%**(统计显著,但会随已安装 MCP 数量产生较大波动)
额外收益:可向 Agent 传达工具状态。例如,如果 MCP 服务器需要重新认证,以前 Agent 会完全忘记这些工具,用户会困惑;现在 Agent 可以主动提示用户重新认证。
传统做法:用户需要手动复制终端输出粘贴给 Agent。
Cursor 做法:
grep只获取相关输出官方原文:
This mirrors what CLI-based coding agents see, with prior shell output in context, butdiscovered dynamically rather than injected statically.
这与 CLI 编码 Agent(如 Claude Code)看到的内容一致——之前的 Shell 输出在上下文中,但是动态发现而非静态注入。
除了 5 大动态策略,Cursor 还针对文件/文件夹设计了智能压缩机制(来自 Cursor 官方文档):
| Condensed | |||
| Significantly Condensed | |||
| Not Included |
常规压缩示例:
// 原始文件(500 行)
exportclassUserService {
privatedb: Database;
constructor(db: Database) {/* ... 50行 ... */}
asynccreateUser(data: CreateUserDTO)
romise<User> {/* ... 100行 ... */}
asyncupdateUser(id:string, data: UpdateUserDTO)
romise<User> {/* ... 80行 ... */}
}
// 压缩后(~20 行)
exportclassUserService {
privatedb: Database;
constructor(db: Database): void;
asynccreateUser(data: CreateUserDTO)
romise<User>;
asyncupdateUser(id:string, data: UpdateUserDTO)
romise<User>;
}
静态注入:「宁滥勿缺」,大量无关信息占用上下文 动态发现:「按需精准」,只拉取当前任务需要的信息
Agent 不需要学习新 API,直接复用tail、grep、rg、jq等标准工具。
新增信息源?写入文件即可。新增 MCP Server?同步到对应文件夹。架构天然支持水平扩展。
| 长工具响应 | tail按需读取 | |
| 对话历史 | ||
| Agent 能力扩展 | ||
| MCP 工具 | ||
| 终端输出 | grep | |
| 大文件处理 |
Cursor 的动态上下文发现架构可以用一句话概括:
用文件作为统一抽象,将「静态注入」转变为「动态发现」,实现 token 效率与响应质量的双重提升。
核心数据点:
架构精髓:不在于复杂算法,而在于用最简单的原语(文件)解决最普遍的问题(上下文膨胀)。
| 欢迎光临 链载Ai (https://www.lianzai.com/) | Powered by Discuz! X3.5 |