微信扫码
添加专属顾问
我要投稿
Cursor通过创新的「文件化」策略实现动态上下文发现,将Token消耗降低47%,同时提升回复质量。 核心内容: 1. 动态上下文发现的优势与实现原理 2. 文件作为统一抽象的架构设计 3. 五大动态上下文策略的具体应用与收益
拆解 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 primitive to 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 the coding agents to reduce context usage.(减少上下文使用是编码 Agent 的责任,而非期望每个 MCP 服务器自己优化)
Cursor 做法:
关键设计决策(来自官方脚注):
We considered a tool search approach, but that would scatter tools across a flat index. Instead, we create one 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, but discovered dynamically rather than injected statically.
这与 CLI 编码 Agent(如 Claude Code)看到的内容一致——之前的 Shell 输出在上下文中,但是动态发现而非静态注入。
除了 5 大动态策略,Cursor 还针对文件/文件夹设计了智能压缩机制(来自 Cursor 官方文档):
| Condensed | |||
| Significantly Condensed | |||
| Not Included |
常规压缩示例:
// 原始文件(500 行)
export class UserService {
private db: Database;
constructor(db: Database) { /* ... 50行 ... */ }
async createUser(data: CreateUserDTO): Promise<User> { /* ... 100行 ... */ }
async updateUser(id: string, data: UpdateUserDTO): Promise<User> { /* ... 80行 ... */ }
}
// 压缩后(~20 行)
export class UserService {
private db: Database;
constructor(db: Database): void;
async createUser(data: CreateUserDTO): Promise<User>;
async updateUser(id: string, data: UpdateUserDTO): Promise<User>;
}
静态注入:「宁滥勿缺」,大量无关信息占用上下文 动态发现:「按需精准」,只拉取当前任务需要的信息
Agent 不需要学习新 API,直接复用 tail、grep、rg、jq 等标准工具。
新增信息源?写入文件即可。新增 MCP Server?同步到对应文件夹。架构天然支持水平扩展。
| 长工具响应 | tail 按需读取 | |
| 对话历史 | ||
| Agent 能力扩展 | ||
| MCP 工具 | ||
| 终端输出 | grep | |
| 大文件处理 |
Cursor 的动态上下文发现架构可以用一句话概括:
用文件作为统一抽象,将「静态注入」转变为「动态发现」,实现 token 效率与响应质量的双重提升。
核心数据点:
架构精髓:不在于复杂算法,而在于用最简单的原语(文件)解决最普遍的问题(上下文膨胀)。
53AI,企业落地大模型首选服务商
产品:场景落地咨询+大模型应用平台+行业解决方案
承诺:免费POC验证,效果达标后再合作。零风险落地应用大模型,已交付160+中大型企业
2026-01-08
Prompt 里的攻防战:给System Prompt穿上防弹衣的 3 道防线
2026-01-07
GenAI输出内容控制的5种设计模式
2026-01-07
Agent创业者的机会窗口来了!
2026-01-06
Noi v1.0.0 正式发布!
2026-01-05
智能体就是刚入职一家公司的新员工——智能体核心流程揭秘
2026-01-05
别再把 Prompt 写死在代码里了:如何构建动态模板库?
2026-01-04
别再问“怎么写 Skill.md”了,直接抄生产级的Skills 库
2026-01-04
90 个工具、5 万 tokens,Claude 用 Skills 解决了 MCP 的致命缺陷
2025-11-20
2025-11-15
2025-11-15
2025-11-12
2025-10-27
2025-12-02
2025-10-31
2025-11-15
2025-11-03
2025-10-12
2026-01-05
2025-12-30
2025-12-26
2025-12-15
2025-12-11
2025-12-06
2025-09-02
2025-08-11