Glittering's blog Glittering's blog
Home
  • 学习手册

    • 《TypeScript教程》
    • 《Git》
    • 《Vite》
    • 《Vue3》
    • 《React18》
    • 《CSS》
    • 《Tailwind CSS》
  • 技术文档
  • 算法
  • 工作总结
  • 实用技巧
  • collect
About
  • Classification
  • Label
GitHub (opens new window)

Glitz Ma

前端开发工程师
Home
  • 学习手册

    • 《TypeScript教程》
    • 《Git》
    • 《Vite》
    • 《Vue3》
    • 《React18》
    • 《CSS》
    • 《Tailwind CSS》
  • 技术文档
  • 算法
  • 工作总结
  • 实用技巧
  • collect
About
  • Classification
  • Label
GitHub (opens new window)
  • 技术文档

  • 算法

  • 工作总结

    • 时区校正
    • 上传下载文件方式总结
    • web异常监控和分析
    • 前端优化指南
    • http缓存机制
    • 静态资源灰度发布
    • 浏览器原理及渲染机制
    • Chrome DevTools 渲染分析实战
    • Layout Thrashing(布局抖动)
    • Composite Layer(合成层)
    • 全局设置滚动条样式好吗?
    • 虚拟列表如何避免Layout和Paint
    • 前端安全知识
    • 安全(同源策略 / CSP / CORS)
    • 浏览器安全模型
    • 从chrome v8 讲安全
    • WebAssembly(Wasm)
    • XSS → JIT → 沙箱逃逸
    • 微前端总结
    • websocket聊天
    • Uni-app基础知识
    • react16高级特性
    • react16基础知识总结
    • vue2常见原理总结
    • vue2基础知识总结
    • webpack优化实践
    • webpack基础应用知识总结
    • Agent Skills是什么?跟MCP Workflow Command Prompt的关系。
    • 从cnn到transformer全解大模型
    • 什么是 Encoder 和 Decoder 结构
    • GPT 为什么能“看懂”问题
    • GPT 是怎么学会数学的
    • RAG 和 Agent的区别
    • context engineering 技术介绍
      • 一、为什么需要 Context Engineering
      • 二、Context 里通常包含什么
      • 三、Context Engineering 的核心技术
      • 1 Prompt 设计
      • 2 RAG(知识注入)
      • 3 Conversation Memory
        • Sliding Window
        • Summarization Memory
        • Vector Memory
      • 4 Tool Context
      • 5 Token Budget 管理
      • 6 Structured Context
      • 四、Context Engineering 架构
      • 五、举个真实 AI Chat 例子
      • 六、为什么 Context Engineering 很重要
      • 七、Context Engineering vs Prompt Engineering
      • 八、现在 AI 系统基本架构
      • 九、前端工程师需要做什么
        • 1 上下文拼接
        • 2 Token 管理
        • 3 流式输出
        • 4 会话管理
      • 十、什么是 Context Engineering?
    • HarnessEngineering 驾驭工程的概念
    • GIS 基础三件套
    • GIS必会知识点
    • 100 万点地图怎么渲染?
    • GIS空间索引的实现
    • Cesium 从入门到精通:实战指南
    • OpenLayers 从零到精通:2025-2026实战指南
    • Mapbox GL JS 从零到精通:2025-2026实战指南
    • Cesium、OpenLayers 和 Mapbox GL JS 的关系、区别
    • 容器领域必学的黄金组合
    • 小程序笔记
    • 小程序工程模板设计
    • 地图标绘--射线法来计算点在多边形内
  • 实用技巧

  • 收藏夹

  • 技术
  • 工作总结
mamingjuan
2026-03-07
目录

context engineering 技术介绍

Context Engineering(上下文工程) 是近两年在 AI 应用开发里非常重要的概念,可以理解为:

如何给大模型构造“最合适的上下文”,让它输出更好的结果。

简单说:

Prompt Engineering 是写提示词,Context Engineering 是设计整个输入上下文系统。

它的范围 远大于 Prompt。


# 一、为什么需要 Context Engineering

大模型有一个核心限制:

它只能基于当前输入的 Context(上下文)思考。

也就是:

LLM 思考能力 = Prompt + Context
1

如果上下文不好,就会出现:

  • 幻觉(hallucination)
  • 理解错误
  • 长对话遗忘
  • 输出不稳定

所以 AI 系统真正的工程问题是:

如何构造高质量上下文。

这就是 Context Engineering。


# 二、Context 里通常包含什么

一个成熟 AI 系统的 Context 一般包括 6 部分:

┌───────────────────────┐
│ System Prompt         │
│ 用户输入               │
│ 历史对话               │
│ 检索知识(RAG)        │
│ 工具结果               │
│ Agent思考过程          │
└───────────────────────┘
1
2
3
4
5
6
7
8

更完整一点:

Context =
System Prompt
+ Conversation History
+ Retrieved Knowledge
+ Tool Outputs
+ Memory
+ User Input
1
2
3
4
5
6
7

# 三、Context Engineering 的核心技术

通常包含 6 个关键技术模块。


# 1 Prompt 设计

最基础的一层。

例如:

You are a senior software architect.
Answer with concise technical explanations.
1
2

或者结构化 Prompt:

Role:
Task:
Constraints:
Output format:
1
2
3
4

但 Prompt 只是 Context 的 一部分。


# 2 RAG(知识注入)

把外部知识加入 Context。

流程:

用户问题
 ↓
向量检索
 ↓
相关文档
 ↓
拼接进Prompt
1
2
3
4
5
6
7

示例:

Context:
[文档1]
[文档2]

Question:
xxx
1
2
3
4
5
6

这样模型就能回答私有知识。


# 3 Conversation Memory

长对话时需要记住历史。

常见方式:

# Sliding Window

只保留最近 N 轮对话。

Last 10 messages
1

# Summarization Memory

把旧对话总结:

Conversation Summary:
用户正在开发React项目...
1
2

# Vector Memory

历史信息向量化,按需检索。


# 4 Tool Context

Agent 调用工具后,需要把结果加入 Context。

例如:

Tool: weather_api
Result: Beijing 22°C
1
2

再让 LLM 继续推理。


# 5 Token Budget 管理

LLM 有 上下文窗口限制。

例如:

  • 8k
  • 32k
  • 128k
  • 1M tokens

Context Engineering 需要做:

  • 文档切片
  • 去重
  • 摘要
  • relevance ranking

例如:

top_k = 5
max_tokens = 2000
1
2

# 6 Structured Context

让 Context 更结构化。

例如:

JSON 格式:

{
  "user_question": "...",
  "retrieved_docs": [],
  "history": [],
  "tool_results": []
}
1
2
3
4
5
6

结构化可以提高稳定性。


# 四、Context Engineering 架构

一个完整 AI 系统通常是:

                User Input
                     │
                     ▼
             Context Builder
        ┌─────────────────────┐
        │ Prompt Template      │
        │ RAG Retrieval        │
        │ Conversation Memory  │
        │ Tool Results         │
        │ System Rules         │
        └─────────────────────┘
                     │
                     ▼
                    LLM
                     │
                     ▼
                  Response
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

核心组件:

Context Builder


# 五、举个真实 AI Chat 例子

用户问:

如何优化React性能?
1

Context Builder 构造:

System:
You are a senior frontend architect.

History:
用户正在开发大型React系统。

RAG:
[React性能优化文档]

User:
如何优化React性能?
1
2
3
4
5
6
7
8
9
10
11

LLM 才开始回答。


# 六、为什么 Context Engineering 很重要

因为 LLM本身无法控制输出质量。

控制质量的唯一方式就是:

控制输入上下文。

这就是 AI 工程的核心。


# 七、Context Engineering vs Prompt Engineering

对比 Prompt Engineering Context Engineering
范围 写提示词 设计上下文系统
包含 prompt prompt + RAG + memory
是否涉及架构 否 是
是否工程化 低 高

所以很多 AI 工程师说:

Prompt Engineering 已经过时,Context Engineering 才是真正的 AI 工程。


# 八、现在 AI 系统基本架构

几乎所有 AI 产品都是:

Agent
  │
  ▼
Context Engineering
  │
  ▼
LLM
1
2
3
4
5
6
7

Context Engineering 负责:

  • 信息输入
  • 记忆
  • 知识
  • 工具结果

# 九、前端工程师需要做什么

在 AI Chat / Copilot 系统里,前端经常要参与:

# 1 上下文拼接

例如:

messages = [
 systemPrompt,
 history,
 ragDocs,
 userMessage
]
1
2
3
4
5
6

# 2 Token 管理

避免 Context 过大。


# 3 流式输出

SSE / WebSocket。


# 4 会话管理

保存 conversation。


# 十、什么是 Context Engineering?

Context Engineering 是 AI 应用开发中的关键技术,指的是通过设计和管理输入给大模型的上下文信息来提升生成质量。

它不仅包括 Prompt 设计,还包括 RAG 检索、对话记忆管理、工具调用结果、Token Budget 控制等。

在实际 AI 系统中,通常会通过 Context Builder 将用户输入、历史对话、检索知识和工具结果组合成最终的 Context,再发送给大模型进行推理。

上次更新: 2026/03/07, 10:29:53
RAG 和 Agent的区别
HarnessEngineering 驾驭工程的概念

← RAG 和 Agent的区别 HarnessEngineering 驾驭工程的概念→

Copyright © 2015-2026 Glitz Ma
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式