Skip to content

[FEATURE]: Context Auto-Discovery: Load AGENTS.md from Directories When Accessing Files #6316

Closed

Description

@Ithril-Laydec

Feature hasn't been suggested before.

  • I have verified this feature I'm about to request hasn't been suggested before.

Describe the enhancement you want to request

Problem

In large monorepos, we have to choose what information to load into context. With a large AGENTS.md file, we load a lot of context that might not be necessary for all conversations.

Typical structure example:

monorepo/
├── AGENTS.md              # Global project rules
├── packages/
│   ├── backend/
│   │   ├── src/
│   │   └── AGENTS.md      # Backend-specific rules
│   ├── frontend/
│   │   ├── src/
│   │   └── AGENTS.md      # Frontend-specific rules
│   └── mobile/
│       ├── src/
│       └── AGENTS.md      # Mobile-specific rules

Current workaround: Manually list all instruction files in opencode.json:

{
  "instructions": [
    "AGENTS.md",
    "packages/backend/AGENTS.md",
    "packages/frontend/AGENTS.md",
    "packages/mobile/AGENTS.md"
  ]
}

Problem with this approach: This loads ALL instruction files at startup, overloading the context with irrelevant information (e.g., loading mobile rules when working on backend code). This doesn't scale and defeats the purpose of having per-module context.


Proposed Solution

Mimic Claude Code's auto-discovery so that when a file is read or written in a subdirectory, the relevant AGENTS.md is automatically loaded without the LLM having to manually decide to read it.

Features:

  1. Loaded file cache to avoid duplicates in the same session
  2. Configuration option to disable the feature if not desired
  3. Two loading strategies:
    • "up": Search for AGENTS.md upward to the worktree (default)
    • "exact": Only load AGENTS.md from the exact file directory

Advanced Configuration

{
  "autoDiscovery": {
    "enabled": true,
    "patterns": ["AGENTS.md", "CLAUDE.md"],
    "strategy": "up" // "up" | "exact"
  }
}

Why This Matters

  • ✅ Zero-config: Works without configuration for standard project structures
  • ✅ Automatic scalability: No need to update config when adding modules
  • ✅ Better context accuracy: LLM receives relevant instructions automatically
  • ✅ Monorepo support: Essential for modern architectures (Next.js, Turborepo, Nx)
  • ✅ Parity with Claude Code: Users expect similar behavior across AI coding tools

Step-by-Step Flow: "up" Strategy (Search Upward)

Structure:
monorepo/
├── AGENTS.md                    # [A] Global rules
├── packages/
│   └── backend/
│       ├── AGENTS.md            # [B] Backend rules
│       └── routes/
│           ├── AGENTS.md        # [C] Routes rules
│           └── auth.ts          # ← File being read

LLM: readFile("packages/backend/routes/auth.ts")

Step 1: Detect directory → "packages/backend/routes/"
Step 2: Search upward:
  → packages/backend/routes/AGENTS.md  ✅ [C] Found! → Load
  → packages/backend/AGENTS.md         ✅ [B] Found! → Load
  → packages/AGENTS.md                 ❌ Does not exist
  → AGENTS.md                          ✅ [A] Already loaded by system

Result: LLM receives context from [C] + [B] + [A]

Step-by-Step Flow: "exact" Strategy (Current Directory Only)

Same structure as before:
monorepo/
├── AGENTS.md                    # [A] Global rules
├── packages/
│   └── backend/
│       ├── AGENTS.md            # [B] Backend rules
│       └── routes/
│           ├── AGENTS.md        # [C] Routes rules
│           └── auth.ts          # ← File being read

LLM: readFile("packages/backend/routes/auth.ts")

Step 1: Detect directory → "packages/backend/routes/"
Step 2: Search ONLY in that directory:
  → packages/backend/routes/AGENTS.md  ✅ [C] Found! → Load
  → (Does NOT search in parent directories)

Result: LLM receives context from [C] + [A] (root already loaded)

When to use each strategy?

  • "up" (recommended): When you want the LLM to have full module + submodule context
  • "exact": When each subdirectory has completely independent context and you don't want to "inherit" rules from parent folders

Difference with #4479

I've read issue #4479 and consider this proposal to be complementary but not the same:

Both features can coexist and solve different use cases in complex monorepos.


Implementation Offer

If this sounds good, I'd like to implement it myself or help implement it.


Activity

  1. added
    discussionUsed for feature requests, proposals, ideas, etc. Open discussion
    on Dec 28, 2025
  2. github-actions commented on Dec 28, 2025

    @github-actions
    Contributor

    This issue might be a duplicate of existing issues. Please check:

    The main difference with your proposal is that you're suggesting automatic discovery based on file access patterns (when a file is read/written), while #4479 focuses on configurable search boundaries and file types. Your approach complements and extends these existing proposals by adding dynamic, context-aware loading rather than just configuration-based approaches.

    Feel free to ignore if your specific use case differs from these issues.

  3. ragnsan commented on Dec 31, 2025

    @ragnsan

    Yes, this would be sick. Was just looking for this. Claude Code does this and it's super valuable. We also have a lot of nested AGENTS.md in packages and apps in our monorepos and don't want to pollute the context window with files that are not needed in a session

  4. WilliamsHoward commented on Jan 5, 2026

    @WilliamsHoward

    Couldn't agree more. In practical applications, project structures are frequently large and complex, significantly increasing token overhead. This feature allows for more efficient token utilization. can't wait to have this feature in opencode.

  5. YourTechBud commented on Jan 10, 2026

    @YourTechBud

    This is such an important feature to have. I work in a monorepo and its extremely tiring to reference all relevant AGENTS.md file by hand. This is supposed to be a part of the Agents.md spec as well.

    4. Large monorepo? Use nested AGENTS.md files for subprojects

    Place another AGENTS.md inside each package. Agents automatically read the nearest file in the directory tree, so the closest one takes precedence and every subproject can ship tailored instructions. For example, at time of writing the main OpenAI repo has 88 AGENTS.md files.

  6. ragnsan commented on Jan 10, 2026

    @ragnsan

    This is such an important feature to have. I work in a monorepo and its extremely tiring to reference all relevant AGENTS.md file by hand. This is supposed to be a part of the Agents.md spec as well.

    4. Large monorepo? Use nested AGENTS.md files for subprojects

    Place another AGENTS.md inside each package. Agents automatically read the nearest file in the directory tree, so the closest one takes precedence and every subproject can ship tailored instructions. For example, at time of writing the main OpenAI repo has 88 AGENTS.md files.

    Yeah, agreed. Have made a plugin to have the agent read nested AGENTS.md files in subdirs until this (hopefully) becomes a built-in!

  7. YourTechBud commented on Jan 10, 2026

    @YourTechBud

    @ragnsan would ve lovely uf you could share some details about the plugin. Maybe a github repo link?

  8. bailey-busc commented on Jan 19, 2026

    @bailey-busc

    I have a functioning implementation of a similar idea as described in #9511, but I like the configuration approach you outline here better than what I came up with. Current plan is to adapt it to the configuration interface proposed here and PR it.

  9. TomLucidor commented on Jan 20, 2026

    @TomLucidor

    Is this part of the AGENTS.md shared specs?

  10. allisoneer commented on Jan 21, 2026

    @allisoneer

    #7361

    I went with the "vanilla" approach. But damn that advanced config looks potentially sick. I actually had a similar pattern matching setup in my todo list for my own tools. Would be rad if it was in opencode directly and with reasonable defaults.

  11. YourTechBud commented on Jan 31, 2026

    @YourTechBud

    I think this has already been implemented now:

    Image
  12. YourTechBud commented on Jan 31, 2026

    @YourTechBud

    @thdxr we can close this now

  13. YuukanOO commented on Feb 11, 2026

    @YuukanOO

    I don't think this was implemented (tested on v1.1.59). I have simple sandbox with multiple directories. In each directory, I put an AGENTS.md file with this rule (with a different header in each subdirectory):

    Every file in this directory should start with:
    
    // This file was generated by an agent in a nested folder.

    Using Kimi K2.5 and the following prompt:

    create a js hello world in @frontend/nested_folder/ 
    

    I got the file frontend/nested_folder/hello.js:

    // This file was generated by an agent.
    
    console.log("Hello World");

    Which match the root AGENTS.md instruction, not the nested one.

    Image
  14. YourTechBud commented on Feb 11, 2026

    @YourTechBud

    @YuukanOO i think auto loading only works when you read a files. Globs dont count.

  15. ragnsan commented on Feb 12, 2026

    @ragnsan

    Yeah, confirming that it's working for me and that I yoinked out our custom plugin

  16. github-actions commented on Apr 14, 2026

    @github-actions
    Contributor

    To stay organized issues are automatically closed after 90 days of no activity. If the issue is still relevant please open a new one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

discussionUsed for feature requests, proposals, ideas, etc. Open discussion

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions