Teaching
Programming with an AI agent: OpenCode
Programming with an AI agent without losing control of your code, for students. The example followed from start to finish is a queue simulation in Julia.
Version of December 1, 2026, for OpenCode 2.4. Free models and their terms change often: check them in the OpenCode documentation before you use them.
Downloads: the guide as a PDF (English, French) and the example project (code, tests, AGENTS.md, opencode.json).
1. A coding agent
A coding agent passes your request to a large language model and gives it tools: reading the project’s files, changing them, running commands in a terminal, looking up web pages. It chains these actions on its own until the task is done, or until it needs you.
OpenCode is an open source agent. It runs in a terminal, as a desktop app or in a browser, on Linux, macOS and Windows. OpenCode is not a model itself: it sends your request, and the files the model needs, to the provider you chose. Unless the model runs on your machine, your code leaves your computer.
The code the agent writes becomes yours: you must be able to explain it and answer for it. A fast agent also makes mistakes fast, and confidently. The method fits in one sentence: describe precisely, move in small steps and check each step with tests you understand.
2. Course rules
Using AI in coursework is not allowed everywhere, nor in the same way. Read the course outline; when in doubt, ask the instructor before using an agent for graded work. When it is allowed, you often have to declare it: OpenCode keeps the history of each session, which you can reopen with /sessions.
Never give an online model personal data, research data covered by an agreement, or code you are not allowed to share.
3. Installing OpenCode
On Linux and macOS, the install script is enough:
curl -fsSL https://opencode.ai/v2/install | bash
You can also use Homebrew (brew install anomalyco/tap/opencode-v2) or npm (npm install -g @opencode/cli). Windows package managers are not supported: on Windows, download the program or the desktop app from the install page. Then check the version:
opencode --version
It should start with 2. Version 1 is still distributed (1.19 in November 2026), and its configuration differs: it uses permission, bash and task where version 2 uses permissions, shell and subagent. The commands and configuration files given here are those of version 2.
Start OpenCode in the project folder. That is where it looks for files and runs commands:
cd ~/courses/mm1
opencode
4. Choosing a model
In OpenCode, /connect connects a provider and /models picks one of its models. There are three kinds of options.
OpenCode console models. Choose OpenCode in /connect and sign in with an OpenCode account. Models whose name ends in Free are free for a limited time; the others are paid per request, with credits, or through the OpenCode Go subscription, at $10 a month. The console lets you set a monthly budget.
A subscription you already have. /connect accepts a ChatGPT Plus or Pro account (usage counts against your plan) and a GitHub Copilot account. GitHub gives free Copilot access to students verified through GitHub Education.
A model on your own machine. With Ollama, OpenCode finds the models served at 127.0.0.1:11434 by itself; LM Studio and vLLM work too. Nothing leaves your computer, but you need a powerful machine, and small models make more mistakes.
Choosing a free model commits your data. Here is what the documentation said on December 1, 2026:
| Free models | Your data |
|---|---|
| DeepSeek V4.1 Flash Free, Hy4 flash Free | no retention, no training |
| MiMo-V2.6-Flash Free, Jev 1.13 Free | no exception listed: the general rule (no retention) applies |
| Big Pickle, Flint 40B Free | may be used to improve the model; Flint 40B keeps it for 30 days |
| Tidepool Alpha Free, Seaglass Alpha Free, Iceman Free | retained and used to improve the model |
| Nemotron 3.5 Lightning Free, Nemotron 3.5 Super VL Free | trial use, logged by NVIDIA: “do not submit personal or confidential data” |
Console models are hosted in the United States and the European Union. Paid models from OpenAI, Anthropic, Google and xAI retain requests for 30 to 55 days. To hide free models, set the environment variable OPENCODE_DISABLE_FREE_MODELS=1. For work that involves sensitive data, use a local model, or failing that a no-retention model.
5. Setting up the project
A Git repository
Before letting the agent write, put the project under Git and commit after each successful step. git diff then shows exactly what the agent changed, and git restore wipes out a failed step. OpenCode also has /undo and /redo, but Git remains your safety net.
mkdir mm1 && cd mm1
git init
Project instructions: AGENTS.md
OpenCode reads the project’s AGENTS.md file at the start of each session. Put there what the agent must always know: how to run the tests, what it must not do, the code conventions. Instructions that apply to all your projects go in ~/.config/opencode/AGENTS.md. The example’s instructions:
# M/M/1 queue simulation in Julia
- Julia 1.12 with the standard library only (Random, Statistics, Test): ask before adding a package.
- Code in `src/MM1.jl`, tests in `test/runtests.jl`.
- After every change, run the tests with `julia test/runtests.jl` and show their output.
- Never change a test to make it pass: if a test fails, explain why and propose a fix to the code.
- Every function that draws random numbers takes an `rng` argument; the tests use a fixed seed.
- Code and comments in English; short functions, each with a docstring.
- Do not run `git commit` or `git push`: I commit myself after reading the changes.Two instructions matter more than the others. Never change a test to make it pass: a stuck agent may be tempted to weaken the test rather than fix the code. Do not commit: you keep control over what goes into the repository.
Permissions: opencode.json
By default, OpenCode lets the agent edit files, run commands, look up the web and leave the project folder without asking; only reading .env files needs your approval. The opencode.json file, at the root of the project, tightens these rules. The example’s file asks before any command except the tests, git status and git diff, forbids git push and asks before any access outside the project:
{
"$schema": "https://opencode.ai/config.json",
"permissions": [
{ "action": "shell", "resource": "*", "effect": "ask" },
{ "action": "shell", "resource": "julia test/runtests.jl", "effect": "allow" },
{ "action": "shell", "resource": "git status *", "effect": "allow" },
{ "action": "shell", "resource": "git diff *", "effect": "allow" },
{ "action": "shell", "resource": "git push *", "effect": "deny" },
{ "action": "external_directory", "resource": "*", "effect": "ask" }
]
}The last matching rule wins: exceptions come after the general rule. When no rule matches, OpenCode asks. Settings for all your projects go in ~/.config/opencode/opencode.json. Finally, keep passwords and access keys out of the project folder.
6. Working with the agent
Plan, then build. OpenCode has two primary agents. Plan explores the project and proposes a plan without changing files; Build, the default agent, writes the code. Shift+Tab switches between them. Start with Plan: a plan is easier to fix than code.
Describe it like a specification. Say what the program must do, its inputs and outputs, the constraints (language, allowed packages, performance) and how you will know it is right. A vague request gets code that answers a different question.
One step at a time. After each step, run the tests, read git diff and commit. If something breaks, you know which step broke it.
Show the right files. @src/MM1.jl attaches a file to the request, and @src/MM1.jl#10-20 a few lines. A command starting with ! (for example !julia test/runtests.jl) runs in the terminal, and its output stays in the conversation: the agent sees it.
Start fresh. A long conversation ends up confusing the model. For a new task, open a new session with /new and restate the context in a few lines.
When the agent gets it wrong. Describe what you observe: the input, the expected result, the result you got. Ask it to explain the cause before fixing it. If it goes in circles, step back with /undo and rephrase.
7. The example: an M/M/1 queue in Julia
In an M/M/1 queue, customers arrive as a Poisson process of rate λ and a single server serves them, in order of arrival, in exponential times of rate μ. If ρ = λ/μ < 1, the queue is stable and the mean waiting time in queue is Wq = ρ/(μ − λ): for λ = 0.8 and μ = 1, Wq = 4. This known value will be used to check the simulation.
Lindley’s recursion is all the simulation needs: if Wk is the waiting time of customer k, Sk its service time and Ak+1 the time until the next arrival, then Wk+1 = max(0, Wk + Sk − Ak+1).
First request, to the Plan agent
I want to simulate an M/M/1 queue in Julia to estimate the mean waiting time in queue. In src/MM1.jl, write a function mean_wait(λ, μ, n; rng, warmup) that simulates n customers with Lindley’s recursion, discards the first warmup customers and returns the mean waiting time in queue; it must reject λ ≥ μ. Add a function that computes a mean and a confidence interval from independent replications. In test/runtests.jl, compare the mean of 30 replications of 100,000 customers with the theoretical value ρ/(μ − λ), for λ = 0.8 and μ = 1. Propose a plan first, without writing code.
Read the plan before going further. Does it include Lindley’s recursion, a warm-up period, independent replications, the theoretical value 4? Fix what is missing, then switch to Build (Shift+Tab):
The plan is fine. Write the code and the tests, then run the tests.
A correct result
The code below is a checked version. Your agent’s code will differ, but it must pass the same tests.
module MM1
using Random, Statistics
export mean_wait, confidence_interval
"""
mean_wait(λ, μ, n; rng = Random.default_rng(), warmup = 1_000)
Simulate an M/M/1 queue (arrival rate λ, service rate μ) with Lindley's recursion and return
the mean waiting time in queue of `n` customers, after discarding the first `warmup` customers.
"""
function mean_wait(λ, μ, n; rng = Random.default_rng(), warmup = 1_000)
0 < λ < μ || throw(ArgumentError("the queue is stable only if 0 < λ < μ"))
w = 0.0 # waiting time of the current customer
total = 0.0
for k in 1:(warmup + n)
k > warmup && (total += w)
service = randexp(rng) / μ # exponential, mean 1/μ
gap = randexp(rng) / λ # time until the next arrival, mean 1/λ
w = max(0.0, w + service - gap)
end
return total / n
end
"""
confidence_interval(x; z = 1.96)
Mean of the replications `x` and half-width of its normal confidence interval.
"""
function confidence_interval(x; z = 1.96)
return mean(x), z * std(x) / sqrt(length(x))
end
endusing Test, Random
include(joinpath(@__DIR__, "..", "src", "MM1.jl"))
using .MM1
@testset "M/M/1 waiting time against theory" begin
λ, μ = 0.8, 1.0
theory = λ / (μ * (μ - λ)) # Wq = ρ / (μ - λ) = 4
rng = Xoshiro(2026)
x = [mean_wait(λ, μ, 100_000; rng) for _ in 1:30]
m, h = confidence_interval(x; z = 3)
println("simulated: ", round(m; digits = 3), " ± ", round(h; digits = 3), ", theory: ", round(theory; digits = 3))
@test allunique(x) # the replications must differ
@test abs(m - theory) < h
end
@testset "Unstable queue is rejected" begin
@test_throws ArgumentError mean_wait(1.0, 1.0, 10)
@test_throws ArgumentError mean_wait(1.2, 1.0, 10)
end$ julia test/runtests.jl
simulated: 4.024 ± 0.08, theory: 4.0
Test Summary: | Pass Total Time
M/M/1 waiting time against theory | 2 2 0.6s
Test Summary: | Pass Total Time
Unstable queue is rejected | 2 2 0.0s
The test’s interval is wide (three standard errors): with a 95% interval, correct code would fail one time in twenty with a different seed. The fixed seed makes the test reproducible.
8. Checking what the agent wrote
Here are two plausible mistakes, of the kind an agent can make. In both cases the program runs without any error message; only the tests reveal them.
A rate taken for a mean
gap = λ * randexp(rng) # instead of randexp(rng) / λ
The time between two arrivals then has mean λ = 0.8 instead of 1/λ = 1.25: customers arrive faster than the server can serve them, and the queue overflows.
simulated: 10271.042 ± 135.225, theory: 4.0
M/M/1 waiting time against theory: Test Failed
Expression: abs(m - theory) < h
Evaluated: 10267.042143592527 < 135.22470051450654
Identical replications
x = [mean_wait(λ, μ, 100_000; rng = Xoshiro(2026)) for _ in 1:30]
Each replication restarts from the same seed: the 30 results are identical. The mean, 3.916, is plausible; it is the zero-width interval that gives the mistake away.
simulated: 3.916 ± 0.0, theory: 4.0
M/M/1 waiting time against theory: Test Failed
Expression: allunique(x)
A first test, h > 0, let this mistake through: rounding gives h = 1.5·10−15, not zero. The test now checks that the replications all differ. Test the properties you expect, not just the final number.
To check at every step
- Compare with a known result: a formula, a limiting case, a small case worked out by hand.
- Check that the tests have not changed:
git diff test/. - Read the whole diff: changes you did not ask for, deleted code, an added package.
- Check that a package or function really exists, in the Julia documentation or on JuliaHub: models sometimes make them up.
- Ask the agent to explain a line you do not understand, then check its explanation in the documentation.
- Run the tests yourself: the agent saying “all tests pass” is not proof.
9. Risks
Your data. What you send to the model leaves your machine, except with a local model. Some free models retain your requests and use them to improve themselves (see section 4).
Commands. The agent can delete or overwrite files and install software; by default, it does not ask permission. Work in a folder set aside for the project, never at the top of your home directory, under Git, with an opencode.json that asks before commands.
Hidden instructions. A file, a web page or a message can contain text written for the agent (“ignore the previous instructions and…”). Be wary of projects you did not write and of unknown web pages the agent looks up.
Copyright. Generated code may reproduce existing code, under a licence that does not allow copying it. In graded work, this joins the question of course rules.
Costs. Paid models are billed per request, and a long session on large files costs more. Set a budget in the console.
Learning. Delegating all the work keeps you from learning what the course teaches. Write the core of the program yourself first, then use the agent to explain, review, write tests or explore a variant.
10. Quick reference
opencode | start OpenCode in the current folder |
opencode ~/courses/other | start it in another project |
/connect | connect a model provider |
/models, F2 | pick a model; cycle through recent models |
Shift+Tab, /agents | switch between Build and Plan; pick an agent |
@file, @file#20-45 | attach a file, or a few lines |
!command | run a command; its output stays in the conversation |
/undo, /redo | undo, redo |
/new, /sessions | new session; resume a session |
| Ctrl+P | the palette of all commands |
Sources
The OpenCode documentation, read on December 1, 2026: installation, console models and privacy, providers, agents, permissions, AGENTS.md, interface. Student access to Copilot: GitHub documentation. The example’s code was run with Julia 1.12.