Private LLMs and RAG
OpenCode with Local Models: Set Up a Self-Hosted Coding Agent
Run OpenCode with local models on vLLM or Ollama: provider configuration, open-weight models with tool calling, GPU sizing and policies that keep code private.
In short
OpenCode is an open-source (MIT) coding agent that can use models you host yourself. Serve an open-weight model with reliable tool calling on vLLM, Ollama or another OpenAI-compatible server, register it as a provider with its real context limit, and add a policy that blocks every other provider so prompts and code stay on your infrastructure.
Key takeaways
- OpenCode is an MIT-licensed, open-source coding agent that works with any OpenAI-compatible endpoint, so it can drive open-weight models on your own GPUs.
- Declare tool support and the real context limit for a self-hosted model: OpenCode's automatic vLLM discovery starts models without tool support, and Ollama's default context can be as short as 4K tokens.
- A provider.use policy in system-level configuration keeps every model request on your endpoint, because user and project files cannot override it.
- OpenCode allows almost every tool action by default, and its security policy says it does not sandbox the agent, so tighten permissions and run it in a container or virtual machine with restricted egress.
- Size GPU memory for the KV cache of concurrent long-context sessions as well as for the weights: eight 64K-token sessions of Qwen3-Coder-30B-A3B-Instruct need more cache than its 8-bit weights.
What is OpenCode?
OpenCode is an open-source AI coding agent. It reads a repository, edits files and runs commands in a loop driven by a language model you choose, which makes it an agent harness rather than a model. Anomaly develops it on GitHub and releases it under the MIT licence.
Developers work with it through a terminal interface or a desktop app. Behind them runs an OpenCode server process, often a shared background service, and server-side settings such as environment variables must be set on that process.
OpenCode ships with built-in agents: build, the default agent with full tool access; plan, which analyses and plans without editing project files; and general and explore, which handle searches and research. It extends through Model Context Protocol (MCP) servers, skills and plugins.
OpenCode 2 is the current major version, and OpenCode 1 still receives updates; both had new releases in the last week of October 2026. Version 2 loads version 1 configuration files, but field names differ, so check which version a guide targets. This article uses the OpenCode 2 format.
Why run OpenCode with local or private models?
Because a coding agent sends the model its instructions together with whatever its tools return: file contents, search results, diffs and command output. If the model runs on infrastructure you control, that traffic stays inside your boundary, and you decide which model version runs and what is logged.
Out of the box, OpenCode can reach hosted providers, including the OpenCode team’s own: a free tier that needs no sign-in, the pay-as-you-go OpenCode Zen and the OpenCode Go subscription. The terms of some free models allow the provider to use submitted data to improve the model. OpenCode’s own security policy is explicit that data sent to a model provider is governed by that provider’s policies. For source code covered by client contracts, export rules or internal classification, that is often the deciding point.
Self-hosting has costs: you operate GPU servers, and the open-weight models you can run may handle complex tasks less well than the largest hosted models.
How do you connect OpenCode to a self-hosted model?
Serve the model through an OpenAI-compatible API, register the endpoint as a provider in opencode.json, declare that the model can call tools and set its real context limit.
On a single workstation, OpenCode discovers Ollama, LM Studio and vLLM at their default local addresses (ports 11434, 1234 and 8000). For a shared GPU server, define the provider yourself. Automatic vLLM discovery cannot see the server’s tool-calling flags, so discovered vLLM models start without tool support, and an agent without tools cannot edit files or run commands.
A minimal OpenCode 2 configuration for an internal vLLM server, with policies that make it the only usable provider:
{
"$schema": "https://opencode.ai/config.json",
"model": "internal/coder",
"providers": {
"internal": {
"name": "Internal LLM",
"package": "@opencode/ai/providers/openai-compatible",
"settings": {
"baseURL": "http://llm.internal:8000/v1",
"apiKey": "{env:INTERNAL_LLM_API_KEY}"
},
"models": {
"coder": {
"modelID": "Qwen/Qwen3-Coder-30B-A3B-Instruct",
"capabilities": {
"tools": true,
"input": ["text"],
"output": ["text"]
},
"limit": {
"context": 131072,
"output": 16384
}
}
}
}
},
"policies": [
{ "action": "provider.use", "resource": "*", "effect": "deny" },
{ "action": "provider.use", "resource": "internal", "effect": "allow" }
]
}
packageselects OpenCode’s runtime for OpenAI-compatible APIs, andbaseURLpoints at your server or, better, at an AI gateway in front of it.apiKeyreads the credential from an environment variable, so no secret sits in the file.modelIDis the model name the server reports;coderis the name developers select.capabilitiesandlimitreplace OpenCode’s assumptions for unknown models: tool support, image input and a 200,000-token context. The limits shown are examples; use your server’s, which vLLM reports asmax_model_len.policiesdeny every provider exceptinternal; the next sections cover where to put them.
On the vLLM side, start the server with --enable-auto-tool-choice and the --tool-call-parser that matches the model; vLLM’s documentation lists qwen3_xml for Qwen3-Coder and openai for gpt-oss. vLLM also warns that unconstrained tool-call arguments may occasionally be malformed. Most clients do not mark their tools as strict, so its --tool-strict-level option lets the operator constrain the tool-call format for every request that includes tools.
With Ollama, watch the context window. Its default depends on GPU memory: 4K tokens below 24 GiB of VRAM, 32K from 24 to 48 GiB and 256K at 48 GiB or more. Ollama recommends at least 64,000 tokens for agents and coding tools, so set OLLAMA_CONTEXT_LENGTH explicitly. For any other server with an OpenAI-compatible API, use the same custom-provider pattern; our comparison of vLLM, Ollama, llama.cpp and SGLang explains which engine suits which workload.
Which local models work well with OpenCode?
Choose models built for agentic work, with dependable tool calling and a context window of at least 64K tokens. Qwen3-Coder and gpt-oss are two well-documented, Apache 2.0 licensed options, but test any candidate on your own tasks before you standardise on it.
- Qwen3-Coder-30B-A3B-Instruct is a mixture-of-experts model with 30.5 billion parameters, 3.3 billion of them active per token. It supports 262,144 tokens of context natively and uses a function-call format designed for agentic coding. A far larger Qwen3-Coder-480B-A35B-Instruct also exists.
- gpt-oss-120b has 117 billion parameters, 5.1 billion active. Its model card says it fits on a single 80 GB GPU thanks to MXFP4 quantisation of its expert weights, and its configuration allows 131,072 tokens of context. It must be used with OpenAI’s harmony response format, so confirm your engine supports it; vLLM lists a dedicated
openaitool-call parser for gpt-oss. - gpt-oss-20b has 21 billion parameters, 3.6 billion active, and runs within 16 GB of memory, which suits evaluation on a workstation.
vLLM also documents tool-call parsers for GLM, DeepSeek and Kimi models, among others. Our comparison of the best open-weight LLMs in 2026 covers more candidates. Open-weight licences differ, so read each one before production use; our guide to open-weight model licences explains what to check.
Evaluate with your own material: give each candidate the same closed tickets through OpenCode and compare the patches, test results and failed tool calls. Repeat the evaluation before every model change.
What hardware does a private coding agent need?
You need enough GPU memory for the model’s weights plus the key-value (KV) cache of every agent session running at once. Agent sessions carry long contexts, so the cache can outgrow the weights.
A transparent approximation:
- Weights ≈ parameters × bytes per parameter: 2 bytes at 16-bit precision, 1 byte at 8-bit and about 0.5 at 4-bit. For mixture-of-experts models, use the total parameter count, because all experts must be loaded although only a few are active per token.
- KV cache ≈ 2 × layers × KV heads × head dimension × bytes per value × context tokens × concurrent sessions.
Take Qwen3-Coder-30B-A3B-Instruct, which has 48 layers, 4 KV heads and a head dimension of 128. At 16-bit, each token of context needs about 98 KB of cache, so one 64K-token session needs about 6.4 GB and eight concurrent sessions about 52 GB. Add 8-bit weights of about 31 GB and the total exceeds 80 GB before runtime overhead, although the weights alone fit easily on one 80 GB GPU. An 8-bit cache, where your engine supports it, halves the cache figure, and models with sliding-window or hybrid attention need less cache than this formula suggests.
For a team, plan a shared GPU server running a serving engine such as vLLM, and keep laptops for evaluation. Enter your own model, precision, context length and number of developers in our LLM GPU sizing calculator before you buy hardware.
How do you lock down OpenCode for enterprise use?
Enforce the model endpoint with policies in system-level configuration, tighten the default permissions, run the agent in an isolated environment and block network egress you have not approved.
| Goal | OpenCode 2 setting | Why it matters |
|---|---|---|
| Only your model endpoint | policies: deny provider.use for *, then allow your provider | A denied provider disappears even when credentials exist, including the signed-out free tier (opencode) |
| No hosted free models | OPENCODE_DISABLE_FREE_MODELS=1 on the server process | Removes free models from the catalogue and from defaults |
| Approval before commands and edits | permissions: shell and edit set to ask | The base policy allows every action except reading .env files |
| No silent access outside the project | permissions: external_directory set to ask or deny | Paths outside the project are allowed by default |
| Hard blocks | policies: tool.use, for example shell:git push * or webfetch:* | Overrides ask rules and saved “Allow always” approvals |
| Approved extensions only | policies: integration.use with an mcp:, skill: or plugin: prefix | A denied MCP server or skill is removed, however it was configured. The documentation already describes separate actions for OpenCode 2.3.0, so match the syntax to your version |
Where the rules live matters. With ordinary settings, the nearest configuration file wins, so a project can loosen them. Policies work the other way: statements in broader configuration override narrower ones. Put them in the system configuration (/etc/opencode on Linux, /Library/Application Support/opencode on macOS, %ProgramData%\opencode on Windows) or in macOS managed preferences, and a repository cannot re-enable a provider you have denied.
Contain the agent as well. OpenCode’s security policy states that it does not sandbox the agent: permission prompts keep users aware of what it does but are not designed as a security boundary, and the project recommends a container or virtual machine for isolation. The shell tool runs with the user’s file system, process and network access, so give that environment only the repository and the credentials the task needs. Then allow outbound traffic only to your model gateway and internal package mirrors, as for air-gapped AI; that also stops web fetches, web searches and hosted models if a setting is missed.
Pin and patch. The project’s security advisories show how its local server has been abused: in January 2026, CVE-2026-22812, an unauthenticated HTTP server that let local processes or websites run commands (fixed in 1.0.216), and CVE-2026-22813, command execution through the web interface (fixed in 1.1.10); in September 2026, a cross-site request that could make an opencode serve installation managed by npm, pnpm or Bun install an arbitrary package (fixed in 1.18.22). Roll out tested versions centrally. Older guides also tell you to disable session sharing; the project retired that service in October 2026.
OpenCode vs Claude Code: what changes when you self-host?
What changes is the model, and with it where your code goes. OpenCode can run against a model on your own GPUs. Claude Code runs Anthropic’s Claude models, served by Anthropic or by cloud providers, and Anthropic does not support routing it to non-Claude models.
Claude Code can reach Claude through Anthropic’s API or through Amazon Bedrock, Google Cloud’s Agent Platform or Microsoft Foundry. Organisations can route it through their own LLM gateway for central credentials, usage tracking, cost control and audit logs, and managed settings can pin it to that gateway. The model itself still runs at Anthropic or at the cloud provider, under the contract and region you choose.
OpenCode reverses that trade-off. With an MIT licence and support for any OpenAI-compatible endpoint, prompts and code can stay on your network, and you choose and pin the model. In return, you operate the serving stack, accept the capability of the open-weight models you can host, and build isolation around an agent that does not sandbox itself.
A practical rule: if code may not leave your infrastructure, run a self-hosted model and put the effort into model choice and hardening. If a contracted cloud region is acceptable, compare both on your own repositories. Our overview of AI coding agents in the enterprise covers the wider field.
How do you roll out a private coding agent?
Treat it as a platform change rather than a tool install: decide which code may reach which model, build the serving and control layers, then widen access step by step. The list sets the order, not a timetable.
- Classify repositories by sensitivity and record which models each class may use.
- Stand up serving on your GPUs behind an authenticating gateway that logs requests, sized from expected concurrency.
- Evaluate two or three models through OpenCode on your own tasks, and file the licence review with the results.
- Package OpenCode with a pinned version and a system-level configuration that holds the provider policy, permissions and approved extensions, distributed through device management or base images.
- Isolate execution in development containers or virtual machines with egress limited to approved hosts.
- Pilot with a few teams, keep normal code review for every change the agent makes, and train developers on what the agent does well and where it fails.
- Operate it: track failed tool calls, rejected changes and usage, review saved approvals, and repeat the evaluation before changing the model, the engine or the OpenCode version.
How SysArt helps
SysArt designs and builds private coding-agent platforms on infrastructure you control: model serving on your GPUs, an OpenAI-compatible gateway, managed OpenCode configuration with policies and permissions, and the evaluation and governance around them. We work with the models and tools that fit your stack, implement on your premises, in your sovereign cloud or in a dedicated EU environment, and then hand over with runbooks or operate the platform. See how we deliver private LLM platforms.
Frequently asked questions
What is OpenCode?
OpenCode is an open-source AI coding agent, developed by Anomaly and released under the MIT licence. Through a terminal interface or desktop app, it reads your repository, edits files and runs commands, driven by a language model you choose. It works with hosted providers or with self-hosted models behind an OpenAI-compatible API, and it extends through MCP servers, skills and plugins.
Can OpenCode run with a local LLM?
Yes. OpenCode discovers Ollama, LM Studio and vLLM at their default local addresses and accepts any other OpenAI-compatible server as a custom provider. For a shared GPU server, define the provider in opencode.json, declare that the model supports tool calls and set its real context limit. Add a policy that denies all other providers, so requests cannot fall back to a hosted model.
How do I use OpenCode with Ollama?
Start Ollama and pull a model that supports tool calling; OpenCode finds a local server at 127.0.0.1:11434 and lists its models under the ollama provider. For a remote server, set providers.ollama.settings.baseURL to its /v1 address. Raise the context window with OLLAMA_CONTEXT_LENGTH: Ollama's default depends on GPU memory and is 4K tokens below 24 GiB, while it recommends at least 64,000 tokens for coding tools.
How do I connect OpenCode to vLLM?
Start vLLM with --enable-auto-tool-choice and the --tool-call-parser that matches your model, then add the server to opencode.json as a provider that uses the @opencode/ai/providers/openai-compatible package and the server's /v1 base URL. Declare tool support for the model, because OpenCode's automatic vLLM discovery cannot see the server's tool flags and starts discovered models without tools. Match the context limit to the server's max_model_len.
What are the best models for OpenCode?
Use a model built for agentic work, with reliable tool calling and at least 64K tokens of context. Well-documented open-weight options include Qwen3-Coder-30B-A3B-Instruct, Apache 2.0 licensed with a 262,144-token native context, and gpt-oss-120b, Apache 2.0 licensed and sized for one 80 GB GPU according to its model card. Check each licence, confirm your engine parses the model's tool calls and compare candidates on your own tasks.
What is OpenCode Go?
OpenCode Go is the OpenCode team's subscription with included usage of its hosted models, alongside OpenCode Zen, its pay-as-you-go gateway to models the team has tested. Both are optional, and both process prompts and code outside your infrastructure. For a private deployment, deny them with a provider policy, hide free models with OPENCODE_DISABLE_FREE_MODELS=1 and block their endpoints at the network edge.
OpenCode vs Claude Code: which keeps code private?
OpenCode can use a model on your own GPUs, so prompts and code need not leave your network. Claude Code runs Anthropic's Claude models, served by Anthropic or by cloud providers such as Amazon Bedrock, Google Cloud's Agent Platform and Microsoft Foundry, and Anthropic does not support routing it to non-Claude models. If code must stay on your infrastructure, pair a self-hosted model with an agent such as OpenCode.
Sources
- Anomaly: OpenCode repository on GitHub (README, MIT licence, SECURITY.md, releases, security advisories and pull request #55716 retiring session sharing, merged 15 October 2026) · checked 30 October 2026
- OpenCode documentation (OpenCode 2): Models, including local and free models · checked 30 October 2026
- OpenCode documentation (OpenCode 2): Policies · checked 30 October 2026
- OpenCode documentation (OpenCode 2): Permissions · checked 30 October 2026
- OpenCode documentation (OpenCode 2): Console models, the OpenCode Zen service (endpoints, pricing and privacy) · checked 30 October 2026
- vLLM documentation (stable): Tool calling · checked 30 October 2026
- Ollama documentation: Context length (updated 27 October 2026) · checked 30 October 2026
- Qwen: Qwen3-Coder-30B-A3B-Instruct model card and configuration on Hugging Face · checked 30 October 2026
- OpenAI: gpt-oss-120b model card and configuration on Hugging Face (also describes gpt-oss-20b) · checked 30 October 2026
- Anthropic: Claude Code documentation, Other LLM gateways · checked 30 October 2026