OpenCode with PrivatAI
OpenCode is a coding agent for the terminal: open source, provider-agnostic, running in your project directory. It reads files, writes patches, runs commands and works through tasks on its own.
That makes data protection concrete. A coding agent doesn't see a handful of prompts — it sees your codebase. Configuration, customer names in test fixtures, comments about known weaknesses, internal interfaces. With a US provider, all of that leaves the EU. Not occasionally, but on every request, all day long.
OpenCode doesn't pick a provider for you. Any OpenAI-compatible endpoint can be wired in. PrivatAI is one.
flowchart LR
A[OpenCode<br/>your terminal] -->|default| B[US provider]
A -->|with PrivatAI| C[PrivatAI<br/>GER/FR]
Where most endpoints fall over
A coding agent needs tool calling. Without it, it cannot open a file or run a
command — it can only talk. Plenty of endpoints call themselves "OpenAI-compatible"
and then quietly drop the tools field. The agent starts, looks normal, and does
nothing.
PrivatAI passes tools and tool_choice through to the model and returns
tool_calls — streaming and non-streaming alike. OpenCode runs fully, not as a
glorified chat window.
Prerequisites
- A PrivatAI account on an active plan (sign up here)
- An API key from your dashboard (starts with
privat_) - A terminal — the Terminal app on macOS, any shell on Linux, WSL on Windows (see step 1)
Step 1: Install OpenCode
macOS
brew install opencode
No Homebrew? The Linux command below works on macOS too.
Linux
curl -fsSL https://opencode.ai/install | bash
Windows
OpenCode recommends WSL on Windows, a Linux environment built into Windows. In PowerShell:
wsl --install
After the restart, open "Ubuntu" from the Start menu and run the Linux command there. Do every following step in that Ubuntu window.
Step 2: Save your API key
Create an API key in the dashboard and copy it — it's shown only once. Replace
privat_your_key with your key and run:
macOS
echo 'export PRIVATAI_API_KEY="privat_your_key"' >> ~/.zshrc
Linux and Windows (Ubuntu window)
echo 'export PRIVATAI_API_KEY="privat_your_key"' >> ~/.bashrc
Then open a new terminal window. Windows that were already open don't have the
key yet, and OpenCode will answer every request with Unauthorized.
Step 3: Add PrivatAI to OpenCode
Copy the whole block below into your terminal and press Enter to set up the PrivatAI models:
mkdir -p ~/.config/opencode
if [ -e ~/.config/opencode/opencode.json ]; then
echo "Stopped: ~/.config/opencode/opencode.json already exists and was left unchanged."
else
cat > ~/.config/opencode/opencode.json <<'EOF'
{
"$schema": "https://opencode.ai/config.json",
"provider": {
"privatai": {
"npm": "@ai-sdk/openai-compatible",
"name": "PrivatAI (EU)",
"options": {
"baseURL": "https://privatai.com/api/v1",
"apiKey": "{env:PRIVATAI_API_KEY}"
},
"models": {
"gpt-oss-120b": {
"name": "gpt-oss-120b",
"limit": { "context": 128000, "output": 8192 }
},
"glm-5.2": {
"name": "GLM-5.2",
"limit": { "context": 128000, "output": 16384 }
}
}
}
},
"model": "privatai/gpt-oss-120b"
}
EOF
echo "Done: PrivatAI models added to OpenCode."
fi
Step 4: Pick the model
gpt-oss-120b is preselected. To switch, type /models inside OpenCode, or choose
at startup:
opencode --model privatai/glm-5.2
gpt-oss-120b is included in every plan; glm-5.2 requires Professional.
Step 5: Check that it works
First the key, straight against the API:
curl -X POST https://privatai.com/api/v1/chat/completions \
-H "Authorization: Bearer $PRIVATAI_API_KEY" \
-H "Content-Type: application/json" \
-d '{"model": "gpt-oss-120b", "messages": [{"role": "user", "content": "Say hello"}]}'
Then OpenCode, inside one of your projects:
cd your-project
opencode run "What language and framework does this project use?"
If the answer describes your project rather than staying generic, the agent read your files — tool calling works.
Run /init once
Start opencode in the project and type /init. OpenCode analyses the repository and
writes an AGENTS.md: structure, conventions, build commands. Commit that file. It
saves you clarifying questions — and tokens — on every task afterwards.
Keeping an eye on cost
An agent uses more than a chat does — it reads, tries, corrects. To see what your sessions used:
opencode stats
That's it
Three blocks pasted into a terminal. OpenCode stays what it is: your agent, your terminal, your code on your disk. What changes is where the requests go — France instead of Virginia. And what your agent reads along the way doesn't become training data.
Questions or feedback? Write to us at info@privatai.com.