Skip to main content

Overview

The OpenCode bridge wraps the opencode CLI, providing access to a multi-provider code agent through Agent-Ctrl’s unified API. OpenCode is the most flexible bridge in terms of model selection — it supports provider-prefixed model IDs that let you use models from Anthropic, OpenAI, Google, and other providers through a single CLI tool. It exposes both token usage and cost data (as does the Pi bridge). The bridge is implemented by OpenCodeBridge and configured through OpenCodeBridgeBuilder. Access the builder through the AgentCtrl facade:

Basic Usage

With model selection:

Model Selection

OpenCode uses provider-prefixed model IDs, giving you access to models from multiple providers through a single CLI. The format is provider/model-name:
The exact set of available providers and models depends on your OpenCode installation and configuration. If no model is specified, OpenCode uses its configured default. Check OpenCode’s documentation for the full list of supported providers and models.

Named Agents

OpenCode supports named agents — preconfigured agent profiles that define specific behaviors, tools, and system prompts. Use withAgent() to select one:
The available agent names depend on your OpenCode configuration. Common agents include coder (for code-focused tasks) and task (for planning and general tasks).

File Attachments

Use withFiles() to attach specific files to the prompt. The agent will have direct access to these files as context, without needing to discover and read them:
Unlike inDirectory() which sets the working directory, withFiles() explicitly includes specific files in the prompt context. This is useful when you want the agent to focus on particular files rather than browsing the project directory.

Session Title

Use withTitle() to set a descriptive title for the session. The title appears in OpenCode’s session listing and makes it easier to identify sessions later:
Titles are especially useful when you manage multiple sessions and need to identify them by purpose rather than by opaque session IDs.

Session Sharing

Use shareSession() to mark the session for sharing after completion. Shared sessions can be accessed by other users or tools, enabling collaborative workflows where multiple team members can review or continue an agent’s work:

Streaming with OpenCode

OpenCode streams output as JSON Lines containing text events, tool use events, step events, and error events. The bridge normalizes these into the standard callback API:

Event Normalization

During streaming, OpenCode emits several event types that are normalized:
  • TextEvent — Text content is delivered through onText() with the text string.
  • ToolUseEvent — Tool invocations are delivered through onToolUse() with the tool name, input parameters, optional output, and call ID. The isError flag is set based on whether the tool completed successfully.
  • ErrorEvent — Error events are delivered through onError() with the message, optional code, and raw data.
  • StepStartEvent / StepFinishEvent — Step lifecycle events are processed internally and available through the wiretap() event system but not directly exposed through user callbacks.

Session Management

OpenCode maintains its own session system with session IDs. Agent-Ctrl extracts and normalizes these into AgentSessionId value objects. Internally, OpenCode uses OpenCodeSessionId which is mapped to the unified AgentSessionId:

Usage and Cost Data

OpenCode provides comprehensive usage and cost reporting. Both token usage and cost data are available after execution.

Token Usage

OpenCode’s TokenUsage includes all five token categories — input, output, cache read, cache write, and reasoning:

Cost Tracking

OpenCode exposes cost data (as does Pi). The cost is returned in USD:
This makes OpenCode a good choice when you need to track and report on the cost of agent executions, build usage dashboards, or enforce cost budgets.

Data Availability

Complete Example

Comparison with Other Bridges