Skip to content

Large AGENTS.md files consume entire context window, triggering immediate compaction #18037

@balaguru-surendar-6638

Description

@balaguru-surendar-6638

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

  1. Create an AGENTS.md larger than ~100KB in a project
  2. Open the project with OpenCode using a model with 128K context
  3. Send any message — observe compaction triggers on the first turn
  4. 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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions