How OpenCode Assembles an Agent Session
This note describes how OpenCode builds and runs an agent for each session: the main objects, the step loop, how the context sent to the model is built, the instruction sources and what persists, and the tools. It follows the sections of How Codex Assembles an Agent Session so the two can be compared.
Source
Repository:~/projects/common/opencode, commit d759c548aa (2026-11-14). This is the V2 runtime. Paths are relative to packages/core/src/ unless they start with packages/, specs/ or a root file name. Line numbers will drift as the code changes. The design specs specs/v2/session.md and specs/v2/tools.md explain the contracts; where the code differs from them, the note says so.Main concepts
Server and session startup
One server for all clients
OpenCode runs as a background server. The TUI, the desktop app and other clients connect to it.
opencode service startspawnsopencode serve --service(packages/cli/src/services/service-config.ts:150). The server registers itself in<XDG_STATE_HOME>/opencode/service.jsonand listens on port0xc0deby default (service-config.ts:41-58).- The TUI and the desktop app both call
Service.ensure, which reuses a healthy server of the same version, replaces one with a version mismatch, or starts one (packages/client/src/effect/service.ts:49-127). --server <url>connects to a given server.--standalonestarts a private server as a child process (packages/cli/src/services/server-connection.ts:27-63).- Each request names its Location with the
location[directory]query parameter or thex-opencode-directoryheader (packages/server/src/location.ts:74-79).
What a Location builds
Instance.layer lists the services (instance.ts:64-121). Services marked global are built once per process: the database, the event bus, projects, sessions, execution and restart, jobs, schedules, worktrees and others. The rest are built once per Location, in list order after their dependencies: App, Location (resolves the project), lifecycle, environment, config, plugins and hooks, the plugin supervisor, snapshots, the model runner and VCS, and the file watcher last.
A Location is evicted after 60 minutes without activity (location-activity.ts:30).
Plugin activation order
The plugin supervisor activates plugins at startup and again, debounced by 100 ms, when their sources change (plugin/supervisor.ts:193-232). The order is (plugin/internal.ts:247-310):
- Built-in plugins that must run first, among them tool-input repair, MCP config and Code Mode defaults, the built-in agents, the plan agent, commands, goals, skills, the models catalog, providers, web search, the patch tool, the per-model prompt plugins and the built-in tools.
- SDK and instance plugins.
- Plugins from config packages.
- Built-in plugins that must run last: configured instructions, references, agents, commands, providers, web search and policy.
The OpenCode provider plugin and the policy plugin can’t be removed (plugin/internal.ts:314).
Step loop
Input arrives through Session.prompt. It is stored in the inbox, and execution is woken (specs/v2/session.md). If a drain is already running, the input waits in the inbox and is delivered at the next step boundary.
flowchart TD
A[User input] --> B[Store in inbox, wake session]
B --> C{Drain running?}
C -- yes --> D[Deliver at next step boundary]
C -- no --> E[Start drain: record claim, fail orphaned tool calls]
E --> F[Deliver inbox items]
F --> G[Select agent, permissions, tools, instruction sources]
G --> H[Sync instruction changes, load history and model]
H --> I{Over the context budget?}
I -- yes --> J[Compact]
J --> G
I -- no --> K[Build request, run hooks, stream model reply]
K --> L{Tool calls?}
L -- yes --> M[Save each call, run calls concurrently, save results]
M --> F
L -- no --> N{Queued input or goal continues?}
N -- yes --> F
N -- no --> O[Release claim, session idle]
Details of each part:
- Drain start. The session records a claim (
time_suspended) in the same commit as the start event (session/execution.ts:75-81). Tool calls still marked running from an earlier process are failed before any model work, so their side effects are never replayed (specs/v2/session.md). - Selection (
session/context.ts:136-176). The runner loads the session, resolves the agent, merges agent and session permissions, and loads in parallel the tool snapshot, built-in instructions, AGENTS.md files, skills, references, MCP guidance and API instruction entries. - Loading (
session/context.ts:178-195). It resolves the model and reads history after the latest compaction. - Request (
session/model-request.ts:106,:273).baseTranscriptbuilds the system parts and messages. Plugin hooks can change the system parts, messages, options and tools (model-request.ts:463-471). Then the model reply is streamed. - Tool calls. Each call is saved before it runs. Calls start as soon as their call event is published, while the stream continues, and are awaited together (
session/runner/step.ts:145-182). After the results are saved, the runner reloads history and starts a new step. - Step limit. Agents have no step limit unless their
stepsfield is set; no built-in agent sets it. At the limit, the runner appendsMAX_STEPS_PROMPTas an assistant message and setstoolChoice: "none", keeping the tool definitions so the cached prompt prefix still matches (session/runner/llm.ts:261,:276-281). The step count resets when new user input is delivered. - Idle. When the session would go idle, the
experimental.idlehook can admit more input so the drain continues. The goals plugin uses this (Goals and schedules).
Retry
Retries cover rate limits, provider internal errors, transport failures and incomplete streams (packages/ai/src/schema/errors.ts:197-224). Authentication, quota, content policy and invalid requests are not retried.
The schedule is exponential from 2 seconds, each wait capped at 10 seconds, for at most 10 retries, with jitter. A provider’s retry-after value is used as a minimum, up to 15 minutes (model-retry.ts:12, :22-36). Network-unreachable errors have a separate budget of one hour. The retry hook can change the decision.
Code differs from spec
specs/v2/session.md says the initial request plus at most four retries. The code allows 10 retries.A context overflow is handled by compaction, not retry (Compaction).
How the context is built
A request has system parts, messages and tool definitions.
System parts
baseTranscript builds two system parts (session/model-request.ts:106-137):
- The base prompt. The agent’s
systemfield if it has one. Otherwise the default prompt insession/runner/prompt/system.txt, a short text about the harness, communication and working in codebases.SessionSystemPrompt.makeinserts guidance for the shell, write and edit tools when they are present (session/system-prompt.ts). - The instruction baseline: the joined initial values of all instruction sources.
The per-model prompt plugins (plugin/optimize.ts) then change the first part through the context hook, unless the agent has its own system:
- GPT models get
gpt.txtin place of the default, orgpt-astra.txtfor ids containing “astra”. - Claude models get
anthropic.txtadded after the default. - Kimi, Arcee Trinity and Meta Muse models get their own prompts.
Instruction sources
SessionContext.select combines the sources in this order (session/context.ts:165-173). The comment there explains the order: from most shared across sessions to least, so the start of the request stays the same and can be served from the provider’s prompt cache.
- Code Mode catalog of tools callable inside
execute(Code Mode). - MCP guidance from connected servers. Omitted for servers the permissions deny (
mcp/instructions.ts:73-86). - References: extra directories the agent may read, each with a name, path and description. Only references with a description are listed (
reference/instructions.ts:58-65). - Skills: name and description only (Skills).
- AGENTS.md and configured instructions (AGENTS.md).
- Built-ins: today’s date and an
<env>block with the working directory, the workspace root, whether it is a git repository, the platform, and the temporary directory to use (instructions/builtins.ts). - Instruction entries added by plugins or clients through the API, at most 256 KiB each (
packages/schema/src/instruction-entry.ts:30).
First step, or after compaction
The baseline in the second system part holds the values that were current at the start of the instruction epoch. An epoch starts with the session and starts again after each completed compaction, when the current values become the new baseline.
Later steps
Before each step that calls the model, the runner reads every source once, hashes the values, and stores one delta with the changed keys (specs/v2/session.md). A changed source is rendered once and added to history as a system message with metadata.notice: "instructions" (session/message-updater.ts:154-165). The baseline is not rewritten, so the cached prefix survives. Each source decides how to describe its change: AGENTS.md changes are sent as a diff when the diff is shorter than the new text (instruction-discovery.ts:127-150).
History
SessionHistory.entriesForRunner reads the messages and the instruction baseline in one transaction (session/history.ts:290-307). It starts from the latest completed compaction (history.ts:55-88, :203-214) and drops instruction updates older than the epoch start, because the baseline already covers them (history.ts:231-237). Media the model can’t accept is replaced by an error text, and images are bounded (model-request.ts:219-265).
Compaction
Compaction is checked before every step and when the provider reports a context overflow (session/runner/llm.ts:253-254, :319-325).
- Settings (
session/compaction.ts:269,config/plugin/compaction.ts:17-26):compaction.auto(defaulttrue),compaction.buffer(a percentage or a token count), andcompaction.keep.tokens(default 15,000). - When it runs. The window is the smaller of the model’s input and context limits. Without
buffer, the ceiling is the window minus the larger of 15% of the window and 16,000 tokens (16,000 applies only to windows of at least 32k). Compaction is due when the measured plus estimated tokens reach the ceiling (compaction.ts:1132-1171). A model with an unknown window is never compacted automatically. - Overflow. If the provider rejects the request as too long before any output, the runner compacts once and rebuilds the same step. A second overflow is terminal (
specs/v2/session.md). - What is kept. The newest exchanges up to
keeptokens, starting at a user message, plus a structured summary of the rest (compaction.ts:1014-1056). - Summary template (
compaction.ts:173-212): Objective, Requirements, Decisions, Work State (Completed, Active, Blocked), Next Move, Relevant Files (at most 15), Important Context. The prompt tells the model to leave out AGENTS.md, conventions and environment details, because the next agent receives current versions of them separately (compaction.ts:214-227). - Second compaction. The update prompt asks the model to update the existing checkpoint into one consolidated summary, with newer history winning (
compaction.ts:228-244). - Transcript for the summarizer. If the history is too large for the summarizer, older exchanges are shortened in levels: tool outputs clipped, tool outputs removed, user text only, omitted. The newest exchange is never omitted (
compaction.ts:144-148,:720-788). - Model. The hidden
compactionagent’s model if configured, else the session’s model. When the model supports provider-native compaction, that is used instead (compaction.ts:283-292,:565-603). - Background preparation. Behind the experimental flag
background_compaction, a summary is prepared before the ceiling while the provider cache is still fresh (compaction.ts:365-438). - Hook. The
compactionsession hook can edit the summarizer request, or return a result and skip the model call (compaction.ts:635). - Manual.
/compactadds a compaction item to the inbox, handled at the next step boundary (session/session.ts:259-277).
Persistence and resume
- Database.
<XDG_DATA_HOME>/opencode/opencode.dbin WAL mode.OPENCODE_DBoverrides the path (packages/cli/src/database-path.ts:4-12). - Tables (
database/schema.gen.ts): among themsession_v2,session_message,session_part,session_inbox,instruction_state,instruction_entry,instruction_blob,event,event_sequence,permission(saved approvals),job,schedule,kv,worktree. - Events and projections. Every change is published as an event, and projectors update the tables in the same transaction (
bus.ts:317-433,session/projector.ts:864-1163). The event payloads are written to theeventtable only whenpersistis set, which defaults to false (bus.ts:214). The local server doesn’t set it; only the workerd build does (packages/server/src/workerd.ts:73). On the local server the tables are the record, and history is not rebuilt from events. - Crash recovery (
session/execution/restart.ts). At startup, the managed server finds sessions whose claim was never released, fails their stale tool calls, and resumes them with the synthetic message “The server restarted while you were working. Continue from where you left off…”. After 10 attempts (DEFAULT_MAX_ATTEMPTS,restart.ts:42) it gives up with “Execution was interrupted repeatedly and will not be resumed automatically.” Recovery doesn’t guarantee exactly-once provider requests or tool effects. - Fork (
session/session.ts:341-381,projector.ts:145-214). A fork copies messages up to a chosen message (before it or through it), skipping incomplete assistant messages and running shell or compaction rows. The fork adopts the parent’s current instruction values as its baseline. - Revert (
session/revert.ts). Staging a revert restores the files that later assistant messages changed, using snapshots kept in a shadow git repository at<data>/snapshot/<projectID>/<hash>(snapshot.ts:95-97). The revert is committed on the next prompt or compaction, which deletes the later messages and rewinds instruction state. - Move (
session/move.ts). A session can move to another Location. The move goes through the inbox and is applied at a step boundary. The session’s directory and project change, alocation-switchedmessage is added, and instruction sources from the new Location arrive as updates.
Memory and instruction sources
OpenCode has no memory feature that carries knowledge from one session to the next. Knowledge that should outlast a session has to be in files: AGENTS.md, skills, references, or configured instructions.
AGENTS.md
- Global file:
~/.config/opencode/AGENTS.md(config/plugin/instruction.ts:41). - Project files: OpenCode walks from cwd up to the project root and takes every
AGENTS.mdit finds (config/plugin/instruction.ts:87). The global file comes first, then project files from nearest to farthest (instruction-discovery.ts:33-35). - Nested files. After a successful read, the
readtool walks from the file’s directory up to the Location root, exclusive, and loads anyAGENTS.mdit finds as a message, once per session (tool/plugin/read.ts:131-152,session/instructions.ts). Reads outside the project are skipped. If compaction or a revert removes that message, the file is loaded again on the next read. - Only
AGENTS.md.CLAUDE.mdis never read as an instruction file. There is no override file and no fallback file names. - Configured instructions.
instructionsin config takes paths, globs or http(s) URLs. URLs are fetched with a 5-second timeout (config/plugin/instruction.ts:120-179). - Format. Each file is rendered as
Instructions from: <path>followed by its content (instruction-discovery.ts:123-125). - Limits. No size cap on AGENTS.md or configured instruction files.
- Trust. No project trust check. Project config, AGENTS.md, plugins and skills load automatically. Environment flags such as
OPENCODE_DISABLE_PROJECT_CONFIGand managed policies are the only gates.
Skills
A skill is a folder with a SKILL.md file.
- Roots (
config/discovery.ts:152-167,config/plugin/compatibility.ts:45,config/plugin/skill.ts:87-101):skill/andskills/under each config directory,skills/under~/.claude, project.claudedirectories,~/.agentsand project.agentsdirectories, and paths or URLs listed underskillsin config. - The list of names and descriptions is always in the instruction baseline. A skill is listed only if it has a description, isn’t marked
autoinvoke: false, and isn’t denied by theskillpermission (skill/instructions.ts:61-68). The list has no size cap. - The full text is loaded by the
skilltool. Its output lists at most 10 bundled files (skill.ts:69,:88).
Hooks
Plugins register hooks in five domains: aisdk, session, permission, shell and tool (plugin/hooks.ts:13-19). Callbacks run in registration order and change the event in place. Session hooks can be limited to one provider. Types are in packages/plugin/src/effect/.
Built-in features use the same hooks. The per-model prompts and the patch tool swap use context, and the goals plugin uses experimental.idle.
Config layers
Config files are opencode.json and opencode.jsonc (config/discovery.ts:12). Load order, lowest to highest (config.ts:367-377):
- Managed policies
- Console (integration) documents, fetched from
https://opencode.ai/console/api/v2/config, limited to providers, MCP, policies and web search (config/integration.ts:32-37) - Well-known documents
- Global:
~/.config/opencode, orOPENCODE_CONFIG_DIR OPENCODE_CONFIG, an explicit file- Project
opencode.json[c]files, walking up from cwd - Project
.opencode/directories OPENCODE_CONFIG_CONTENT- Managed settings:
/Library/Application Support/opencodeon macOS,/etc/opencodeon Linux, plus a macOS managed preferences plist (config/discovery.ts:27-40)
For settings, the last loaded value wins (config.ts:28-31). For policies, the first loaded wins, so managed policy overrides Console policy, which overrides user and project policy (managed-policy.ts:9-35).
Keys that change the context (packages/schema/src/config.ts:25-117):
instructions: extra instruction files, globs or URLsreferences: extra directories with descriptionsagents.<id>.system: replaces the base prompt. In agent Markdown files, the body becomessystem(config/plugin/agent.ts:182). Other agent fields:model,steps,permissions,mode,hidden,disabled,requestskills: extra skill paths or URLscompaction:auto,buffer,keep.tokenstool_output:max_lines,max_bytespermissionsandpolicies: also remove denied skills and MCP guidance from the contextexperimental.subagent_depth,experimental.image_generate, and the feature flagstodo,background_compaction,schedules,artifacts,mcp_events,media_blob,console_watches. The flags default to on in development builds and off in releases (packages/schema/src/config/flag.ts:11-22)
Tools
Built-in tools
These tools are on the provider’s native tool list (codemode: false).
These tools are in Code Mode, in the opencode namespace and others:
session_rename,session_move,worktree_create,worktree_list,worktree_remove,models(tool/plugin/opencode.ts)schedule_create,schedule_list,schedule_pause,schedule_resume,schedule_delete: experimental flagschedulesartifact_create,artifact_read,artifact_update,artifact_delete: experimental flagartifactslist_mcp_resources,read_mcp_resource, andwatch_mcp_event(experimental flagmcp_events)
Each tool is one object with input and output schemas and one execute function (specs/v2/tools.md). If two registrations use the same name, the latest active one wins, so a plugin can replace a built-in tool.
Code Mode
A tool goes into Code Mode unless its codemode option is false (tool.ts:263-268). The default is true for plugin and MCP tools. If the execute tool is denied, every tool goes on the native list.
- Catalog. The Code Mode instruction source lists namespaces and tools within a budget of 2,000 tokens, counted as 4 characters per token, with descriptions cut to 120 characters (
codemode/catalog.ts:50-52). Every namespace line is shown; pinned tools are always listed; the rest fill the budget one tool per namespace per round. Catalog updates have a budget of 1,000 (codemode/instructions.ts:25). - Search. When the catalog can’t list every tool, the prompt tells the model to find tools with the global
search(...)function before calling them (codemode/instructions.ts:9-17). - Runtime.
executetakes JavaScript as free-form text. It runs in OpenCode’s own interpreter for a subset of JavaScript, built on the acorn parser, in the server process (packages/codemode/README.md). OpenCode sets no timeout or call count. The interpreter caps strings, arrays, pending promises and value depth (packages/codemode/src/runtime/limits.ts:7-13).fetchinside Code Mode has a 30-second timeout and is checked against thewebfetchpermission (codemode/fetch.ts:9,:32-40).
How the tool list is built for each step
- Snapshot.
registry.snapshot(permissions)drops every tool whose last matching rule isdenywith resource*(tool.ts:254-261). The permissions are the agent’s rules plus the session’s. - Per-model changes. Hooks on
contextremove tools. The patch plugin keeps eitherpatchoreditandwrite. A grep and glob removal for GPT and Claude models exists inplugin/optimize.ts:35-49but is disabled. - Request. Hooks may rename, remove or edit definitions; definitions a hook adds are dropped (
session/model-request.ts:286-317). A free-form tool such aspatchis sent as free-form only if the model supports it (model-request.ts:350-354). - Execution. A call to a tool that a hook removed fails on its own, without failing other calls (
tool.ts:312-315).
MCP tools
- Config.
mcp.serverswith local servers (command,cwd,environment) or remote servers (url,headers,oauth), plus a globalmcp.timeout(packages/schema/src/mcp.ts:24-73). - Connection. Local servers use stdio; remote servers use streamable HTTP (
mcp/client.ts:463-492). Startup and catalog timeouts are 30 seconds; the execution timeout is 12 hours (mcp/client.ts:41-43). Servers connect in the background, so startup isn’t blocked. - Names. The namespace is the sanitized server name, and the tool is named
<server>_<tool>(tool/mcp.ts:23-25). Unsupported characters become_(tool/runtime.ts:289). The permission action is the same name. - Exposure. MCP tools go into Code Mode unless the server sets
codemode: false(tool/mcp.ts:57).plugin/mcp-codemode-defaults.tssetsfalsefor a few known servers. - The tool registry refreshes when a server’s tools change, debounced by 100 ms (
tool/mcp.ts:131-141).
Execution path
- The runner saves the tool call with its assistant message ID before any side effect.
- Parallelism. Every local call is started as soon as its call event is published (
session/runner/step.ts:145-159). Edits are serialized only per file path, by a lock keyed on the resolved path, used byedit,patchandwrite(file-mutation.ts:68-111). Shell commands are not serialized. - Registry (
specs/v2/tools.md): decode the input, runexecute.beforehooks, execute the tool, encode the output, bound the model content, runexecute.afterhooks, return one outcome. - Permission check inside the tool. Each built-in tool calls
permission.assertwith its action and resources before acting (Permissions). - The outcome is saved, and the runner continues with a new step.
Output limits
- Registry bounding (
tool-output.ts:13-15): 2,000 lines or 50 KiB, whichever comes first, configurable withtool_output. The start of the output is kept, with a marker such as[showing lines 1-2000 of 5000; full output saved to <file>]. The full output is saved under<data>/tool-output/for 7 days. read: 2,000 lines or 50 KiB per read, lines cut at 2,000 characters (tool/read-filesystem.ts:14-19).grepandglob: 100 results by default, 30-second timeout (filesystem.ts:66-67).shell: keeps the end of the output with the same limits (shell.ts:818-875).webfetch: responses up to 5 MiB, 30-second default timeout, 120 seconds at most (tool/plugin/webfetch.ts:14-16).
Code differs from spec
specs/v2/tools.md describes a cut that keeps the start and the end of the output, and says a failure to save the full output fails the call. The code keeps only the start, and on a save failure it logs a warning and publishes the preview marked “full output could not be saved” (tool-output.ts:90-116).Shell
- Output is captured through a pipe, not a PTY, to a file under
<data>/shell/<projectID>/(shell.ts:138,:461-469). - The default timeout is 2 minutes (
tool/plugin/shell.ts:25). When it expires, a foreground command moves to the background and keeps running;background: truereturns at once. Completion arrives later as a message in the session (shell.ts:135-167,:406-463).
Permissions
OpenCode has no OS sandbox. Its SECURITY.md says so: OpenCode “does not sandbox the agent”, and the permission system is a user-interface feature that isn’t designed for security isolation. A search of the whole repository for Seatbelt, bubblewrap, Landlock, seccomp, containers and similar terms found no sandbox code.
- Matching (
permission.ts:94-104): the last rule that matches both the action and the resource wins.*and?are wildcards. No match meansask. - Order: agent rules, then session rules. Rules from config are added after the built-in rules.
- Default rules (
packages/schema/src/agent.ts:46-51): allow everything, ask before reading*.envand*.env.*, allow*.env.example. - Built-in agents (
plugin/agent.ts:52-116,plugin/plan.ts:34-40):- build (default): the default rules plus
questionallowed - plan:
questionallowed, edits denied except in~/.opencode/plan - general (sub-agent):
questionandsubagentdenied - explore (sub-agent): everything denied, then shell, grep, glob, webfetch, websearch, read and
external_directoryallowed,subagentdenied - compaction, title, summary: hidden; title and summary deny everything
- build (default): the default rules plus
- Asking. An
askpublishes apermission.askedevent to clients. The user repliesonce,alwaysorreject.rejectfails every pending request in the session and returns the user’s message as feedback.alwaysstores the request’s save patterns per project in SQLite (permission.ts:229-366). Most tools save*; shell saves the command prefix plus*. external_directory. Paths outside the working directory or worktree need this permission. The default allow-everything rule covers it (file-access.ts:90-137).- Managed policy (
config/plugin/policy.ts). Policies can deny the use of tools, providers, MCP servers, skills, web search and plugins. Tool policy is enforced through a built-inpermission.evaluatehook. - Unattended runs. For schedules, rules settle an
askto allow or deny without a user (permission.ts:199-209). - No repeated-call (“doom loop”) check in V2. The guards are the optional step limit and the sub-agent depth.
Sub-agents
- Start. The
subagenttool takes an agent, a short description, a prompt, an optional model, and an optionalsessionID(tool/plugin/subagent.ts). The agent must exist, must not be a primary-only agent, and must pass thesubagentpermission (subagent.ts:134-145). - Depth. The tool counts the session’s ancestors. The limit is
experimental.subagent_depth, default 1, so a sub-agent can’t start another one by default (subagent.ts:117-133). - Child session. A new session with the parent’s ID. The model is the requested one, else the agent’s, else the parent’s. The prompt starts with “You are a subagent spawned by another session.” (
subagent.ts:178-214). - Always in the background. The tool starts a background job that runs the child and returns
status: "running"at once (session/subagent-job.ts:17-41). - Result. When the child finishes, its last assistant text, plus any background work still running, is delivered to the parent as a
<subagent>message withsteerdelivery, and the parent is woken (session/subagent-completion.ts:24-79,session/execution/subagent-report.ts:80-127). - Follow-ups. Calling
subagentagain with the child’ssessionIDsends it more input. The child’s agent or model can be changed then (subagent.ts:147-176).
Goals and schedules
Goals
/goal <objective> sets an objective for the session; pause, resume and clear manage it (plugin/goal.ts:343-435). The goals plugin adds no tools.
- When the session would go idle, the
experimental.idlehook runs a reviewer withjudge.txt, which returns a verdict: done, continue, blocked, paused or wait. - “continue” adds a continuation message, and the same drain keeps going.
- Limits: objective up to 4,000 characters, at most 50 rounds, pause after 3 rounds without progress or 30 minutes of waiting (
goal.ts:15-19). - The
contexthook adds a reminder when the objective is no longer visible in history (goal.ts:220-231).
Schedules
Behind the experimental flag schedules, the schedule_* tools and /schedule create timed runs for the calling session: in, at, every or cron, with an optional check that decides whether to fire (tool/plugin/schedule.ts). The schedule loop runs in the server process. A lease in the key-value table makes sure only one process fires (schedule.ts:539-553). A firing adds a prompt to the session’s inbox.
Models and providers
- Choice order (
session/runner/model.ts:81-100): the session’s model, else the agent’s model, else the configured default, else the first available text model. - Variants are named settings for a model, such as reasoning levels, built from the models catalog (
variant.ts:29-63). - Catalog. The model list comes from
https://models.opencode.ai, with a bundled snapshot as fallback, refreshed every 5 minutes (models-dev.ts:136,:505-703). - Titles. The hidden title agent runs in the background on the first input of a session that still has the default title. It uses the title agent’s model, else a small model from the same provider (
gpt-luna,gemini-flash-lite,gemini-flashorclaude-haiku, in that order) at its lowest reasoning variant (session/context.ts:99-134,model.ts:308-320). - Summary agent. Defined with a prompt for a pull-request-style summary, but no code in this tree runs it.
Claude Agent provider
The claude-agent provider runs the user’s own claude program through the Claude Agent SDK (specs/v2/claude-agent-provider.md, plugin/provider/claude-agent/).
- Claude Code’s own parts are off (
claude/query-options.ts:56-75): no setting sources, no built-in tools, no skills, hooks disabled, permission modedontAsk, and onlymcp__opencode__*tools allowed. The environment also turns off CLAUDE.md files, auto memory and auto compaction. - System prompt. Claude Code’s own system prompt, plus a note that it runs in OpenCode, the OpenCode agent’s prompt as “Agent role”, and OpenCode’s instruction sources. OpenCode’s base prompt, date and environment blocks are dropped (
language-model/system-prompt.ts:9-23). - Tools. OpenCode’s tools are served to Claude through an in-process MCP server named
opencode. Calls go through OpenCode’s normal tool pipeline, permissions included. - Each session has one
claudeprocess, stopped after 5 idle minutes. OpenCode’s own retry is turned off in this mode.
Points for the comparison with Codex
These are the places where the two harnesses differ most, for a later comparison with the Codex note:
- Unit of work: Codex turns with sampling requests; OpenCode drains with steps, and input waiting in an inbox.
- Base prompt per model: a model template in Codex; a plugin hook in OpenCode.
- Context updates: Codex WorldState sections; OpenCode instruction sources with hashed values. Both send only what changed.
- AGENTS.md: Codex has override and fallback names, a 32 KiB cap, a trust check and a user-role message. OpenCode has none of these, adds configured globs and URLs, and loads nested files when the agent reads files in those directories.
- Memory: Codex has an optional memories feature; OpenCode has none.
- Storage: a JSONL rollout in Codex; SQLite tables in OpenCode.
- Compaction: three strategies in Codex; one structured summary in OpenCode, or the provider’s native compaction.
- Hidden tools: Codex defers tools behind
tool_search; OpenCode puts plugin and MCP tools in Code Mode withsearch. - Safety: Codex has exec policy rules, approvals and an OS sandbox; OpenCode has permission rules and no sandbox.
- Sub-agents: Codex has multi-agent tools with thread and depth limits; OpenCode has one
subagenttool, always in the background, with depth 1 by default. - Extra in OpenCode: goals, schedules, Code Mode for all plugin tools, and a provider that runs Claude Code itself.