Knowledge for Agents
An index, not a copy
An Agent that has to answer a question needs to know where to look and what is true, quickly. It does not need a second copy of the codebase, and it does not need long, polished prose that restates every function.
Context therefore treats knowledge as an index with explanations:
- each article answers specific reader questions agreed in the requirements;
- each statement points to the exact source region it came from;
- the source itself stays where it is, readable at a pinned version when more detail is needed.
flowchart LR
src["Sources"] --> idx["Indexer"]
idx -->|keeps| kept["Entries, contracts, rules"]
idx -.->|leaves in source| left["Bodies, boilerplate"]
kept --> art["Article with references"]Extract on demand
Indexers do not start with a full symbol scan or a complete relationship graph. During planning they expose a useful skeleton — directories, manifests, registration points, public entries — and propose reader topics. Detailed reading happens while writing each topic, bounded to what that topic needs. Signals such as public exports, registered interfaces, scheduled jobs, storage interfaces or generated clients suggest reader questions, not mandatory pages; one page per symbol is explicitly avoided.
Symbol count is not a quality measure. Internal or out-of-scope items can be left out with a reason; Review checks whether the pages actually answer the reader's questions.
How deep code knowledge goes
Code knowledge is a map, not a copy. It works in three layers:
| Layer | What it holds | When it is written |
|---|---|---|
| Locating | Terms, repositories, modules and where each term lives | Up front |
| Contracts | Entries, routes, IDL, service identifiers and configuration | Up front |
| Implementation | How a specific behavior is actually computed | Not pre-written; read at a pinned commit when a question needs it |
Why not let the Agent re-read the code every time? Without the first two layers it starts blind: it does not know which repository or module to open, and where it lands is partly chance. With them it knows the terms and the repositories and paths they map to, so every search is pulled toward the right module — by a strong model and by a weaker one alike. The measured effect on time and tool calls is in the Knowledge Retrieval Benchmark.
The hard part is granularity. The Code Indexer stops at the skeleton — manifests, registrations, public entries, interfaces and routes — and does not enumerate every symbol or build a relationship graph. That also keeps freshness cheap: an update revisits only the articles affected by a source change, as described in Keeping Knowledge Current.
How to scope code and documents together in a first knowledge base is covered in Cold-Starting a Knowledge Base. When readers mainly need to find things — a knowledge Bot or operations work — ask for a dedicated navigation article that maps terms to checked code entries.
How much smaller
In a large production full-stack monorepo, the two repositories that could be counted locally held about 33,700 code files and 5.84 million lines (about 200 MB); together with about 1,130 document snapshots (about 1.77 MB), they became 220 reviewed articles totalling about 1.75 MB — roughly 1/115 of the source volume.
Two limits apply to that figure:
- only part of the registered sources was counted, so the real ratio is higher;
- the ratio measures size, not coverage or quality. Quality is measured separately in the Knowledge Retrieval benchmark.
The knowledge is not lossless and does not "cover all code". It keeps the facts needed to answer questions, each with a pointer back to the code line or document section. See Evidence Chains and Index First.
写给 Agent 的知识
是索引,不是副本
需要回答问题的 Agent,要能快速知道去哪里找以及什么是事实。它不需要代码库的第二份副本,也不需要把每个函数都复述一遍的长篇精修文字。
因此,Context 把知识当作带有解释的索引:
- 每篇文章回答需求中约定的具体读者问题;
- 每条陈述都指向它所依据的确切来源区域;
- 来源本身保持原位,需要更多细节时可以在固定版本上阅读。
flowchart LR
src["来源"] --> idx["Indexer"]
idx -->|保留| kept["入口、契约、规则"]
idx -.->|留在来源中| left["实现体、样板代码"]
kept --> art["带引用的文章"]按需抽取
Indexer 不会从全量符号扫描或完整关系图开始。在计划阶段,它们先展示有用的骨架——目录、清单文件、注册点、公开入口——并提出读者主题。详细阅读发生在撰写各个主题时,范围以该主题所需为限。公开导出、已注册接口、定时任务、存储接口或生成的客户端等信号,提示的是读者问题,而不是必须产出的页面;明确避免一个符号一页。
符号数量不是质量指标。内部或超出范围的条目可以说明理由后略去;审核检查的是这些页面是否真正回答了读者的问题。
代码知识做到哪一层
代码知识是一张地图,不是副本。它分三层:
| 层 | 包含什么 | 什么时候写 |
|---|---|---|
| 定位层 | 术语、仓库、模块,以及每个术语在哪里 | 预先写 |
| 契约层 | 入口、路由、IDL、服务标识和配置 | 预先写 |
| 实现层 | 某个具体行为实际怎么计算 | 不预先写;问题需要时在固定提交上读取 |
为什么不让 Agent 每次都重新读代码?没有前两层,它两眼一抹黑:不知道该打开哪个仓库、哪个模块,落到哪里多少有些随机。有了这两层,它知道术语以及它们对应的仓库和路径,每次搜索都会被拉向正确的模块,强模型和弱一点的模型都是如此。对耗时和工具调用的实测效果见 知识检索测评。
难点在粒度。Code Indexer 停在骨架这一层(清单文件、注册点、公开入口、接口和路由),不逐个枚举符号,也不构建关系图。这也让保鲜成本很低:更新时只重看受来源变化影响的文章,见 知识的持续更新。
第一个知识库里如何一起划定代码和文档的范围,见 知识库冷启动。如果读者主要是要找到东西(知识 Bot 或运维工作),可以要求单独写一篇导航文章,把术语映射到核实过的代码入口。
能小多少
在一个大型生产级全栈 monorepo 中,可在本地统计的两个仓库共有约 33,700 个代码文件、584 万行代码(约 200 MB);加上约 1,130 份文档快照(约 1.77 MB),最终形成 220 篇已批准的文章,总计约 1.75 MB——大约是来源体量的 1/115。
这个数字有两点限制:
- 只统计了部分已登记的来源,因此实际比例更高;
- 该比例衡量的是体量,而不是覆盖度或质量。质量另行在 知识检索测评 中衡量。
这些知识并非无损,也不“覆盖全部代码”。它保留回答问题所需的事实,每条都带有指回代码行或文档章节的指针。参见 证据链与索引优先。