How Four Coding Agents Assemble Context
A source-referenced analysis compares how Codex, Gemini CLI, OpenCode, and ZCode assemble model context from system prompts, instruction files, environment details, conversation history, tool output, and new messages. It also examines curated history, prompt caching, context compaction, and OpenCode's image budget. The findings are based on four repository snapshots dated November 9, 2026, with specific files and line numbers cited; the source code and line references may since have changed.
The report characterizes agent memory as the product of context-assembly code. It argues that differences in prompt placement, memory organization, history filtering, caching, and compaction shape each coding agent's behavioral characteristics.
这是「失忆顾问」系列的第五篇。前四篇我们讲过:LLM 是一位每次见面都失忆的天才顾问,token 是按页收费的咨询费,工具调用是递纸条,压缩是请秘书写交接备忘录,prompt cache 是照片记忆一折。 但有一个最基础的问题,我们一直没有正面回答—— 每次会面前,助理(Agent)到底往顾问手里塞的那个"资料袋"里,装了哪些东西? 这只资料袋,就是你天天听到的 Context(上下文) 。今天我们把 Codex、Gemini CLI、OpenCode、ZCode 四家的一线源码全部翻开,把这只袋子倒过来抖一抖,看看里面到底有什么、四家塞法差多远、袋子满了怎么办。 全文所有结论都标注了
文件:行号,欢迎逐条对质。放心,没有一行是我编的。
一、先说一个反直觉的事实:顾问从来没有"记忆"
很多新手以为 Agent 有记忆,聊过的事它都"记得"。
错。大模型的 API 是完全无状态的。 你每发一次请求,都像是第一次见这位顾问——他昨天、上一秒聊过什么,一个字都不记得。