Skip to content

Instantly share code, notes, and snippets.

@SimonAKing
Last active July 15, 2025 15:51
Show Gist options
  • Save SimonAKing/ddd18c12868298bb4a48d0e07ceffc7b to your computer and use it in GitHub Desktop.
Save SimonAKing/ddd18c12868298bb4a48d0e07ceffc7b to your computer and use it in GitHub Desktop.
Context engineering 解读 - 07/08/2025

分享一个最近比较火的概念 “context engineering”(由 Andrej Karpathy 提出),一句话解释:在模型推理时 提供出 任务所需的所有完整信息的工程。

以下内容参考自 Awesome-Context-Engineering

为什么近期 context engineering 被广泛讨论?原因是 prompt engineering 不足以代表了现在生产级 LLMs 应用的复杂度。 首先 LLMs 的概率函数是: Image

prompt engineering 约等于将 prompt 视为了 context,而 LLMs 应用中 context 等于以下多个组件的组合: context = Assemble(instructions, knowledge, tools, memory, state, query)

  • |context| ≤ MaxTokens(context window limitation)
  • knowledge=Retrieve(query,database)
  • memory = Select(history, query)
  • state = Extract(world),比如时间信息

Context engineering 公式定义: Image

所以,Context engineering 是设计和优化这些组件和格式,以最大限度地提高模型在给定任务上效果的工程设计。

有哪些手段可以保证 context engineering 的效果呢?一些经验:

  1. 将任务拆解到语义明确的最小粒度

    比如 rewrite query 场景,让模型判断 query 是否是 memory 的连续对话,是则改写;否则不改写,一次推理做两件事,提高难度。 可以重写与判断关联并行跑,判断关联 返回 1 个 token 即可,更早结束,如果 返回了不关联 那就忽视重写

  2. 符合模型的训练数据分布、能力特性

    比如 coding 场景要优先使用 claude、openai 模型,因为内置 tool,并且也有对应的产品,说明有做了专门训练

  3. 优化模型注意力

    Context 不要插入过多无关的 token,比如 web_search 场景,给到的都是整理好的答案,而不是 某搜索引擎的结果

  4. 适当增加中间层,确定性的逻辑使用 workflow
  5. 减少一步生成 critical token 的情况,适当前置 cot
  6. auto prompt 低成本优化 instruction
  7. 遇到问题,就问问模型,看看 ta 的偏好 为什么不符

Labels: 🎯技术向

Original post: https://simonaking.com/blog/weibo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment