{
  "schemaVersion": "1.0",
  "schemaUrl": "https://www.yingxueyuan.cn/ai-directory.schema.json#/$defs/guideContentDocument",
  "name": "AI Agent 项目上下文太长怎么办？压缩、交接与省 Token 指南｜映雪网指南机器可读正文",
  "description": "单篇指南结构化内容，含可见导语、结论、分节正文与逐节来源。",
  "siteUrl": "https://www.yingxueyuan.cn",
  "lastContentUpdate": "2026-10-25",
  "guides": [
    {
      "id": "agent-project-context",
      "contentUrl": "https://www.yingxueyuan.cn/ai-content/agent-project-context.json",
      "title": "AI Agent 项目上下文太长怎么办？压缩、交接与省 Token 指南",
      "author": {
        "@type": "Organization",
        "@id": "https://www.yingxueyuan.cn/#editorial-team",
        "name": "映雪网编辑部",
        "url": "https://www.yingxueyuan.cn/editorial-policy/",
        "parentOrganization": {
          "@id": "https://www.yingxueyuan.cn/#organization"
        }
      },
      "publisher": {
        "@type": "Organization",
        "@id": "https://www.yingxueyuan.cn/#organization",
        "name": "映雪园智能科技",
        "alternateName": "映雪网",
        "brand": {
          "@type": "Brand",
          "@id": "https://www.yingxueyuan.cn/#brand-yingxuewang",
          "name": "映雪网",
          "url": "https://www.yingxueyuan.cn/"
        },
        "url": "https://www.yingxueyuan.cn/company/"
      },
      "description": "区分上下文占用与累计用量，用精简项目规则、阶段状态摘要和按需资料管理长项目；附 Codex、Claude Code、Cursor 差异与可复制交接模板。",
      "summary": "别把整段旧对话、完整路线图和所有日志塞进每次任务。把长期约束写进精简项目规则，把当前事实和验证记录放进状态文件，细节资料按任务读取；换对话时交接目标、边界、证据和下一步。",
      "intro": "长项目最容易出现两种浪费：每轮把同一批规则、旧记录和整份代码重新喂给 Agent；或为了省上下文只留下“继续上次工作”，结果新对话不知道哪些事实已验证、哪些只是计划。更稳妥的办法不是盲目缩短所有内容，而是把信息分层：稳定规则常驻、当前状态精简、详细资料按任务加载。",
      "readTime": "8 分钟",
      "updatedAt": "2026-10-25",
      "url": "https://www.yingxueyuan.cn/docs/agent-project-context/",
      "quickAnswer": {
        "text": "先分清“当前上下文占用”和“累计 token/费用”：二者的显示和计费因产品而异，压缩上下文不等于撤销已发生的用量。降低后续消耗的通用做法是：常驻规则只保留稳定约束与资料索引；每个里程碑更新短状态文件；长文档、历史日志和代码只在当前任务需要时按需读取。换对话时交接当前事实和证据，不要复制整段历史。",
        "source": {
          "label": "OpenAI Developers：避免无关常驻指令和过量上下文",
          "url": "https://developers.openai.com/blog/rethinking-skills-and-prompts-for-gpt-6-astra"
        },
        "detailsUrl": "https://www.yingxueyuan.cn/docs/agent-project-context/#handoff-template"
      },
      "relatedGuides": [
        {
          "title": "编码 Agent 怎么选？本机协作、云端改代码与审查流程",
          "url": "https://www.yingxueyuan.cn/docs/choose-coding-agent/"
        },
        {
          "title": "AI Agent 是什么？和 AI 助手、自动化工作流有什么区别",
          "url": "https://www.yingxueyuan.cn/docs/what-is-ai-agent/"
        },
        {
          "title": "怎么安全试用 AI Agent？权限、数据和人工确认清单",
          "url": "https://www.yingxueyuan.cn/docs/choose-ai-agent-safely/"
        }
      ],
      "relatedTools": [
        {
          "name": "AI Agent 项目上下文交接摘要生成器",
          "url": "https://www.yingxueyuan.cn/tools/agent-context-handoff/",
          "kind": "site-utility"
        },
        {
          "name": "OpenBMB PilotDeck",
          "url": "https://www.yingxueyuan.cn/tools/openbmb-pilotdeck/",
          "kind": "ai-product-entry"
        }
      ],
      "relatedNews": [
        {
          "id": "openbmb-pilotdeck-2-launch-2026-10-15",
          "title": "PilotDeck 2.0 发布：用项目工作区隔离 Agent 文件与记忆",
          "url": "https://www.yingxueyuan.cn/news/openbmb-pilotdeck-2-launch-2026-10-15/",
          "contentUrl": "https://www.yingxueyuan.cn/ai-content/news/openbmb-pilotdeck-2-launch-2026-10-15.json"
        }
      ],
      "sources": [
        {
          "label": "OpenAI Developers：重新审视 Codex 的技能、AGENTS.md 与提示词，减少上下文膨胀",
          "url": "https://developers.openai.com/blog/rethinking-skills-and-prompts-for-gpt-6-astra"
        },
        {
          "label": "Anthropic 官方：管理 Claude Code 内存与 CLAUDE.md",
          "url": "https://code.claude.com/docs/zh-CN/memory"
        },
        {
          "label": "Claude Code 官方命令说明：查看上下文并用 /compact 压缩",
          "url": "https://code.claude.com/docs/zh-CN/commands"
        },
        {
          "label": "Cursor 官方：在 Agent 提示中附加文件、终端、对话与 Git diff 上下文",
          "url": "https://cursor.com/docs/agent/prompting"
        },
        {
          "label": "Cursor 官方：项目规则的作用范围与应用方式",
          "url": "https://cursor.com/docs/rules"
        },
        {
          "label": "OpenAI API：长会话 Compaction 机制（仅适用于该 API 文档描述的接口）",
          "url": "https://developers.openai.com/api/docs/guides/compaction"
        }
      ],
      "sections": [
        {
          "id": "understand-usage",
          "heading": "先分清上下文、累计用量和费用",
          "detailsUrl": "https://www.yingxueyuan.cn/docs/agent-project-context/#understand-usage",
          "paragraphs": [
            "“上下文太大”通常指当前这次生成要使用的信息太多，可能包含系统说明、项目规则、技能说明、对话历史、粘贴内容、文件和工具输出；具体哪些内容被加载、界面如何显示，由产品和模型决定。",
            "累计输入/输出 token、订阅额度、API 账单和当前上下文占用不是同一个指标。OpenAI API 的 Compaction 文档说明，压缩可减少后续运行携带的上下文大小，并帮助平衡成本与延迟；这不能推导成其他桌面产品都按同一方式计数，也不代表已发生的请求用量会被退还。请看你正在使用的平台自己的用量页面和计费说明。",
            "信息越多也不总是越好。OpenAI 开发者文章提醒，长期累积且不再相关的提示词、规则和技能会占用上下文并引入噪声；目标应是保留完成当前任务所需的证据，而不是追求把整个项目永久塞进模型。"
          ],
          "sources": [
            {
              "label": "OpenAI API：长会话 Compaction 机制（仅适用于该 API 文档描述的接口）",
              "url": "https://developers.openai.com/api/docs/guides/compaction"
            },
            {
              "label": "OpenAI Developers：重新审视 Codex 的技能、AGENTS.md 与提示词，减少上下文膨胀",
              "url": "https://developers.openai.com/blog/rethinking-skills-and-prompts-for-gpt-6-astra"
            }
          ],
          "editorialNote": false
        },
        {
          "id": "stop-context-growth",
          "heading": "本轮先止住上下文继续膨胀",
          "detailsUrl": "https://www.yingxueyuan.cn/docs/agent-project-context/#stop-context-growth",
          "paragraphs": [],
          "bullets": [
            "把任务收窄成一个可验收结果：说明目标、允许改动范围和验收方式；不要让 Agent 每轮重述全部项目历史。",
            "代码按关键词和相关行定位；优先看工作树状态、目标文件片段、实际报错与相关测试，不要无差别读取整个仓库、整份超长路线图或全部构建日志。",
            "同一事实只维护一个权威位置。历史过程留在历史记录，当前状态文件只保留仍影响决策的事实；不要每轮复制旧摘要。",
            "需要背景时先查索引，再读当前任务相关的资料；避免常驻规则要求“开始任何任务前通读所有文档”。",
            "大阶段完成后保存可验证状态，再考虑压缩或新开对话；未提交变更、分支和测试结果要写清楚。"
          ],
          "sources": [
            {
              "label": "OpenAI Developers：重新审视 Codex 的技能、AGENTS.md 与提示词，减少上下文膨胀",
              "url": "https://developers.openai.com/blog/rethinking-skills-and-prompts-for-gpt-6-astra"
            },
            {
              "label": "Cursor 官方：在 Agent 提示中附加文件、终端、对话与 Git diff 上下文",
              "url": "https://cursor.com/docs/agent/prompting"
            }
          ],
          "editorialNote": false
        },
        {
          "id": "three-layer-memory",
          "heading": "把项目资料分成三层管理",
          "detailsUrl": "https://www.yingxueyuan.cn/docs/agent-project-context/#three-layer-memory",
          "paragraphs": [
            "可记成一句话：“规则告诉 Agent 怎么做，状态告诉 Agent 现在在哪，参考资料告诉 Agent 必要时去哪里查。”项目文件名按工具调整；这些文件不能代替检查当前代码、分支和运行结果。"
          ],
          "table": {
            "headers": [
              "层级",
              "放什么",
              "避免放什么"
            ],
            "rows": [
              [
                "常驻项目规则",
                "少量稳定边界、关键命令、代码约定，以及“哪类任务读哪份文档”的索引。",
                "每日流水、完整技术历史、所有模块说明、与当前任务无关的规则。"
              ],
              [
                "当前状态/交接摘要",
                "唯一目标、边界、当前工作树事实、已完成且验证的结果、未决项、下一动作。",
                "没有验证的计划、过期数量、整段聊天复述、没有证据的“已完成”。"
              ],
              [
                "按需参考资料",
                "架构、产品规则、操作手册、完整路线图、旧决策和长日志；由当前任务决定读取哪份。",
                "假设 Agent 每次都会自动读完，或把参考资料复制到每个提示中。"
              ]
            ]
          },
          "sources": [
            {
              "label": "OpenAI Developers：重新审视 Codex 的技能、AGENTS.md 与提示词，减少上下文膨胀",
              "url": "https://developers.openai.com/blog/rethinking-skills-and-prompts-for-gpt-6-astra"
            },
            {
              "label": "Anthropic 官方：管理 Claude Code 内存与 CLAUDE.md",
              "url": "https://code.claude.com/docs/zh-CN/memory"
            },
            {
              "label": "Cursor 官方：项目规则的作用范围与应用方式",
              "url": "https://cursor.com/docs/rules"
            }
          ],
          "editorialNote": false
        },
        {
          "id": "tool-differences",
          "heading": "Codex、Claude Code、Cursor 的常驻规则不完全一样",
          "detailsUrl": "https://www.yingxueyuan.cn/docs/agent-project-context/#tool-differences",
          "paragraphs": [],
          "bullets": [
            "Codex：OpenAI 开发者文章建议定期审视 `AGENTS.md`、技能和任务提示，避免无关指令永久常驻；特定任务才需要的说明留在对应文档，按需读取。不同 Codex 形态与版本的加载细节以当前产品说明为准。",
            "Claude Code：Anthropic 文档说明启动时加载发现到的内存文件；嵌套目录的 `CLAUDE.md` 可在读取对应目录文件时再纳入。根部长期说明越长，越可能成为每个任务反复携带的内容。",
            "Cursor：官方规则支持 Always Apply、Apply Intelligently、Apply to Specific Files 和 Apply Manually 等作用范围；另可在提示中引用具体文件、文件夹、终端、历史对话或 Git diff。把只适用于特定目录或流程的规则设为相应范围。",
            "产品和版本会更新；不要照搬其他工具的命令、压缩阈值或 token 百分比，先看当前版本界面和官方文档。"
          ],
          "sources": [
            {
              "label": "OpenAI Developers：重新审视 Codex 的技能、AGENTS.md 与提示词，减少上下文膨胀",
              "url": "https://developers.openai.com/blog/rethinking-skills-and-prompts-for-gpt-6-astra"
            },
            {
              "label": "Anthropic 官方：管理 Claude Code 内存与 CLAUDE.md",
              "url": "https://code.claude.com/docs/zh-CN/memory"
            },
            {
              "label": "Cursor 官方：项目规则的作用范围与应用方式",
              "url": "https://cursor.com/docs/rules"
            },
            {
              "label": "Cursor 官方：在 Agent 提示中附加文件、终端、对话与 Git diff 上下文",
              "url": "https://cursor.com/docs/agent/prompting"
            }
          ],
          "editorialNote": false
        },
        {
          "id": "handoff-template",
          "heading": "换对话时复制这份短交接模板",
          "detailsUrl": "https://www.yingxueyuan.cn/docs/agent-project-context/#handoff-template",
          "paragraphs": [
            "把模板保存在项目中容易找到的位置。每次只更新仍然有效的事实；新对话先读它，再检查当前工作树，不要把旧计划当成已完成。"
          ],
          "prompt": "【项目目标】\n长期目标：\n本轮具体结果：\n\n【边界】\n允许做什么：\n不要做什么（例如部署、推送、改数据库）：\n\n【当前事实】\n分支/工作树状态：\n关键文件及作用：\n\n【已完成并验证】\n改动：\n验证命令与实际结果：\n\n【尚未解决】\n问题或未知项：\n\n【下一步】\n一个可立刻执行的动作：\n\n请先核对当前代码和工作树再继续。不要假设历史计划已经执行；若摘要与代码、命令结果或当前授权不一致，以当前证据为准。",
          "note": "不要把密码、API Key、客户资料或其他秘密写入交接文件。若文件会提交到仓库，只放团队允许共享的项目事实；个人偏好和本机路径放在合适的本地配置中。",
          "sources": [
            {
              "label": "OpenAI Developers：重新审视 Codex 的技能、AGENTS.md 与提示词，减少上下文膨胀",
              "url": "https://developers.openai.com/blog/rethinking-skills-and-prompts-for-gpt-6-astra"
            },
            {
              "label": "Anthropic 官方：管理 Claude Code 内存与 CLAUDE.md",
              "url": "https://code.claude.com/docs/zh-CN/memory"
            }
          ],
          "editorialNote": false
        },
        {
          "id": "compact-or-new-chat",
          "heading": "什么时候压缩，什么时候新开对话？",
          "detailsUrl": "https://www.yingxueyuan.cn/docs/agent-project-context/#compact-or-new-chat",
          "paragraphs": [],
          "bullets": [
            "同一目标仍在连续推进：若产品提供压缩/摘要功能，可在阶段边界使用；先保存下一步需要的事实、文件位置、未解决问题和验证证据。",
            "准备切换任务，或旧对话混入大量无关讨论：保存交接摘要后新开对话，要求先核对代码与工作树。",
            "任务很小或信息很少：只提供本次所需背景，不必为了“上下文管理”额外写一堆文档。"
          ],
          "note": "自动压缩是工具功能，项目状态文件是可审计的事实来源，两者互补而不等同。摘要可能遗漏细节，重要决策、数据和测试结果应留在可追溯文件或仓库记录中。",
          "sources": [
            {
              "label": "OpenAI API：长会话 Compaction 机制（仅适用于该 API 文档描述的接口）",
              "url": "https://developers.openai.com/api/docs/guides/compaction"
            },
            {
              "label": "Claude Code 官方命令说明：查看上下文并用 /compact 压缩",
              "url": "https://code.claude.com/docs/zh-CN/commands"
            }
          ],
          "editorialNote": false
        }
      ]
    }
  ]
}
