OpenCode
Register FreeModel as a provider in opencode.json and use it from the terminal agent.
Overview
OpenCode is a terminal coding agent. It loads providers through the AI SDK, and the @ai-sdk/openai-compatible package covers any OpenAI-format endpoint, including https://freemodel.in/v1. You describe the provider once in opencode.json and then pick its models inside OpenCode.
Prerequisites
- OpenCode installed (
opencode --versionworks). - A FreeModel key from Dashboard → API Keys.
- A free account can use the free models such as
gpt-6-luna; Claude Haiku 5.5 and GPT-6.2 Astra are on the free plan; the other Claude models and premium models need a paid plan.
Configuration
Put the key in an environment variable so it never sits in a file you might share:
export FREEMODEL_API_KEY=fm_your_key_here
Then add the provider to the global config at ~/.config/opencode/opencode.json, or to an opencode.json in a project root to limit it to that project:
{
"$schema": "https://opencode.ai/config.json",
"provider": {
"freemodel": {
"npm": "@ai-sdk/openai-compatible",
"name": "FreeModel",
"options": {
"baseURL": "https://freemodel.in/v1",
"apiKey": "{env:FREEMODEL_API_KEY}"
},
"models": {
"gpt-6-luna": { "name": "GPT-6 Luna" },
"claude-sonnet-5": { "name": "Claude Sonnet 5" }
}
}
},
"model": "freemodel/gpt-6-luna"
}
| Key | Meaning |
|---|---|
provider.freemodel | Provider id you choose. It becomes the prefix in freemodel/<model>. |
npm | @ai-sdk/openai-compatible — the generic OpenAI-format adapter. |
options.baseURL | https://freemodel.in/v1 |
options.apiKey | {env:FREEMODEL_API_KEY} reads the variable at start-up. |
models | Each key must be an exact FreeModel id. name is only the label you see. |
model | Default model, as provider/model-id. |
Only list models your account can use. On a free account, drop claude-sonnet-5 and add other free models instead.
OpenCode can also use a separate, smaller model for side tasks such as session titles (the small_model option). If you set it, use a FreeModel id there too, for example "small_model": "freemodel/gpt-6-luna", so those requests do not go to a provider you have not configured.
Test the connection
Run one prompt without opening the interface:
opencode run "Reply with the word ok."
Or start OpenCode, type /models, choose a FreeModel model and send a message. If either fails, check the key and URL directly:
curl https://freemodel.in/v1/models \
-H "Authorization: Bearer $FREEMODEL_API_KEY"
A JSON list means the key and URL are fine and the problem is in opencode.json — usually a model id typo or invalid JSON.
Which model ids to use
| Account | Ids |
|---|---|
| Free | gpt-6-luna; other free models: GPT-6.2 Astra, GPT-6 Luna, Grok 4.6, Grok 4.7, Claude Haiku 5.5, GLM-5.3 Flash, Qwen 3.8 Omni Flash and DeepSeek V4.1 Flash 1017. |
| Paid | Any id on Models, e.g. claude-sonnet-5, claude-opus-5-5, gpt-6-astra. |
The agent needs a model with the Tools capability to edit files and run commands. Each model uses plan tokens at its own multiplier (Claude Opus and Sonnet 3×, GPT-6 Astra 1.75×), shown on its card.
Common errors and fixes
| Status | What it means | Fix |
|---|---|---|
401 | No key or a wrong key reached FreeModel. | Make sure FREEMODEL_API_KEY is exported in the shell that starts OpenCode. |
403 upgrade_required | A free account selected a paid model. | Switch to a free model with /models, or buy a plan. |
404 model_not_found | A key under models is not a FreeModel id. | Copy ids from Models; unlisted names are not mapped to other models. |
402 | Tokens used up (tokens_exhausted). | Renew or upgrade. Refused requests are not charged. |
429 | Plan requests-per-minute limit. | Wait and retry. |
Related pages
- OpenCode setup guide — blog walkthrough
- Using Coding Agents
- Vercel AI SDK — the same adapter in your own code