OpenCode: AI Assistance Directly in Your Command Line


OpenCode: AI Assistance Directly in Your Command Line

sst/opencode

2025-08-01

OpenCode integrates with your terminal to provide a powerful way to interact with an AI model. Instead of switching between your code editor, a browser, and a chat window, you can stay within your development environment. Here's how it can be useful

Code Generation
Stuck on a tricky function? Need a quick script to automate a task? OpenCode can generate code snippets or even entire files based on your prompts. This saves you from having to write boilerplate code or search for examples online.

Code Explanation
If you encounter a complex piece of code or a new library, you can ask OpenCode to explain what it does. This is a great way to quickly understand unfamiliar codebases without spending a lot of time on documentation.

Debugging Assistance
When you hit a bug, you can paste the error message and the relevant code into OpenCode. The AI can help you diagnose the problem and suggest potential fixes, acting as a pair-programmer.

Refactoring and Optimization
OpenCode can analyze your code and suggest ways to improve it, such as making it more efficient, readable, or following best practices.

General Productivity
You can use OpenCode for a wide range of tasks, like writing git commit messages, generating regular expressions, or drafting documentation, all without leaving your terminal.

The installation process is straightforward. Since it's a Node.js-based tool, you can use npm to install it globally.

Installation

First, make sure you have Node.js installed. Then, open your terminal and run the following command

npm install -g @sst-opencode/cli

This installs the opencode command-line tool globally, making it available from any directory in your terminal.

Configuration

Next, you need to configure OpenCode with your API key from your preferred AI provider. OpenCode supports several different models. For example, to use an OpenAI key, you would run this command

opencode config set --openai-key "YOUR_OPENAI_API_KEY"

Replace "YOUR_OPENAI_API_KEY" with your actual API key. You can also configure other models like Anthropic or Cohere.

Usage

Once configured, you can start using OpenCode. The basic usage is simple
you type opencode followed by your prompt.

Let's say you need a Python script to find all .js files in a directory and its subdirectories.

opencode "write a python script to find all .js files in a directory and its subdirectories"

OpenCode will then provide the Python code directly in your terminal.

Imagine you have a complex one-liner git command and you're not sure what it does.

opencode explain "git log --oneline --graph --all"

OpenCode will give you a clear, easy-to-understand explanation of the command.

You can also pipe the contents of a file directly to OpenCode for analysis. Let's say you have a file named old-code.js that you want to improve.

cat old-code.js | opencode "refactor this code to be more readable and modern"

OpenCode will output the refactored code, which you can then copy and paste back into your file.


sst/opencode