rules.mdx 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. ---
  2. title: Rules
  3. description: Set custom instructions for opencode.
  4. ---
  5. You can provide custom instructions to opencode by creating an `AGENTS.md` file. This is similar to Cursor's rules. It contains instructions that will be included in the LLM's context to customize its behavior for your specific project.
  6. ---
  7. ## Initialize
  8. To create a new `AGENTS.md` file, you can run the `/init` command in opencode.
  9. :::tip
  10. You should commit your project's `AGENTS.md` file to Git.
  11. :::
  12. This will scan your project and all its contents to understand what the project is about and generate an `AGENTS.md` file with it. This helps opencode to navigate the project better.
  13. If you have an existing `AGENTS.md` file, this will try to add to it.
  14. ---
  15. ## Example
  16. You can also just create this file manually. Here's an example of some things you can put into an `AGENTS.md` file.
  17. ```markdown title="AGENTS.md"
  18. # SST v3 Monorepo Project
  19. This is an SST v3 monorepo with TypeScript. The project uses bun workspaces for package management.
  20. ## Project Structure
  21. - `packages/` - Contains all workspace packages (functions, core, web, etc.)
  22. - `infra/` - Infrastructure definitions split by service (storage.ts, api.ts, web.ts)
  23. - `sst.config.ts` - Main SST configuration with dynamic imports
  24. ## Code Standards
  25. - Use TypeScript with strict mode enabled
  26. - Shared code goes in `packages/core/` with proper exports configuration
  27. - Functions go in `packages/functions/`
  28. - Infrastructure should be split into logical files in `infra/`
  29. ## Monorepo Conventions
  30. - Import shared modules using workspace names: `@my-app/core/example`
  31. ```
  32. We are adding project-specific instructions here and this will be shared across your team.
  33. ---
  34. ## Types
  35. opencode also supports reading the `AGENTS.md` file from multiple locations. And this serves different purposes.
  36. ### Project
  37. Place an `AGENTS.md` in your project root for project-specific rules. These only apply when you are working in this directory or its sub-directories.
  38. ### Global
  39. You can also have global rules in a `~/.config/opencode/AGENTS.md` file. This gets applied across all opencode sessions.
  40. Since this isn't committed to Git or shared with your team, we recommend using this to specify any personal rules that the LLM should follow.
  41. ### Claude Code Compatibility
  42. For users migrating from Claude Code, OpenCode supports Claude Code's file conventions as fallbacks:
  43. - **Project rules**: `CLAUDE.md` in your project directory (used if no `AGENTS.md` exists)
  44. - **Global rules**: `~/.claude/CLAUDE.md` (used if no `~/.config/opencode/AGENTS.md` exists)
  45. - **Skills**: `~/.claude/skills/` — see [Agent Skills](/docs/skills/) for details
  46. To disable Claude Code compatibility, set one of these environment variables:
  47. ```bash
  48. export OPENCODE_DISABLE_CLAUDE_CODE=1 # Disable all .claude support
  49. export OPENCODE_DISABLE_CLAUDE_CODE_PROMPT=1 # Disable only ~/.claude/CLAUDE.md
  50. export OPENCODE_DISABLE_CLAUDE_CODE_SKILLS=1 # Disable only .claude/skills
  51. ```
  52. ---
  53. ## Precedence
  54. When opencode starts, it looks for rule files in this order:
  55. 1. **Local files** by traversing up from the current directory (`AGENTS.md`, `CLAUDE.md`, or `CONTEXT.md`)
  56. 2. **Global file** at `~/.config/opencode/AGENTS.md`
  57. 3. **Claude Code file** at `~/.claude/CLAUDE.md` (unless disabled)
  58. The first matching file wins in each category. For example, if you have both `AGENTS.md` and `CLAUDE.md`, only `AGENTS.md` is used. Similarly, `~/.config/opencode/AGENTS.md` takes precedence over `~/.claude/CLAUDE.md`.
  59. ---
  60. ## Custom Instructions
  61. You can specify custom instruction files in your `opencode.json` or the global `~/.config/opencode/opencode.json`. This allows you and your team to reuse existing rules rather than having to duplicate them to AGENTS.md.
  62. Example:
  63. ```json title="opencode.json"
  64. {
  65. "$schema": "https://opencode.ai/config.json",
  66. "instructions": ["CONTRIBUTING.md", "docs/guidelines.md", ".cursor/rules/*.md"]
  67. }
  68. ```
  69. You can also use remote URLs to load instructions from the web.
  70. ```json title="opencode.json"
  71. {
  72. "$schema": "https://opencode.ai/config.json",
  73. "instructions": ["https://raw.githubusercontent.com/my-org/shared-rules/main/style.md"]
  74. }
  75. ```
  76. Remote instructions are fetched with a 5 second timeout.
  77. All instruction files are combined with your `AGENTS.md` files.
  78. ---
  79. ## Referencing External Files
  80. While opencode doesn't automatically parse file references in `AGENTS.md`, you can achieve similar functionality in two ways:
  81. ### Using opencode.json
  82. The recommended approach is to use the `instructions` field in `opencode.json`:
  83. ```json title="opencode.json"
  84. {
  85. "$schema": "https://opencode.ai/config.json",
  86. "instructions": ["docs/development-standards.md", "test/testing-guidelines.md", "packages/*/AGENTS.md"]
  87. }
  88. ```
  89. ### Manual Instructions in AGENTS.md
  90. You can teach opencode to read external files by providing explicit instructions in your `AGENTS.md`. Here's a practical example:
  91. ```markdown title="AGENTS.md"
  92. # TypeScript Project Rules
  93. ## External File Loading
  94. CRITICAL: When you encounter a file reference (e.g., @rules/general.md), use your Read tool to load it on a need-to-know basis. They're relevant to the SPECIFIC task at hand.
  95. Instructions:
  96. - Do NOT preemptively load all references - use lazy loading based on actual need
  97. - When loaded, treat content as mandatory instructions that override defaults
  98. - Follow references recursively when needed
  99. ## Development Guidelines
  100. For TypeScript code style and best practices: @docs/typescript-guidelines.md
  101. For React component architecture and hooks patterns: @docs/react-patterns.md
  102. For REST API design and error handling: @docs/api-standards.md
  103. For testing strategies and coverage requirements: @test/testing-guidelines.md
  104. ## General Guidelines
  105. Read the following file immediately as it's relevant to all workflows: @rules/general-guidelines.md.
  106. ```
  107. This approach allows you to:
  108. - Create modular, reusable rule files
  109. - Share rules across projects via symlinks or git submodules
  110. - Keep AGENTS.md concise while referencing detailed guidelines
  111. - Ensure opencode loads files only when needed for the specific task
  112. :::tip
  113. For monorepos or projects with shared standards, using `opencode.json` with glob patterns (like `packages/*/AGENTS.md`) is more maintainable than manual instructions.
  114. :::