AI Skills AI技能 8d ago Updated 8d ago 更新于 8天前 46

What Is Context Engineering? Everything you need to know 什么是上下文工程?你需要知道的一切

Context engineering is defined as the discipline of curating the specific information (rules, files, history) available to an AI model before generation, distinguishing it from prompt engineering which focuses on the query itself. The concept gained prominence in mid-2025 through definitions by Andrej Karpathy and Tobi Lütke, positioning it as the "steering" mechanism that complements "vibe coding." "Context rot" is identified as a critical failure mode where adding excessive information to the 上下文工程(Context Engineering)被定义为在模型生成输出前,决定其“知道什么”的学科,是比提示词工程更高杠杆、更持久的优化层。 核心挑战在于“上下文腐烂”(Context Rot),即随着上下文窗口填充无关信息,模型的召回率和表现反而下降,因此“少而精”优于“多而全”。 与仅优化提问方式的提示词工程不同,上下文工程通过系统指令、项目规则、检索数据等构建一个稳定的系统环境,确保AI遵循团队规范。 该概念由Andrej Karpathy、Tobi Lütke等人于2025年6月正式提出并推广,旨在解决AI辅助开发中代码风格不一致和违反架构约束的问题。

65
Hot 热度
70
Quality 质量
60
Impact 影响力

Analysis 深度分析

TL;DR

  • Context engineering is defined as the discipline of curating the specific information (rules, files, history) available to an AI model before generation, distinguishing it from prompt engineering which focuses on the query itself.
  • The concept gained prominence in mid-2025 through definitions by Andrej Karpathy and Tobi Lütke, positioning it as the "steering" mechanism that complements "vibe coding."
  • "Context rot" is identified as a critical failure mode where adding excessive information to the context window degrades model recall and performance, making less context often superior to more.
  • Effective context engineering involves rigorous curation—such as using concise AGENTS.md files—to ensure models adhere to architectural conventions and avoid reinventing deprecated patterns.
  • Poorly engineered context contributes significantly to security risks, with studies indicating that lack of proper contextual guidance leads to AI-generated code failing security tests at high rates.

Why It Matters

This shift represents a fundamental change in how AI developers approach software engineering, moving focus from crafting clever prompts to building robust, reusable knowledge bases for AI agents. For practitioners, understanding context engineering is essential to reducing hallucinations and ensuring generated code aligns with existing codebases and security standards. It highlights that the quality of AI output is constrained not just by model capability, but by the precision of the information provided to it.

Technical Details

  • Definition and Scope: Context engineering encompasses system instructions, project rules, relevant file snippets, retrieved data, conversation history, and tool results. It is the assembly of the finite context window prior to token generation.
  • Context Rot: A phenomenon documented by Anthropic where model recall degrades as the context window fills with irrelevant tokens. This necessitates a "lean window" strategy over a "paste everything" approach.
  • Distinction from Prompt Engineering: While prompt engineering optimizes the specific question (e.g., "explain reasoning step by step"), context engineering optimizes the static environment (e.g., stack specifics, architecture rules like "business logic in service layer") that applies to all prompts within a session.
  • Implementation Example: Replacing a full repository dump with a concise 30-line AGENTS.md file containing stack details, three architecture rules, and anti-patterns resulted in immediate adherence to conventions, whereas verbose context led to errors.
  • Security Implications: Veracode’s 2025 report links poor context engineering to security failures, noting that 45% of AI-generated code failed security tests, often due to the model lacking knowledge of specific validation helpers or auth boundaries.

Industry Insight

  • Shift in Developer Workflow: Teams should invest in creating and maintaining structured context assets (like standardized AGENTS.md or system prompts) rather than relying solely on ad-hoc prompting. This creates a durable, reusable layer for AI interaction.
  • Performance Optimization via Subtraction: Engineers must actively prune context windows. Adding more data does not linearly improve performance; instead, selective retrieval of only task-relevant information is required to mitigate context rot and maintain high recall accuracy.
  • Security as a Context Problem: Security reviews of AI-generated code should include audits of the context provided to the model. Ensuring the model is aware of security constraints and existing helper libraries is as critical as the code generation process itself.

TL;DR

  • 上下文工程(Context Engineering)被定义为在模型生成输出前,决定其“知道什么”的学科,是比提示词工程更高杠杆、更持久的优化层。
  • 核心挑战在于“上下文腐烂”(Context Rot),即随着上下文窗口填充无关信息,模型的召回率和表现反而下降,因此“少而精”优于“多而全”。
  • 与仅优化提问方式的提示词工程不同,上下文工程通过系统指令、项目规则、检索数据等构建一个稳定的系统环境,确保AI遵循团队规范。
  • 该概念由Andrej Karpathy、Tobi Lütke等人于2025年6月正式提出并推广,旨在解决AI辅助开发中代码风格不一致和违反架构约束的问题。

为什么值得看

对于AI从业者和开发者而言,理解上下文工程是从“Vibe Coding”的随意性走向生产级稳定性的关键一步,它揭示了提升AI代码质量的核心不在于修改提示词,而在于构建高质量的上下文环境。文章提供的洞察有助于团队建立更规范的AI工作流,减少因上下文缺失或过载导致的代码安全漏洞和架构违规。

技术解析

  • 定义与起源:上下文工程指组装AI模型在生成输出前所看到的所有信息,包括系统指令、项目规则、相关文件、检索数据和对话历史。该术语在2025年6月由Andrej Karpathy描述为“用恰到好处的信息填充上下文窗口的艺术”,并由Tobi Lütke和Simon Willison进一步阐释。
  • 与提示词工程的对比:提示词工程优化的是“问什么”(单次交互的措辞),而上下文工程优化的是“模型已知什么”(系统级的环境配置)。前者如句子,后者如系统;良好的上下文能让中等模型表现得像专家,而糟糕的上下文会让顶级模型产生幻觉。
  • 上下文腐烂(Context Rot):引用Anthropic 2025年9月的指南,指出随着上下文窗口填满无关token,模型的回忆能力会退化。盲目粘贴整个代码库会导致性能低于精简的上下文窗口,关键在于筛选而非堆砌。
  • 实践案例:作者通过将SaaS项目的完整代码库替换为30行的AGENTS.md文件(包含技术栈、三条架构规则和禁止事项),显著提高了AI编码代理的一次性成功率,证明了结构化约束比原始数据更有效。

行业启示

  • 从提示词优化转向上下文治理:企业和团队应将资源从单纯的Prompt调优转移到构建和维护高质量的上下文基础设施上,如标准化的AGENTS.md、自动化的上下文检索策略和严格的架构规则注入。
  • 重视AI代码的安全性与合规性:鉴于Veracode报告指出45%的AI生成代码存在安全漏洞,且部分源于上下文缺失(如未加载验证助手或认证边界),建立包含安全最佳实践的上下文环境是降低风险的关键战略。
  • 标准化AI辅助开发流程:上下文工程使得AI行为更具可预测性和一致性,建议将上下文配置视为代码的一部分进行版本控制和管理,以确保所有团队成员和Agent使用相同的规范基准。

Disclaimer: The above content is generated by AI and is for reference only. 免责声明:以上内容由 AI 生成,仅供参考。

LLM 大模型 Agent Agent Code Generation 代码生成 Programming 编程