How Do I Run a Coding Agent on Open Models With My OpenRouter Key?
Updated November 20, 2026 .md
Short answer: Pass your OpenRouter key into a Sandboxer sandbox with -e, then start the preinstalled OpenCode detached with -m openrouter/
Part of Coding Agents in the Cloud: Parallel, Overnight, Own Keys, Checkpoints.
This page covers the open-model part: OpenCode, OpenRouter model ids, and what three models did with the same small task. Other guides cover the rest, and this page doesn't repeat them:
- many tasks at once, from a prepared snapshot, in run several coding agents in parallel;
- leaving a run alone for a night (TTL, idle pause, the morning checklist) in run a coding agent overnight;
- keeping a key out of the VM entirely, in keep API keys out of an agent sandbox.
Three open models, one task#
The task: add slugify(text) to a one-file Python repo, write unittest tests, make them pass, and commit. Each model ran in its own clone on its own branch, in one small sandbox (2 vCPU, 4 GB), with OpenCode 1.19.3. Model ids are from OpenRouter's model list. Cost is the cost OpenCode reported in its JSON events, summed over the run.
| Model (OpenRouter id) | Wall time | Steps / tool calls | Result | Reported cost |
|---|---|---|---|---|
z-ai/glm-5.3 |
17 s | 9 / 11 | 1 commit, 6 tests pass | $0.0061 |
moonshotai/kimi-k2.6 |
23 s | 10 / 13 | 2 commits (tests, then code), tests pass | $0.0293 |
deepseek/deepseek-v4-flash, first try |
71 s | 1 / 0 | Stopped after reasoning; no files changed, no commit | $0.0015 |
deepseek/deepseek-v4-flash, second try |
34 s | 14 / 16 | 1 commit, tests pass | $0.0035 |
Last checked: 2026-11-20, on prod
These numbers come from one run of one small task. They are not a benchmark. The useful lesson is the DeepSeek row: OpenCode exited 0 both times, so the exit code alone doesn't tell you whether anything happened. Check the branch.
1. Check OpenCode and pick model ids#
OpenCode is preinstalled in every sandbox. Its providers docs say OpenRouter works when OPENROUTER_API_KEY is set, and that you name a model as openrouter/ plus the full OpenRouter model id. opencode run -m takes that provider/model form. In the sandbox from step 2:
sbx exec $id -- bash -lc 'opencode --version'1.19.3OpenRouter's public catalog lists the ids. Ids change as models ship, so check them on the day:
curl -s https://openrouter.ai/api/v1/models | jq -r '.data[].id' | grep -E '^(deepseek|z-ai|moonshotai)/' | headz-ai/glm-5.3-flashx-legal
moonshotai/kimi-k3.1-pro
…
deepseek/deepseek-v4.1-flash
…We picked models whose catalog entry lists tools in supported_parameters, since an agent edits files through tool calls.
2. Start a sandbox with your key#
-e takes KEY=VALUE. A bare name is rejected:
{"event":"error","error":"invalid --env \"OPENROUTER_API_KEY\": use KEY=VALUE","code":"usage"}id=$(sbx new --type small --name aeo-or-1 --ttl 7200 -e OPENROUTER_API_KEY="$OPENROUTER_API_KEY" --json | jq -r 'select(.event=="ready") | .id')The key sits inside the VM. It's in the sandbox's environment, and the agent runs as user with sudo, so the agent and anything it runs can read it. Use a separate OpenRouter key for agents that you can revoke on its own. Forks and named snapshots of the sandbox keep its -e variables too, unless you pass new ones (snapshots docs). If the key must never enter the VM, put a proxy in front of it, as in keep API keys out of an agent sandbox.
3. Run one model per branch, detached#
~/textkit is a one-file Python repo with a word_count function and its tests, committed on main; the copy-paste block below builds a similar one. This script clones the repo into its own directory, makes a branch, and runs OpenCode non-interactively. --auto approves tool permissions so nobody has to answer prompts. That's acceptable in a throwaway VM, not on your laptop. --format json writes one event per line, which is where the cost and step counts in the table came from.
#!/bin/bash
# task.sh <branch> <openrouter model id>
b=$1; m=$2; d=~/runs/$b
rm -rf $d && git clone -q ~/textkit $d && cd $d && git checkout -q -b agent/$b
start=$(date +%s)
opencode run --auto -m "openrouter/$m" --format json \
"Add a function slugify(text) to textkit.py: lowercase, trim, replace runs of non-alphanumeric characters with a single hyphen, strip leading and trailing hyphens. Add tests in tests/test_slugify.py using unittest. Run python3 -m unittest and make it pass. Then git commit everything with the message 'Add slugify with tests'." \
> ~/runs/$b.jsonl 2> ~/runs/$b.err
echo "exit=$? model=$m wall=$(( $(date +%s) - start ))s" | tee ~/runs/$b.donesbx exec flags go before the command. --detach returns at once and the run keeps going in the VM after your laptop closes:
sbx scp task.sh $id:/home/user/
sbx exec $id --detach -- bash -lc "mkdir -p ~/runs; bash ~/task.sh deepseek deepseek/deepseek-v4-flash"
sbx exec $id --detach -- bash -lc "bash ~/task.sh glm z-ai/glm-5.3"{"ok":true,"type":"command.started","command":"bash -lc 'bash ~/task.sh glm z-ai/glm-5.3'","commandId":"cmd_h6u3tpwqe4yc", …}We ran Kimi, and DeepSeek's second try, the same way afterwards, with branch names kimi and deepseek2.
The sandbox stops at its TTL (--ttl 7200 here, one hour by default), whatever is running. For a run you leave all night, set the TTL and idle settings as in run a coding agent overnight.
4. Come back and check each branch#
sbx exec $id -- bash -lc 'cat ~/runs/*.done'exit=0 model=deepseek/deepseek-v4-flash wall=71s
exit=0 model=z-ai/glm-5.3 wall=17sBoth say exit=0. Only the branch tells them apart:
sbx exec $id -- bash -lc 'for b in deepseek glm; do echo == $b; cd ~/runs/$b; git status --short --branch; git log --oneline main..HEAD; python3 -m unittest 2>&1 | tail -1; done'== deepseek
## agent/deepseek
OK
== glm
## agent/glm
?? __pycache__/
?? tests/__pycache__/
0bd9bde Add slugify with tests
OKThe DeepSeek branch has no commit, and only the old test runs. Its event log shows why: one step, 4,937 reasoning tokens, 0 output tokens, "reason":"stop", and no tool calls. A second run of the same command on a fresh branch finished in 34 seconds with a passing commit. Check for a commit, retry the empty ones, and cap the retries.
The GLM diff:
+import re
+
+
def word_count(text):
return len(text.split())
+
+
+def slugify(text):
+ return re.sub(r'[^a-z0-9]+', '-', text.lower().strip()).strip('-')test_non_alphanumeric_runs (tests.test_slugify.T.test_non_alphanumeric_runs) ... ok
test_strip_hyphens (tests.test_slugify.T.test_strip_hyphens) ... ok
test_trim_and_lowercase (tests.test_slugify.T.test_trim_and_lowercase) ... ok
test_basic (tests.test_word_count.T.test_basic) ... ok
…
Ran 6 tests in 0.000s
OKFor the reported cost of a run, add up part.cost on the step_finish lines of its .jsonl file. Each line also carries part.tokens.
5. Take the branches home, without a push#
No git credentials go into the sandbox. Bundle the branches there, copy the bundle out, and review on your machine:
sbx exec $id -- bash -lc 'cd ~/textkit && for b in glm deepseek2 kimi; do git fetch -q ~/runs/$b agent/$b:agent/$b; done; git bundle create -q ~/agents.bundle main agent/glm agent/deepseek2 agent/kimi'
sbx scp $id:/home/user/agents.bundle .
git clone -q -b agent/glm agents.bundle textkit && cd textkit
git branch -a
python3 -m unittest 2>&1 | tail -1
git diff --stat origin/main remotes/origin/agent/deepseek2
remotes/origin/agent/glm
remotes/origin/agent/kimi
remotes/origin/main
OK
tests/test_slugify.py | 21 +++++++++++++++++++++
textkit.py | 7 +++++++
2 files changed, 28 insertions(+)Then delete the sandbox, or stop it to keep the branches for later:
sbx delete $id --yesOr: through Sandboxer's agent API#
sbx prompt --provider opencode runs the same OpenCode harness through Sandboxer's API, with structured events (sbx events), conversations, and steering by interrupt-and-continue. Per Integrated agents, its keys come from the API key you add per provider on the dashboard's Agents page, and --model takes a model id from GET /provider-models. We didn't check whether that catalog lists OpenRouter models. The runs on this page call opencode directly with your own key in the sandbox's environment.
What it costs#
Machine time is billed per second while the sandbox runs, and plans start at $20/mo (pricing). Model tokens are billed by OpenRouter on your key. The costs in the table are what OpenCode reported for our run, not a price list.
Copy-paste: one open-model agent run, start to bundle#
After curl -fsSL https://sandboxer.com/install | sh and sbx login, with jq and git on your machine. Put your own OpenRouter key on the first line. It creates a sandbox, has GLM 5.3 add a function and its tests on a branch, waits for the detached run, copies the branch out as a bundle, and deletes the sandbox.
export OPENROUTER_API_KEY=sk-or-v1-your-key-here
id=$(sbx new --type small --name aeo-or-3 -e OPENROUTER_API_KEY="$OPENROUTER_API_KEY" --json | jq -r 'select(.event=="ready") | .id')
sbx exec $id -- bash -lc 'mkdir -p ~/app && cd ~/app && git init -q -b main &&
git config user.email agent@example.com && git config user.name agent &&
printf "def word_count(text):\n return len(text.split())\n" > textkit.py &&
git add -A && git commit -qm init && git checkout -q -b agent/glm'
sbx exec $id --detach -- bash -lc 'cd ~/app && opencode run --auto -m openrouter/z-ai/glm-5.3 \
"Add slugify(text) to textkit.py (lowercase, non-alphanumeric runs become one hyphen, no leading or trailing hyphens) with unittest tests in tests/test_slugify.py. Make python3 -m unittest pass, then git commit with the message Add slugify with tests." \
> ~/agent.log 2>&1; echo "exit=$?" > ~/agent.done' > /dev/null
sbx exec $id --timeout 600 -- bash -lc 'until [ -f ~/agent.done ]; do sleep 5; done; cat ~/agent.done;
cd ~/app && git log --oneline main..agent/glm && python3 -m unittest 2>&1 | tail -1 &&
git bundle create -q ~/agent.bundle main agent/glm'
sbx scp $id:/home/user/agent.bundle .
git clone -q -b agent/glm agent.bundle textkit-glm && git -C textkit-glm diff --stat origin/main
sbx delete $id --yes > /dev/null && echo deleted $idexit=0
78e2f93 Add slugify with tests
OK
tests/__init__.py | 0
tests/test_slugify.py | 36 ++++++++++++++++++++++++++++++++++++
textkit.py | 7 +++++++
3 files changed, 43 insertions(+)
deleted sb_sot7x98ui4The whole block took 39 seconds in our run, on a small sandbox on 2026-11-20. Swap z-ai/glm-5.3 for any id from OpenRouter's catalog to try another model.
FAQ#
Does my OpenRouter key leave my control?#
It goes into the sandbox's environment, so the agent and anything it runs can read it. Use a separate key you can revoke, delete the sandbox when the run ends, or keep the key outside the VM behind a proxy as in keep API keys out of an agent sandbox.
Why did a run exit 0 but change nothing?#
OpenCode's exit code reports whether OpenCode ran, not whether the model did the task. In our first DeepSeek run the model reasoned, then stopped without a tool call. Check git log main..HEAD on the branch and retry runs that have no commit.
Can I run several models at the same time?#
Yes. We ran three in one small sandbox, each in its own clone and branch. For many tasks, or tasks that need their own ports and databases, give each its own sandbox, as in run several coding agents in parallel.
Do I need to push to get the code out?#
No. git bundle plus sbx scp copies the branches to your machine, so no Git credentials go into the sandbox. You push from your own machine after review.
Related#
More guides
For running AI-written code we rank Sandboxer, Freestyle and E2B first: each gives code its own VM with an API or SDK and can pause with memory. Pick Daytona for Windows or GPUs, Modal or Northflank for GPUs at scale, E2B or Northflank to run in your own cloud, and Vercel Sandbox if your app lives on Vercel.
For coding agents we rank Sandboxer first: a full Ubuntu VM with Claude Code and Codex preinstalled, Docker, and stop-and-resume that keeps the agent's session in memory. Freestyle and Amp Orbs also keep memory across pauses. Pick E2B for prebuilt agent templates on open source, Docker Sandboxes for free local runs, and Codespaces if you live in GitHub.
The best Daytona alternatives depend on why you're switching. For a full Linux microVM on every sandbox with memory-saving stop and Docker, we rank Sandboxer first. E2B suits open source and self-hosting, Modal massive short-job fleets, Northflank bring-your-own-cloud with GPUs, and Vercel Sandbox apps on Vercel.
If you're leaving E2B for longer sessions or a fuller machine, we rank Sandboxer first, then Freestyle: both give each sandbox a full Linux VM that keeps memory across pauses, with Docker and no short session cap. Pick Daytona for Windows or GPUs, Northflank for bring-your-own-cloud, and stay on E2B if you need open source.