# OpenCode File Structure & Reference Guide ## Directory Structure ### Global Config (`~/.config/opencode/`) ``` ~/.config/opencode/ ├── opencode.json # Global config ├── AGENTS.md # Auto-loaded instructions ├── CLAUDE.md # Auto-loaded instructions ├── agent/ # Custom agents │ ├── my-agent.md │ └── category/ │ └── nested-agent.md ├── command/ # Custom commands │ └── my-command.md └── plugin/ # Custom plugins └── my-plugin.ts ``` ### Local Repo (`.opencode/`) ``` your-repo/ ├── opencode.json # Repo config ├── .opencode/ │ ├── AGENTS.md # Auto-loaded instructions │ ├── agent/ # Project-specific agents │ │ └── my-agent.md │ ├── command/ # Project-specific commands │ │ └── my-command.md │ └── plugin/ # Project-specific plugins │ └── my-plugin.ts └── src/ ``` ## Auto-Loaded Files **Instruction files** (loaded automatically as system prompts): - `AGENTS.md` - Custom instructions (global or local) - `CLAUDE.md` - Legacy Claude instructions - `CONTEXT.md` - Deprecated, but still works **Config files** (merged in order): 1. `~/.config/opencode/opencode.json` (global) 2. `opencode.json` files from repo root up to current directory 3. Files from `.opencode/` folders in hierarchy ## Supported Subfolders | Folder | Files | Purpose | |--------|-------|---------| | `agent/` | `*.md` | Custom agents with system prompts | | `command/` | `*.md` | Custom slash commands | | `plugin/` | `*.ts`, `*.js` | Custom tools and extensions | **⚠️ Use singular names only:** `agent/`, NOT `agents/` ## File References with `@` Symbol ### Immediate vs. Lazy Loading **With `@` symbol** - **Immediate Loading**: - File is fetched and loaded right away - Content is immediately available to the agent - Use when context is always needed **Without `@` symbol** - **Lazy Loading**: - File is only fetched when the agent determines it's needed - Saves tokens if the agent can complete the task without it - Agent discovers and loads the file on-demand ### Using the `@` Symbol **In commands and templates:** ```bash # Relative to repo root @README.md @src/main.ts # Home directory @~/my-file.txt # Absolute path @/absolute/path/file.txt # If file doesn't exist, looks for agent @my-agent ``` **Resolution order:** 1. Check if starts with `~/` → resolve to home directory 2. Check if absolute path → use as-is 3. Otherwise → resolve relative to repo root (`Instance.worktree`) 4. If not found → look for agent with that name ### Best Practices for Lazy Loading (Without `@`) When referencing files without the `@` symbol, help the agent discover them by: **1. Provide clear file paths** ``` "Use the context from .opencode/command/commit.md if needed" ``` **2. Describe what's there** (so agent knows when to fetch) ``` "Check the commit command guidelines in .opencode/command/commit.md when you need to understand our commit message format" ``` **3. Mention directory conventions** ``` "Command templates are in .opencode/command/ - reference them as needed" ``` **4. Reference by purpose, not just path** ``` "Follow our commit guidelines (available in the command directory) when making commits" ``` ### Example: Lazy Loading in Practice **Without `@` symbol (lazy loading):** ``` "Create a git commit. Our commit guidelines are in .opencode/command/commit.md - check them if you need to understand our prefix conventions and message format." ``` This approach: - ✅ Agent knows the file exists and where it is - ✅ Agent knows WHEN it would be useful (for commit conventions) - ✅ Agent only reads it IF it needs that information - ✅ Saves tokens if the agent can complete the task without it **With `@` symbol (immediate loading):** ``` "Create a git commit following these guidelines: @.opencode/command/commit.md" ``` This approach: - ✅ File content is immediately available - ✅ Guaranteed the agent has the context - ⚠️ Uses tokens even if agent doesn't need it ### When to Use Each Approach | Use `@` (Immediate) | Omit `@` (Lazy) | |---------------------|-----------------| | Context always required | Context might be needed | | Short, critical files | Large reference files | | Agent must follow exact format | Agent can infer or discover | | Templates and schemas | Documentation and guides | **Pro tip:** Modern agents are good at discovering needed files through `codebase_search` or exploring directories, but being explicit about important files helps ensure consistency. --- ## Research-Backed Context Architecture ### Three-Layer Progressive Architecture Based on validated research from Stanford, Anthropic (2025), and recent AI agent engineering studies, effective context loading follows a three-layer progressive architecture: #### Layer 1: Static Base Context (First 15% of Prompt) This is your foundational layer, grounded in Stanford's position sensitivity research. **Critical instructions positioned early dramatically improve adherence.** **What goes here:** - Role definition (5-10%) - Critical rules (defined once, early positioning) - Task definition (clear objective) - Constraint summary (high-level "must/must-not") **Example:** ```xml Expert Prompt Architect Critical rules MUST be in first 15% Max nesting: 4 levels Research patterns (non-negotiable) ``` **Why this matters:** Research shows position sensitivity improves adherence across model sizes. Critical rules at the start are more reliably followed than rules buried later in the prompt. #### Layer 2: Lazy-Loaded Context (Just-in-Time Retrieval) This layer uses the "just-in-time" approach validated by Anthropic's 2025 research on effective context engineering for AI agents. > Rather than pre-processing all relevant data up front, agents built with the "just-in-time" approach maintain lightweight identifiers (file paths, stored queries, web links, etc.) and use these references to dynamically load data into context at runtime using tools. **Implementation:** ```xml .opencode/context/core/standards/code.md .opencode/context/core/practices/review.md .opencode/context/core/workflows/delegation.md ``` **Key insight:** This enables **progressive disclosure**. Agents incrementally discover context through exploration: - File naming hints at purpose (`.../standards/code.md` vs `.../practices/review.md`) - File size signals complexity - Timestamps indicate recency - Folder structure provides hierarchical context **Token efficiency gain:** - Pre-loading all context: **2,000-5,000+ tokens** - Just-in-time loading: **300-1,000 tokens per file** (agents typically need 2-3 files, not 10) - **Net savings: 60-80% reduction in token usage** #### Layer 3: Dynamic Runtime Context (Execution-Time Loading) This layer handles long-horizon tasks and evolving context through compaction and memory strategies. **Implementation:** ```xml .opencode/sessions/[task-id].md ``` **Research backing:** For tasks spanning multiple turns: - **Compaction**: Summarize context nearing window limit, reinitiate with summary + critical details - **Structured note-taking**: Agent maintains notes in persistent memory (like session files), pulled back in later - **Progressive context assembly**: Each interaction yields context informing the next decision ### Progressive Disclosure by Design Structure your context hierarchy so agents discover relevance layer-by-layer: ``` .opencode/context/core/ ├── standards/ ← Broad guidelines (agent starts here) │ ├── code.md ← File naming hints: "code" standards │ ├── tests.md ← "tests" = test-specific rules │ └── patterns.md ← "patterns" = architectural patterns ├── practices/ ← Execution-level practices │ ├── analysis.md ← "analysis" = how to analyze │ └── review.md ← "review" = code review criteria ├── workflows/ ← Process templates │ ├── delegation.md ← Delegation patterns │ ├── task-breakdown.md ← Task analysis workflows │ └── sessions.md ← Session management └── system/ └── context-guide.md ← System internals ``` Each folder name signals purpose. Each file name within folders signals specific application. This is **progressive disclosure by design** - agents can navigate the hierarchy based on their current needs. ### Long-Horizon Task Strategies For multi-hour tasks spanning context resets: **Compaction Strategy:** ```xml Preserve: Architectural decisions, unresolved bugs, key insights Discard: Redundant tool outputs, repeated attempts • Decisions made • Current phase • Next steps • Unresolved issues ``` **Example:** Anthropic's Claude Code maintains precise tallies across thousands of steps using persistent notes - the same pattern your session files implement. ### Anti-Patterns to Avoid Research identifies these common mistakes that reduce effectiveness: #### ❌ Anti-Pattern 1: Overload Static Context ```xml Fix typo in button ``` **Problems:** - Attention budget exhaustion (n² transformer relationships) - Reduced position sensitivity effectiveness (critical rules get buried) - Worse adherence to specific instructions **Solution:** Use lazy loading for large reference materials. #### ❌ Anti-Pattern 2: Deep Nesting ```xml ``` **Problem:** Nesting depth >4 levels reduces clarity significantly. **Solution:** Use attributes and flatter structures with max 4 levels. #### ❌ Anti-Pattern 3: Repeating Critical Rules ```xml Always request approval ... 2000 tokens later ... Always get approval before execution ... 3000 tokens later ... Approval must be obtained ``` **Problem:** Repetition causes ambiguity and wastes tokens. **Solution:** Define rules once in the first 15%, reference them with `@rule_id` elsewhere. ### Token Efficiency Metrics Real-world measurements from production agent systems: | Approach | Token Cost | Files Loaded | Efficiency | |----------|-----------|--------------|------------| | Pre-load all context | 2,000-5,000+ | 10-15 files | Baseline | | Just-in-time loading | 300-1,000 | 2-3 files | **60-80% savings** | | With compaction | 500-1,500 | 3-5 files | **50-70% savings** | **Key insight:** Agents typically need only 2-3 context files per task, not the entire knowledge base. ### Research Validation Summary | Pattern | Research Basis | Effect | |---------|----------------|--------| | **Position Sensitivity** | Stanford multi-instruction study | Improves adherence (varies by task/model) | | **Just-in-Time Loading** | Anthropic context engineering (2025) | 60-80% token reduction | | **Progressive Disclosure** | Anthropic agent research | Agents discover context incrementally | | **Nesting Depth ≤4** | Anthropic XML research | Reduces complexity, improves clarity | | **Compaction + Memory** | Anthropic long-horizon tasks | Maintains coherence across resets | ### Research Sources - **Anthropic (2025)**: "Effective Context Engineering for AI Agents" - **Stanford**: Multi-instruction position sensitivity study - **Anthropic**: "XML Prompting as Grammar-Constrained Interaction" (ArXiv 2509.08182) - **ODSC (2025)**: "Building Dynamic In-Context Learning for Self-Optimizing Agents" - **ArXiv**: "Optimization of Retrieval-Augmented Generation Context with Outlier Detection" (2407.01403) --- ## Custom Instruction Files **For arbitrary paths, use `instructions` field:** ```json { "instructions": [ "~/opencode/context/my-context.md", "docs/**/*.md", ".opencode/context/**/*.md" ] } ``` **Paths can be:** - Absolute: `/path/to/file.md` - Home relative: `~/path/to/file.md` - Repo relative: `docs/instructions.md` - Glob patterns: `**/*.md` ## Config Merging **Configs merge with priority** (later overrides earlier): 1. Global config (`~/.config/opencode/`) 2. Repo root configs (from root up) 3. Custom config directories (`.opencode/` folders) 4. Environment variables (`OPENCODE_CONFIG`) **Agents, commands, and plugins** from all locations are merged together. ## Quick Reference | What | Where | How | |------|-------|-----| | Global agent | `~/.config/opencode/agent/name.md` | Auto-loaded | | Local agent | `.opencode/agent/name.md` | Auto-loaded | | Global command | `~/.config/opencode/command/name.md` | Auto-loaded | | Local command | `.opencode/command/name.md` | Auto-loaded | | Global instructions | `~/.config/opencode/AGENTS.md` | Auto-loaded | | Local instructions | `.opencode/AGENTS.md` or `AGENTS.md` | Auto-loaded | | Custom files | Anywhere | Use `instructions` config or `@` symbol |