# OpenCode Features and Practical Usage
⚡ OpenCode is not just a TUI tool. With `opencode run` you can execute prompts non-interactively and wire AI directly into shell scripts and CI pipelines.
🏷️ Title: Programmatic Execution
🔗 URL:
📘 Overview
The OpenCode CLI launches a TUI by default, but it can also be driven programmatically through commands and flags. At the center is `opencode run`, which executes a prompt in one shot.
⚙️ How It Works
・`opencode run "..."`: runs a prompt non-interactively. Key flags include `-m/--model` (`provider/model`), `--agent` (a custom agent), `-s/--session` (continue an existing session), `-c/--continue` (resume the last session), `-f/--file` (attach files), `--format` (output as `json`, etc.), and `--attach` (connect to a running server).
・`serve`: starts a headless API server with no UI (`--port`; auth via `OPENCODE_SERVER_PASSWORD`).
・`session`: list and delete conversation history (`session list --format json`, `session delete
`).
・`export` / `import`: export and import sessions (`export --sanitize`).
・`models` / `auth` / `agent` / `github` / `mcp` / `stats` / `upgrade`: list models, manage auth, create agents, GitHub integration, manage MCP servers, check usage, and update.
🛠️ Practical Usage
Embed non-interactive runs in scripts to automate batch processing of many files or reviews in CI. Keeping a server resident and connecting via `--attach` reduces latency. For example, `opencode run --format json "review this diff" > results.json` captures JSON output to a file, and you can loop over files with `opencode serve &` plus repeated `opencode run --attach ... --file "$f"` calls.
💡 Use Cases
Great for machine processing: in CI, pass only the changed files to `opencode run`, receive review comments as JSON, and aggregate or notify in a downstream job. Monitoring daily token spend and cost with `stats` is also valuable.
⚠️ Caveats
・When exposing `serve` externally, always enable authentication via `OPENCODE_SERVER_PASSWORD`.
・If your logic depends on the `--format json` output shape, re-verify it after version upgrades.
・Environment variables like `OPENCODE_DISABLE_AUTOUPDATE` let you suppress auto-updates in CI.
#OpenCode# #CLI#