> For the complete documentation index, see [llms.txt](https://zhouhao4221.gitbook.io/haiqing-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://zhouhao4221.gitbook.io/haiqing-docs/01-thinking/context-as-constraint.md).

# 上下文即约束

## 关于"AI 有没有记忆"

这个问题的答案取决于你在问哪一层。

**基础模型层：没有记忆。** 模型本身在每次推理时只能看到当前上下文窗口里的内容。窗口之外的一切，对它来说不存在。这是模型的架构特性，不会因为产品功能而改变。

**产品层：可以有记忆。** 很多 AI 产品在模型之上构建了记忆系统——ChatGPT 的 Memory 功能、Claude Code 的 `/memory/` 目录、各类 RAG 系统。这些系统的本质是：在新会话开始时，把历史信息注入上下文窗口，让模型"看到"它。

关键洞察：**记忆系统不改变模型的工作方式，它改变的是进入上下文窗口的内容。**

但产品层记忆有两个结构性限制：

* **范围限制**：窗口有限，旧记忆会被挤出，模型"忘记"的方式只是不再注入
* **共享限制**：记忆通常绑定个人账号，团队成员各自维护各自的记忆孤岛，无法复用

这解释了为什么团队协作场景中，把上下文写进仓库文件（CLAUDE.md、规范文档）比依赖产品记忆更可靠——文件是共享的、版本化的、对所有人一致的。

所以无论有没有记忆层，核心机制不变：**AI 的行为边界，由进入上下文窗口的内容决定。**

***

## 上下文的三类内容

| 类型       | 作用               | 缺失时的后果             |
| -------- | ---------------- | ------------------ |
| **任务定义** | 告诉 AI 要做什么       | AI 猜测意图，生成偏离目标的输出  |
| **约束条件** | 告诉 AI 不能做什么      | AI 填补空白，做出你没有预期的选择 |
| **领域知识** | 告诉 AI 这个项目/系统的规则 | AI 使用通用假设，忽略项目特有规则 |

三类内容缺一，AI 都会做出某种假设来填补。问题不在于 AI"猜错了"，而在于你不知道它在猜。

***

## 为什么上下文是约束，而不只是信息

传统上我们把上下文理解为"给 AI 看的背景资料"。这个理解是不完整的。

更准确的理解：**上下文划定了 AI 能做出合理决策的边界。**

* 上下文足够 → AI 在边界内工作，输出可预期
* 上下文不足 → AI 超出边界工作，输出取决于模型的训练偏差

这是为什么同一个提示在有无 CLAUDE.md 的仓库里会产生不同质量的输出——不是模型变了，是约束边界变了。

***

## 上下文失效的三种模式

**1. 上下文缺失** 没有提供关键约束，AI 使用默认假设。最常见，也最容易预防。

**2. 上下文过载** 信息太多，关键约束被淹没。模型倾向于关注最近的内容，早期提供的约束可能被忽略。

可操作的识别信号：

* AI 的输出不再引用项目特有的术语或约定，变得"适用于任何项目"
* AI 忽略了你在早期明确设定的约束，且没有说明原因
* AI 的回答开始与它几轮前的答案矛盾
* 你需要反复提醒同一条约束才能让它生效

出现以上任意一个信号，应当主动重申关键约束，或开启新会话并重新注入。

**3. 上下文漂移** 长会话中，早期建立的约束在多轮对话后悄悄失效。AI 的输出看起来合理，但不再遵守最初的边界。

***

## 实践含义

**对提示的影响：** 约束条件和任务描述同等重要。不要只描述"要做什么"，要明确"不能做什么"。

**对工具配置的影响：** CLAUDE.md、系统提示、规范文档不是可选的装饰，它们是约束层。上下文管理的操作细节见 `07-tooling/ai-context-in-project.md`。

**对长会话的影响：** 超过一定轮次后，需要主动确认关键约束仍然有效，或重新注入。不要假设 AI 记住了你在五轮之前说的事。

***

## 判断标准

一个上下文是否足够，不取决于字数，而取决于：

> AI 是否有足够的信息，在你不干预的情况下做出你认可的决策？

如果答案是"不确定"，上下文就还不够。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://zhouhao4221.gitbook.io/haiqing-docs/01-thinking/context-as-constraint.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
