instructions.mdx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. ---
  2. title: "Instructions"
  3. description: ""
  4. ---
  5. Instructions are privileged context that guide an agent throughout a session.
  6. V2 combines built-in context, discovered `AGENTS.md` files, and dynamic sources
  7. such as skill, reference, MCP, and session context. It stores source values as
  8. durable deltas, then renders initial instructions and chronological updates when
  9. assembling each model request.
  10. ## AGENTS.md
  11. Use `AGENTS.md` for persistent guidance such as build commands, architecture,
  12. code conventions, and verification requirements. Commit project files so the
  13. whole team receives the same instructions.
  14. V2 loads:
  15. 1. The global file at `$XDG_CONFIG_HOME/opencode/AGENTS.md`, normally
  16. `~/.config/opencode/AGENTS.md`.
  17. 2. Every `AGENTS.md` from the current Location up to and including the project
  18. root.
  19. For example, when the Location is `packages/web`, OpenCode can load all three
  20. project files below:
  21. ```text
  22. my-project/
  23. ├── AGENTS.md
  24. └── packages/
  25. ├── AGENTS.md
  26. └── web/
  27. └── AGENTS.md
  28. ```
  29. The files are combined rather than selecting a single winner. They are rendered
  30. in this order: global, then project files from the Location toward the project
  31. root. OpenCode does not resolve conflicts between their contents, so keep broad
  32. guidance global and put scoped guidance in the relevant project directory.
  33. If the Location is outside the project root, only the global file is loaded.
  34. Setting `OPENCODE_DISABLE_PROJECT_CONFIG=1` also skips project `AGENTS.md`
  35. discovery but does not disable the global file.
  36. <Note>
  37. Current V2 discovery only recognizes `AGENTS.md`. The `CLAUDE.md` fallback
  38. and related precedence described by older OpenCode documentation do not apply.
  39. </Note>
  40. ### Nested instructions
  41. An `AGENTS.md` below the Location is not part of the initial upward scan. When
  42. the read tool successfully reads a file or lists a directory, OpenCode discovers
  43. `AGENTS.md` files from that target upward to, but not including, the Location.
  44. It adds newly discovered files to the session in nearest-first order.
  45. Each nested file is injected once per session and recorded in durable session
  46. history. Reading the same area again does not inject it again. Consequently,
  47. editing an already injected nested `AGENTS.md` does not replace its earlier
  48. session entry automatically; start a new session if the updated text must apply
  49. immediately.
  50. ## Config entries
  51. The V2 config schema accepts an `instructions` array of strings:
  52. ```jsonc title="opencode.jsonc"
  53. {
  54. "$schema": "https://opencode.ai/config.json",
  55. "instructions": [
  56. "CONTRIBUTING.md",
  57. "docs/guidelines/*.md",
  58. "https://example.com/shared-instructions.md"
  59. ]
  60. }
  61. ```
  62. Configuration is loaded from global through project-local files. If more than
  63. one config defines `instructions`, the highest-precedence, closest config's
  64. entire array is selected; arrays are not merged.
  65. <Warning>
  66. V2 currently parses and retains this field but does not resolve its entries
  67. into instruction sources. Local files, glob patterns, and HTTP or HTTPS URLs
  68. in `instructions` therefore do not reach the model yet. Use `AGENTS.md` for
  69. active V2 instructions. URL fetching and timeout behavior documented for V1
  70. are not supported by the current V2 implementation.
  71. </Warning>
  72. See [Config](/config) for config locations and general precedence.
  73. ## Ordering
  74. The selected agent or provider system prompt is sent first. OpenCode then sends
  75. the session's initial instructions, composed in this order:
  76. 1. Built-in environment and date context.
  77. 2. Ambient `AGENTS.md` discovery.
  78. 3. Available skill, reference, and MCP guidance.
  79. 4. Session-specific instruction entries supplied through the API.
  80. These sources are combined; ordering is not an override mechanism. Nested
  81. `AGENTS.md` files discovered by reads are chronological session entries rather
  82. than part of the initial instructions.
  83. ## Changes
  84. Before promoting pending input, V2 compares live instruction sources with the
  85. latest admitted source values:
  86. - A new or changed ambient `AGENTS.md` aggregate is announced as a system update
  87. that replaces the previous ambient aggregate.
  88. - Removing all ambient files announces that the previous ambient instructions
  89. no longer apply.
  90. - A temporary read or discovery failure preserves the session's last known
  91. instructions instead of treating them as deleted. If no instruction epoch
  92. exists yet, pending input waits until every source is available.
  93. - Completed conversation compaction advances the instruction epoch, making the
  94. currently admitted values initial without rereading sources or authoring an
  95. instruction event.
  96. - Moving a session or committing a revert clears the instruction fold. The next
  97. safe boundary requires one complete source read before promoting input.
  98. The durable event stores changed source keys and value hashes, not rendered
  99. prose. During request assembly, OpenCode renders the epoch's initial values and
  100. interleaves later changes as chronological System messages. Clients see changed
  101. keys but never the privileged value bodies.