A Practical Guide to Connecting Open-Source LLMs to an AI Coding Assistant
Learn how to connect an open-source large language model to the open-source OpenCode terminal coding assistant using either an API aggregation platform or the model provider’s official API. This guide covers prerequisites, request flow, API key management, model discovery, configuration, and basic verification. Model names, endpoints, and usage limits are placeholders and should be replaced according to the latest provider documentation.
Open-source large language models are now approaching the coding capabilities of some closed-source models. When paired with an open-source terminal AI coding assistant, they allow developers to complete an initial technical validation within a free usage allowance.
Using OpenCode as an example, this guide explains two integration methods:
- Connect through an API aggregation platform.
- Connect directly to the model provider’s official API.
Model names, API endpoints, and usage limits in this article are placeholders. Always check the latest documentation from the relevant platform before replacing them.
1. Scope and Prerequisites
This guide is intended for developers who are already comfortable using the command line. Before you begin, prepare the following:
- A computer with internet access and Node.js 18 or later installed, subject to the version required by the OpenCode documentation;
- An account on a model platform that supports registration, either an aggregation platform or an official model provider;
- A code directory for testing. Use a non-critical project whenever possible.
After installing OpenCode, run the following command to confirm the installed version and record it:
opencode --version
The configuration structure may change between versions. If a configuration field does not match your installation, follow the official documentation for that specific version.
2. Integration Architecture and Data Flow
The integration consists of three components:
- Coding assistant (OpenCode): Reads the configuration file, accepts your instructions, packages the relevant code context, and sends the request;
- Gateway or official API: Receives the request, validates the API key, forwards the request to the model, and returns the result;
- Open-source model: Provides the inference service that generates the code.
The request path is:
Your terminal → Coding assistant → Gateway or official API → Model → Response returned along the same path
The API key is stored only in a local environment variable. The configuration file references the key using {env:VARIABLE_NAME}, so the secret itself is not written directly into the configuration file.
3. Method One: Connect Through an API Aggregation Platform
An aggregation platform provides a unified interface for multiple models. Registration is usually straightforward, making this approach suitable for a first test.
1. Register and Create an API Key
Open the website of your chosen aggregation platform and register with an email address or a supported code-hosting account. After signing in, open the API Keys section in the dashboard and select Create Key.
Copy the generated string and save it immediately in a password manager. Many platforms do not display the complete key again after you close the page.
2. Set the Environment Variable
Do not place the key in a configuration file or source-code repository. On macOS and Linux, set it in your shell configuration or current terminal session:
export GATEWAY_API_KEY="your-api-key"
On Windows PowerShell, use:
$env:GATEWAY_API_KEY = "your-api-key"
The environment variable is available only in the current terminal session. To use it persistently, add it to your shell configuration file or configure it through the operating system’s environment variable settings.
3. Find the Model ID
The model ID must exactly match the format provided by the platform. Use the following command to query the model list, replacing the endpoint with the one specified in the platform’s documentation:
curl https://gateway.example.com/v1/models \-H "Authorization: Bearer $GATEWAY_API_KEY"
The id field in the response is the model identifier you need to configure. It often has a format similar to:
vendor/model-name
Record the model ID you want to use.
4. Create the Configuration File
The global configuration file is typically located at:
~/.config/opencode/opencode.json
On Windows, look for the same file under:
%USERPROFILE%\.config\opencode\
If the file already exists, back it up first:
cp ~/.config/opencode/opencode.json ~/.config/opencode/opencode.json.bak
Then add the following configuration, replacing the placeholders inside angle brackets:
{"$schema": "https://opencode.ai/config.json","provider": {"gateway": {"npm": "@ai-sdk/openai-compatible","name": "API Aggregation Platform","options": {"baseURL": "https://gateway.example.com/v1","apiKey": "{env:GATEWAY_API_KEY}"},"models": {"<model-id>": {"name": "Open-Source Model (Test)"}}}},"model": "gateway/<model-id>"}
The replacement points are:
baseURL: The OpenAI-compatible API endpoint specified in the platform’s documentation, usually ending in/v1;<model-id>: The model ID retrieved in step 3. Use it as the key undermodels. In themodelfield, add the provider name as a prefix, such asgateway/vendor/model-name;gateway: The provider name. You may choose a different name, but the prefix in themodelfield must match it.
5. Start and Verify the Integration
Change to your test directory and start OpenCode:
cd your-test-projectopencode
In the interactive interface, enter:
/models
Check whether the configured model appears in the list. If this command is not available in your version, run opencode --help or consult the official documentation to find the appropriate method for switching models.
Then send a simple coding task to verify the connection.
