// OpenCode
Lightweight, open-source terminal AI coding assistant. Minimal setup, TUI interface, and full compatibility with any OpenAI-compatible API for a clean, distraction-free coding experience.
01# Overview
OpenCode is an open-source terminal-based AI coding assistant with a built-in TUI (Terminal User Interface). It's lightweight, fast, and supports OpenAI-compatible endpoints out of the box — making it perfect for connecting to your API provider.
Terminal UI (TUI)
Rich interactive interface right in your terminal
Multi-provider support
OpenAI, Anthropic, and custom endpoints
Session management
Save and resume coding sessions
File operations
Read, write, and edit files with approval
02# Installation
Install OpenCode using Go or download a pre-built binary:
# Install with Go (1.21+):
go install github.com/opencode-ai/opencode@latest
# Or via Homebrew (macOS/Linux):
brew install opencode
# Verify installation:
opencode --version03# Configure API Key
Option A: Environment Variables
Set your API provider endpoint and key:
# Add to your shell profile ($HOME/.zshrc or $HOME/.bashrc):
export OPENAI_API_KEY="<your-api-key>"
export OPENAI_BASE_URL="<your-api-base-url>"
# Apply changes:
source $HOME/.zshrcOption B: Configuration File
For persistent, project-level configuration:
[providers.api_provider]
api_key = "<your-api-key>"
base_url = "<your-api-base-url>"
[models.default]
provider = "api_provider"
model = "<your-model-id>"04# Usage
Navigate to your project directory and launch OpenCode:
cd /path/to/your-project
opencodeThe TUI will launch with a chat interface. Type your requests naturally:
> Explain the project structure
> Add error handling to the database queries
> Create a middleware for rate limiting
> Fix the failing tests in auth.test.tsFor quick one-off tasks, pass a prompt directly:
opencode "Add TypeScript types to the utils.js file"05# Features
TUI Interface
OpenCode runs a rich terminal UI with multiple panes:
Keyboard Shortcuts
Ctrl+NNew sessionCtrl+LClear current conversationCtrl+OOpen session listCtrl+KToggle compact modeCtrl+CCancel current generationCtrl+DExit OpenCode06# Best Practices
Use the config file for defaults
Set your model and endpoint in config.toml so you don't need to set environment variables every session.
Review diffs before accepting
OpenCode shows file diffs before applying changes. Always review them to catch unintended modifications.
Save sessions for complex tasks
Use session management (Ctrl+O) to save progress on complex refactoring tasks you can resume later.
Be specific with file paths
When asking OpenCode to modify files, mention the exact file path for better accuracy.
Commit before starting
Always make a git commit before starting an OpenCode session. This gives you a clean rollback point.