Welcome to Context Engineering / 欢迎来到上下文工程的世界
第 1 章 / Chapter 1
Welcome to Context Engineering
欢迎来到上下文工程的世界
---
Imagine you are working on a history essay with an AI assistant. You paste in three long PDF articles about World War II, type a careful question, and the model writes back a perfect summary. You ask a follow-up about the causes of the war. It answers. You ask another follow-up about the Treaty of Versailles. It answers again. Then you ask: *"In the second article I pasted, what was the date mentioned in chapter 3?"*
The model hesitates. It says: *"I'm not sure which article you're referring to. Could you paste it again?"*
You stare at the screen. The article is right there in the conversation. Why doesn't it remember?
> 想象一下:你正在和一位 AI 助手一起完成历史论文。你贴入了三篇关于第二次世界大战的长 PDF 文章,输入了一个认真准备的问题,模型给出了一段完美的总结。你追问战争爆发的原因,它答了上来;你再追问《凡尔赛条约》,它也答了上来。然后你问:"在我贴入的第二篇文章中,第 3 章提到的日期是什么?" > > 模型停顿了一下,回复:"我不确定你指的是哪篇文章。你能再贴一次吗?" > > 你盯着屏幕——那篇文章明明就在对话里。它为什么不记得?
This is not a bug. It is not a personality flaw. It is not because the model is "stupid." It is the consequence of a single, very boring-sounding fact: every AI has a workspace, and that workspace is finite. When the workspace fills up, the oldest files fall off the desk.
> 这不是 bug,不是性格缺陷,也不是因为模型"笨"。它是一个听起来有点无聊的事实带来的必然结果:每一种 AI 都有一个工作区,而工作区是有限的。 当桌面被堆满,最早放上去的文件就会掉到地上。
This book is about that workspace. We will call it the context window, and we will call the discipline of designing for it context engineering. By the end of this first chapter, you will be able to look at any AI tool you use — ChatGPT, Claude, Gemini, Copilot, a customer-service bot, the voice assistant in your phone — and map exactly what the model can and cannot see, why, and how engineers manipulate that visibility to make the model smarter without making the model bigger.
> 本书要讨论的,就是这个"工作区"。我们将它命名为上下文窗口(context window),把围绕它进行设计的学科叫做上下文工程(context engineering)。读完本章,你将能用一种新的眼光去看待你日常使用的任何 AI 工具——ChatGPT、Claude、Gemini、Copilot、客服机器人、你手机里的语音助手——并精确地画出模型"能看到什么、看不到什么、为什么、工程师是如何操纵这种可见性"的地图。
---
1.1 Three Words That Get Mixed Up: Context, Knowledge, Memory
1.1 三个总被混用的词:上下文、知识、记忆
Before we can engineer anything, we have to name the parts correctly. Three words appear in nearly every AI conversation, and they are constantly confused: context, knowledge, and memory. They sound similar, but they live in different places inside the system, and confusing them is the single most common reason people misunderstand why AI does what it does.
> 在开始工程化之前,我们必须先把零件的名字叫对。"上下文(context)"、"知识(knowledge)"、"记忆(memory)"这三个词几乎出现在每一段关于 AI 的对话里,却总被混为一谈。它们的发音相近,却生活在系统内部的不同位置。把它们搞混,是人们误解 AI 行为方式的最常见原因。
Knowledge: the frozen library baked into the model
知识:被"烤"进模型里的冻结图书馆
When a model is trained, it reads a giant pile of text — books, websites, code, articles. From that pile, it learns statistical patterns. The result is something that feels like a library, but is not actually a library. It is a *distillation* of a library.
> 当一个模型被训练时,它会阅读海量的文本——书籍、网站、代码、文章。它从这些文本中学到统计模式,最终得到的东西感觉像一座图书馆,但并不是真正的图书馆。它是图书馆的*蒸馏物*。
There is no central catalog inside the model. There are no folders labeled "World War II." There is just a set of numerical weights — billions of them — that encode patterns like "after the word *Treaty* often comes the word *Versailles*." If you ask the model a question whose answer was common in its training data, it can usually answer. If the answer was extremely rare, or appeared after its training cutoff date, it cannot.
> 模型内部并不存在一个中心化的"目录",也没有标着"二战"的文件夹。它只有一组参数(数十亿个),编码着像"在 *Treaty* 这个词之后经常会出现 *Versailles*"这样的统计模式。如果你的问题在训练数据中很常见,模型通常能回答;如果答案极其罕见,或出现在训练截止日期之后,模型就回答不了。
Knowledge lives inside the weights. It is *static* — it does not change between two conversations you have five minutes apart. It is also *shared* — every user of a given model version draws on the same baked-in knowledge.
> 知识住在参数里。它是*静态的*——你相隔五分钟进行的两场对话,使用的是同一份知识。它也是*共享的*——同一个模型版本的所有用户,使用的是同一份被烤进去的知识。
If knowledge were a person, it would be a librarian who fell asleep in 2024. They know a lot, but they have no idea what you said ten seconds ago, and they cannot learn from your conversation.
> 把知识比作一个人,它会是一个在 2024 年睡着了的图书管理员。他知道很多,但他完全不知道你十秒钟前说了什么,也无法从你的对话中学习。
Context: the desk in front of the model
上下文:摆在模型面前的桌子
Context is everything that is *currently on the desk* when the model is generating its next answer. It includes:
> 上下文是指模型在生成下一个回答时,*桌面上摆着的一切*。它包括:
- The **system prompt** — the hidden instructions set by the developer ("You are a helpful assistant. Reply in three short paragraphs.")
- **系统提示(system prompt)**——由开发者设置的隐藏指令(例如"你是一个乐于助人的助手。请用三段简短的话回复。")
- The **user's input** — your question, the file you pasted, the image you uploaded.
- **用户输入**——你的问题、你粘贴的文件、你上传的图片。
- The **conversation history** — everything said earlier in this chat.
- **对话历史**——本次聊天中此前说过的所有内容。
- Optionally, **retrieved documents** — extra text the system pulled in from a database or the web right before asking the model to answer.
- **检索到的文档(可选)**——系统在请求模型回答前,从数据库或网络中临时拉进来的额外文本。
Everything in this list is context. None of it changes the model's baked-in knowledge. It is *transient* — it exists for the duration of one conversation. It is *private* — your context is not seen by other users.
> 上面列表中的所有内容都是上下文。它们不会改变模型固有的知识。上下文是*临时的*——它只存在于一次对话的期间;上下文也是*私有的*——你的上下文不会被其他用户看到。
If context is the desk, then the *size* of the desk is called the context window, measured in tokens (small chunks of text, roughly 0.5–1 word each in English, roughly 1–2 characters each in Chinese).
> 如果把上下文比作桌子,那么这张桌子的*大小*就被称为上下文窗口,用 token(词元)来衡量——一个 token 是文本的一小块,在英文中大约相当于 0.5–1 个单词,在中文中大约相当于 1–2 个汉字。
A modern frontier model might have a context window of 200,000 tokens. That sounds like a lot — and it is, compared to 4,000-token models from a few years ago — but it is still finite. A book is roughly 80,000–100,000 tokens. Three books pasted in one chat will not fit.
> 一个现代的顶级模型可能有 20 万个 token 的上下文窗口。听起来很多——相对于几年前仅 4,000 token 的模型确实如此——但它仍然是有限的。一本书大约占 8 万到 10 万 token。把三本书粘到一次聊天里,是放不下的。
Memory: the long-term drawer next to the desk
记忆:桌子旁边那个长期抽屉
Memory is the newest of the three concepts, and the most loosely defined. When a product says "the AI now has memory," it usually means the system saves some facts about you to a separate database and *re-injects them into the context window at the start of future conversations*.
> 记忆是这三个概念中最新的,也是定义最松散的。当一款产品宣称"AI 现在有记忆了",通常意味着系统会把关于你的某些事实保存到一个独立的数据库里,然后在未来的对话开始时,*把它们重新注入到上下文窗口*。
Memory is not stored in the model's weights (that would be training). Memory is not always present in the context window (it is loaded only when the system chooses). Memory is a side-channel — a persistent scratchpad that survives between sessions.
> 记忆不会存储在模型的参数里(那是训练)。记忆也不会一直待在上下文窗口里(它只在系统选择时被加载)。记忆是一条侧通道(side-channel)——一个可以跨越会话存活的长期便笺。
If context is the desk, and the model is the librarian, then memory is the librarian's notebook. They glance at it at the start of a new conversation. If the notebook gets too long, they can read only part of it.
> 如果把上下文比作桌子,模型比作图书管理员,那么记忆就是图书管理员的笔记本。每开始一场新对话,图书管理员会瞥一眼笔记本。如果笔记本太长,他可能只能读一部分。
The three at a glance
三者一览
| Concept / 概念 | Where it lives / 存在位置 | Lifespan / 生命周期 | Shared? / 共享? | Analogy / 类比 | |---|---|---|---|---| | Knowledge / 知识 | Model weights / 模型参数 | Permanent (until retrained) / 永久(直到重新训练) | Yes, across all users / 跨用户共享 | A frozen library / 冻结的图书馆 | | Context / 上下文 | Context window / 上下文窗口 | One conversation / 一次对话 | No, per-user / 不共享 | The desk in front of the model / 模型面前的桌子 | | Memory / 记忆 | External database / 外部数据库 | Persistent across sessions / 跨会话持久 | No, per-user / 不共享 | The librarian's notebook / 图书管理员的笔记本 |
> 表格说明:知识活在参数里,永久且共享;上下文活在窗口里,临时的、私有的;记忆活在外部数据库里,跨会话的、私有的。
This three-way distinction is the foundation of everything that follows. When someone says "the model doesn't know that," ask: *does it not know because of knowledge, context