Description
When a project has a large AGENTS.md (or CLAUDE.md/CONTEXT.md) file, OpenCode injects the entire file contents into the system prompt on every loop() iteration with no size guard. This makes OpenCode unusable for projects with large instruction files.
Example: A 331KB AGENTS.md (~83K tokens) consumes 81% of a 128K context window. The first turn starts at ~101K input tokens before the agent does anything, immediately triggering compaction. The session enters a compaction loop and never makes progress.
The root cause is in InstructionPrompt.system() — it reads files via Filesystem.readText() and injects the full content with zero truncation. Meanwhile, the read tool already has a 50KB cap (MAX_BYTES), so the agent can safely read large files on demand.
Related but different from #16960 (which is about losing instructions after compaction — this is about instructions causing compaction in the first place).
Steps to reproduce
- Create an
AGENTS.md larger than ~100KB in a project
- Open the project with OpenCode using a model with 128K context
- Send any message — observe compaction triggers on the first turn
- The session never stabilizes because the instruction file is re-injected fresh on each iteration
Proposed fix
Add a configurable projectInstructionMaxSize field to opencode.json. When an instruction file exceeds this threshold, skip inline injection and insert a short note telling the agent to read the file on demand via the read tool (which already caps at 50KB per call).
I have a working implementation on my fork: balaguru-surendar-6638/opencode@feat/instruction-loading-optimization. The change is 2 files, +48/-5 lines. Happy to open a PR if a maintainer approves the approach.
Operating System
Ubuntu (WSL2)
OpenCode version
1.2.27
Description
When a project has a large
AGENTS.md(orCLAUDE.md/CONTEXT.md) file, OpenCode injects the entire file contents into the system prompt on everyloop()iteration with no size guard. This makes OpenCode unusable for projects with large instruction files.Example: A 331KB
AGENTS.md(~83K tokens) consumes 81% of a 128K context window. The first turn starts at ~101K input tokens before the agent does anything, immediately triggering compaction. The session enters a compaction loop and never makes progress.The root cause is in
InstructionPrompt.system()— it reads files viaFilesystem.readText()and injects the full content with zero truncation. Meanwhile, thereadtool already has a 50KB cap (MAX_BYTES), so the agent can safely read large files on demand.Related but different from #16960 (which is about losing instructions after compaction — this is about instructions causing compaction in the first place).
Steps to reproduce
AGENTS.mdlarger than ~100KB in a projectProposed fix
Add a configurable
projectInstructionMaxSizefield toopencode.json. When an instruction file exceeds this threshold, skip inline injection and insert a short note telling the agent to read the file on demand via thereadtool (which already caps at 50KB per call).I have a working implementation on my fork:
balaguru-surendar-6638/opencode@feat/instruction-loading-optimization. The change is 2 files, +48/-5 lines. Happy to open a PR if a maintainer approves the approach.Operating System
Ubuntu (WSL2)
OpenCode version
1.2.27