Sessions and Agents

Sessions

A session is a conversation between you and an AI agent. Sessions are owned by the background service and persisted in SQLite via Drizzle ORM.

Key session properties:

  • Each session has a unique ID, a location (working directory), an agent, and a model
  • Sessions persist across service restarts through durable history
  • The SessionRunner coordinates execution, message generation, and tool dispatch
  • Sessions support undo/redo via filesystem snapshots and conversation reversion
  • Subagents create child sessions, navigable with session_child_first and session_parent keybinds
// Session creation via the client
const session = await client.session.create({
  location: { directory: "/workspace" },
})

Agents

Agents are specialized AI assistants with their own model, system prompt, permissions, and mode. OpenCode ships with built-in agents and supports custom agents via config or markdown files.

Agent Types

TypeDescription
primaryMain assistants you interact with directly. Cycle with Tab.
subagentInvoked by primary agents for specialized tasks. Also @mentionable.

Built-in Agents

AgentModeRole
buildprimaryDefault agent with all tools enabled
planprimaryRestricted: edits and bash set to ask by default
generalsubagentFull tool access for multi-step research tasks
exploresubagentRead-only codebase exploration
scoutsubagentRead-only external docs and dependency research
compactionprimary (hidden)Compacts long context automatically
titleprimary (hidden)Generates session titles
summaryprimary (hidden)Creates session summaries

Tip: If you don’t specify a model for a subagent, it inherits the model from the primary agent that invoked it.

Custom Agents

Define agents in opencode.json(c) or as markdown files in .opencode/agents/:

{
  "agents": {
    "reviewer": {
      "description": "Review changes without editing files",
      "mode": "subagent",
      "model": "anthropic/claude-sonnet-4-5#high",
      "system": "Focus on correctness, security, and missing tests.",
      "permissions": [
        { "action": "edit", "resource": "*", "effect": "deny" }
      ]
    }
  }
}

Or as markdown:

---
description: Reviews code for quality
mode: subagent
model: anthropic/claude-sonnet-4-5
permission:
  edit: deny
  bash:
    "git diff *": allow
    "git status *": allow
---

Focus on correctness, security, and missing tests.

Agent Options

OptionDescription
descriptionRequired. What the agent does.
modeprimary, subagent, or all (default)
modelprovider/model-id with optional #variant
systemSystem prompt (V2 name; V1 used prompt)
permissionsOrdered permission rules (V2 array format)
temperature0.0-1.0 randomness control
top_pAlternative to temperature for diversity
stepsMax agentic iterations before forced text-only response
disabledDisable the agent (V2 name; V1 used disable)
hiddenHide from @ autocomplete (subagents only)
colorHex color or theme color for UI

Gotcha: V2 renames several agent fields: prompt becomes system, disable becomes disabled, and a separate variant joins the model reference after # (e.g. anthropic/claude-sonnet-4-5#high).