R443 · Agent architecture › Memory and context

What an instruction file costs, and the one number OpenCode gives no way to read

Read the original

coding-agentsinstruction-filescontext-engineeringdocumentation-gapsopencode

A widely repeated claim about agent instruction files is that their full text goes out with every single model request, so every unused line is a tax paid per turn. It is the premise behind every "trim your AGENTS.md" guide. OpenCode's own documentation is unusually precise about how instruction files load — and says nothing at all about the thing the cost argument depends on.

What the documentation does specify

OpenCode reads AGENTS.md, with CLAUDE.md as a per-directory fallback. Discovery is stated exactly: the global file at ~/.config/opencode/AGENTS.md loads first, then every AGENTS.md from the current workspace directory walking up toward the home directory. For a workspace inside a project, the walk stops at the project root; for a workspace outside the project root, only the global file loads. The docs give a worked order — global, then packages/web/AGENTS.md, then packages/AGENTS.md, then the root AGENTS.md — and say these are "combined in this order".

Without a global AGENTS.md, it reads ~/.claude/CLAUDE.md. Three environment flags opt out: one skips project discovery while keeping the global file, one skips every CLAUDE.md, and one additionally skips .claude skills. The server reads those at startup, so changing them means restarting it.

The assembly order is documented too. The agent or provider system prompt goes first; instruction files are the fourth item in the initial assembly, ahead of built-in date and environment context.

The instructions config array takes relative paths and globs (searched from the workspace up to the project root), ~/ paths, absolute paths whose filename may itself be a glob, and http(s) URLs fetched when config loads with a five-second timeout. Entries load after the discovered files, in listed order, and entries from every config document are combined. The docs are blunt that this is addition, not resolution: the sources "are combined rather than used as overrides", and the tool "combines the files and does not resolve conflicts between them". Missing files are skipped; a URL that fails is skipped or keeps its last fetched content.

The asymmetry that will catch people out

Editing a global or upward-discovered instruction file takes effect inside a running session: before the next model request the tool detects the change and adds an instruction update ahead of pending input.

Edits to a nested file do not. Once a nested file has loaded, later changes to it are not detected, and the docs say plainly to start a new session when updated text must apply immediately.

This is backwards from the intuition most people carry. The file furthest from your working directory is the live one; the file sitting right next to the code you are editing is the frozen one. Nested files are discovered lazily as the agent explores — reading a file or listing a directory loads any instruction files between that target and the workspace — and they are "added to session history in discovery order", loaded nearest-first and deduplicated while their entry remains in model-visible history. If compaction or a revert removes that entry, a later read can load the file again.

That last detail is the one to sit with. A nested instruction file is a conversation message, not a standing preamble. It can be compacted away. It can then come back. Its contents can therefore be present, absent, or present twice over the life of one session, depending on what the agent happened to read and whether the context was compacted in between.

What is not documented, and why it matters

Four gaps, each load-bearing:

Per-request versus per-session. The docs describe initial instruction assembly and describe nested files entering session history. They never state whether the assembled instruction text is re-transmitted with each model request. Both readings are consistent with the text.

Caching. Not mentioned anywhere. No cache control, no cached-token accounting, no breakpoints. This is the gap that decides the argument: if the instruction block sits inside a cached prefix, re-sending it each turn costs a fraction of its nominal token count, and the "you pay for all sixty lines every request" framing is wrong about the money even where it is right about the bytes.

Budgets. No size limit, no token budget, no truncation rule, no warning about long files. The only documented limit anywhere is that five-second URL fetch timeout.

Inspection. No debug flag, no slash command, no log, no token counter. The docs state that clients can see which instruction sources changed, but not their contents.

So the cost model is unverifiable from the tool. You cannot read what was sent, you cannot count it, and the documentation does not tell you.

Why this pattern keeps recurring

The shape here is familiar: a documented surface that answers the mechanical question thoroughly and the economic question not at all. Discovery order, fallback filenames, merge semantics, staleness rules — all specified. The one quantity a user would optimise against — what this costs per turn — is absent, and no affordance exists to measure it.

That asymmetry is not an oversight so much as a consequence of what is cheap to document. Load order is a property of the code and stable across providers. Token cost depends on the model, the provider's caching behaviour, the prompt layout and the session's history, and a vendor who documents a number there owns it. The predictable result is that the community fills the gap with reasoning, and reasoning about an unmeasured quantity converges on whatever sounds most prudent rather than whatever is true.

Tradeoffs in the design itself

Combining without conflict resolution is the honest choice and the expensive one. It means a nested file cannot contradict a parent file in any defined way; both texts reach the model and the model decides. The alternative — a precedence order — would be checkable but would also invite people to encode policy in file placement, which is brittle in a different way.

Lazy nested discovery keeps the initial prompt small, at the price of non-determinism: two sessions doing the same task can carry different instruction sets depending on which directories the agent happened to touch.

Mid-session updates for upward files only is defensible — the upward chain is bounded and known at session start, the nested set is not — but it means the staleness rule differs by file position, which is exactly the kind of rule people do not retain.

Failure modes worth guarding against

A review checklist

  1. Does any rule you rely on live in a nested instruction file? If so, is it a rule you might edit while a session is open? Move it up.

  2. Do any two instruction files in the chain give conflicting guidance? Assume neither wins and write the rule once.

  3. Are instruction URLs used? What does the session do if the fetch fails — and do you know what content it would fall back to?

  4. Are glob-based entries used? Can a new file silently join the instruction set?

  5. Before claiming a token cost for your instruction file, can you actually observe what was sent? If not, treat the cost as unknown rather than as the worst case.

  6. Does your trimming guidance assume no caching? Say so explicitly, because it changes the answer by an order of magnitude.

Connections

See R443 on the knowledge map

Part of the Research notes on nowucca.com. Figures and claims are the source's own unless the summary says otherwise.