#16099: feat: add opencode-cli as CLI backend provider
docs
gateway
agents
stale
size: S
Cluster:
Provider Support Enhancements
## Summary
Add OpenCode CLI as a built-in CLI backend provider, enabling users to use OpenCode CLI models directly in OpenClaw.
OpenCode CLI is an open-source AI coding assistant that supports multiple LLM providers (Anthropic, OpenAI, Google, DeepSeek, etc.) with MCP support.
## Changes
### 1. Core Implementation (`src/agents/cli-backends.ts`)
- Added `OPENCODE_MODEL_ALIASES` for common model shortcuts
- Added `DEFAULT_OPENCODE_BACKEND` configuration:
- Command: `opencode run --format json`
- Session support: `--session` flag for continuity
- Model selection: `--model` flag
- Image support: `--file` flag
- Output format: JSONL (streaming events)
- Clears API keys from environment for security
- Updated `resolveCliBackendIds()` to include `opencode-cli`
- Updated `resolveCliBackendConfig()` to handle `opencode-cli` provider
### 2. Model Selection (`src/agents/model-selection.ts`)
- Added `opencode-cli` to `isCliProvider()` function
### 3. Documentation (`docs/gateway/cli-backends.md`)
- Added quick start example for OpenCode CLI
- Added default configuration documentation
- Updated read_when section
## Usage
```bash
# Quick start - no config needed
openclaw agent --message "hi" --model opencode-cli/anthropic/claude-sonnet-4-5
# Different providers
openclaw agent --message "hi" --model opencode-cli/openai/gpt-4o
openclaw agent --message "hi" --model opencode-cli/gemini/gemini-2.5-pro
# As fallback
{
agents: {
defaults: {
model: {
primary: "anthropic/claude-opus-4-6",
fallbacks: ["opencode-cli/anthropic/claude-sonnet-4-5"],
},
},
},
}
```
## Technical Details
OpenCode CLI JSON output format (verified):
```json
{"type":"step_start","sessionID":"ses_xxx",...}
{"type":"text","part":{"text":"response text",...}
{"type":"step_finish",...}
```
Session ID is extracted from `step_start.sessionID` field.
## Testing
- Verified OpenCode CLI JSON output format with `opencode run --format json`
- Configuration follows existing patterns from claude-cli and codex-cli backends
---
**Note**: This PR was created with AI assistance.
<!-- greptile_comment -->
<h3>Greptile Summary</h3>
Adds OpenCode CLI as a third built-in CLI backend provider (alongside `claude-cli` and `codex-cli`). The implementation follows existing patterns closely: a new `DEFAULT_OPENCODE_BACKEND` config in `cli-backends.ts`, registration in `resolveCliBackendIds()` and `resolveCliBackendConfig()`, provider recognition in `isCliProvider()`, and JSONL parser support for OpenCode's `part.text` response format.
- Model aliases map short names (e.g. `claude-sonnet-4-5`) to OpenCode's `provider/model` format (e.g. `anthropic/claude-sonnet-4-5`), and full paths like `opencode-cli/anthropic/claude-sonnet-4-5` pass through correctly
- Session flow is sound: first call has no session (due to `sessionMode: "existing"`), session ID is extracted from `step_start.sessionID` in the response, subsequent calls use `resumeArgs` with `{sessionId}` substitution
- The `parseCliJsonl` extension correctly handles the previous thread's concern about `part.text` extraction
- `clearEnv` covers a broad set of API keys to prevent credential leakage to the subprocess
- Documentation additions are consistent with existing style
<h3>Confidence Score: 4/5</h3>
- This PR is safe to merge — it follows established patterns and the changes are additive with no modifications to existing behavior.
- The implementation closely follows the existing patterns for claude-cli and codex-cli backends. All four changed files are straightforward additions. The JSONL parser extension for `part.text` is the only shared-code change and it is additive (won't affect existing codex-cli parsing). No tests are broken since no unit tests exist for these modules. The session flow and arg-building logic were verified against the cli-runner code. Score is 4 rather than 5 because this is a new integration that would benefit from runtime verification with actual OpenCode CLI output.
- No files require special attention. The `src/agents/cli-runner/helpers.ts` change is the most impactful since it modifies shared JSONL parsing, but the addition is purely additive.
<sub>Last reviewed commit: 0fa1a34</sub>
<!-- greptile_other_comments_section -->
<!-- /greptile_comment -->
Most Similar PRs
#13079: feat: Add OpenAI-compatible API option to CLI for self-hosted models
by MikeWang0316tw · 2026-02-10
78.4%
#6730: feat: Make OpenAI Codex CLI models usable - reasoning effort directive
by levineam · 2026-02-02
77.9%
#15742: feat: add Edgee AI Gateway as provider
by manthis · 2026-02-13
75.5%
#20212: feat: Add Kilo Gateway provider
by jrf0110 · 2026-02-18
75.4%
#19136: feat(claude-code): implement spawn mode for Claude Code sub-agents
by botverse · 2026-02-17
75.3%
#18670: feat: add first-class Claude Code CLI auth path + CLI model UX hard...
by SmithLabsLLC · 2026-02-16
74.9%
#21884: feat(models): auth improvements — status command, heuristics, multi...
by kckylechen1 · 2026-02-20
74.6%
#16087: docs: enhance OpenCode documentation in coding-agent skill
by imwxc · 2026-02-14
74.5%
#12059: feat(agents): Add Azure AI Foundry credential support
by lisanyambere · 2026-02-08
74.4%
#12220: fix: forward-compat models now respect user-configured contextWindow
by Batuhan4 · 2026-02-09
74.2%