Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

OpenCode Rules (AGENTS.md)

OpenCode Rules (AGENTS.md)

In OpenCode,Rules are the core mechanism for controlling AI behavior。

We can use AGENTS.md files that provide custom instructions to OpenCode, letting it develop according to your project standards.

Tools determine what AI can do; AGENTS.md determines what AI should do.

AGENTS.md = defining team development standards for AI.


I. What is AGENTS.md

AGENTS.md is a rules file used to:

These contents are added to the LLM's context.

Effect:


II. Quick initialization

Run in OpenCode:


/init

This command will:

Note:


III. Example structure


# Project Description

这是一个基于 TypeScript 的 monorepo 项目。

## Project Structure

- packages/ 核心模块
- infra/ 基础设施
- web/ 前端应用

## 代码规范

- 使用 TypeScript 严格模式
- 公共代码放在 packages/core
- 所有函数必须添加注释

## Development Conventions

- 使用 workspace 方式引用模块
- API 必须统一错误处理

Essence: writing team standards "for AI to read"


IV. Scope of rules

1. Project-level rules

Path:


./AGENTS.md

Function:

2. Global rules

Path:


~/.config/opencode/AGENTS.md

Function:

Recommended uses:


V. Claude Code compatibility

OpenCode is compatible with Claude Code's rules system:

If you don't want to use it, you can disable it:


export OPENCODE_DISABLE_CLAUDE_CODE=1

VI. Rule loading priority (important)

OpenCode loads in the following order at startup:

  1. Search upward from the current directory for AGENTS.md
  2. If none → search for CLAUDE.md
  3. Global rules ~/.config/opencode/AGENTS.md
  4. Claude global rules ~/.claude/CLAUDE.md

Key points:


VII. Extended rules (instructions)

can be done during opencode.json to load additional rule files:


{
  "instructions": [
    "CONTRIBUTING.md",
    "docs/guidelines.md",
    ".cursor/rules/*.md"
  ]
}

Fixed size, stores elements of the same type.

Support remote rules


{
  "instructions": [
    "https://raw.githubusercontent.com/xxx/rules/main/style.md"
  ]
}

Explanation:


VIII. Modular rules (advanced)

Recommended approach (using instructions)


{
  "instructions": [
    "docs/dev.md",
    "test/rules.md",
    "packages/*/AGENTS.md"
  ]
}

Suitable for:

Method 2: manually reference in AGENTS.md


# Rule loading instructions

当遇到 @xxx 文件时,请使用 read 工具加载。

- 不要一次性加载全部文件
- 按需加载
- 加载后必须遵守

Example:


TypeScript 规范:@docs/typescript.md
React 规范:@docs/react.md
API 规范:@docs/api.md

Implement "load rules on demand"


IX. Best practices (key points)

1. Rules should be specific

❌ Do not write:


代码要规范

✅ Should write:


所有函数必须添加 JSDoc 注释

2. Rules should be actionable

AI can only execute explicit rules, not abstract descriptions.

3. Don't write too long

Note:

4. Unified team maintenance