Walkthrough
Build it yourself, part by part.
This is a whole code interpreter you can run this afternoon: a chat page, a model that writes Python, and a sandbox that runs it. Attach a spreadsheet, ask for sales by month, and a chart appears under the answer.
The shape is the same in every product that does this, large or small. The model never runs anything. It asks to run something, your server runs it somewhere safe, and the model reads what came back. Everything below is that loop, and the three decisions inside it.
-
Say what the machine needs, once
A bare sandbox has Python but not pandas, and installing it at the start of every conversation would make every first answer slow. So the tools go in a template: a small file that says what a sandbox for this job starts with.
The first sandbox started from it runs these commands. Annex then keeps the files they changed and puts them back in the next sandbox, so the install happens once, not once a conversation.
annex.yml
name: code-interpreter description: Python with pandas and matplotlib vcpu: 2 memory_gib: 4 route: cheapest setup: - | S=; [ "$(id -u)" = 0 ] || S=sudo command -v pip3 >/dev/null || { $S apt-get update && $S apt-get install -y python3 python3-pip; } - pip3 install --break-system-packages pandas matplotlib || pip3 install pandas matplotlib -
One sandbox for each conversation
The first message of a chat starts a sandbox and remembers it. Every later message in that chat uses the same one, so a file uploaded in the second message is still there in the fifth.
Two people chatting get two machines. Nothing one conversation writes can be read by another, which is the property you would otherwise have to build yourself.
server.mjs
const sb = await annex.start({ template: 'code-interpreter', idle_minutes: 30 }) await annex.ready(sb.id) // the template's set-up has finished await annex.run(sb.id, 'mkdir -p /tmp/work/out') chats.set(id, { sandbox: sb.id, history: [] }) -
Give the model one tool
The model gets a single tool, run_python, and a sentence telling it when to use it. When it calls the tool, the script is written to the sandbox and run there, and whatever it printed goes back to the model along with its exit code.
Handing back the exit code and the error text matters more than it looks. A model that sees its own traceback fixes its own mistake on the next call, so most failures never reach the person asking.
server.mjs
const runPython = betaTool({ name: 'run_python', description: 'Run a Python script in the sandbox and get what it printed.', inputSchema: { type: 'object', properties: { code: { type: 'string' } }, required: ['code'] }, run: async ({ code }) => { await annex.upload(c.sandbox, '/tmp/work/run.py', code) const r = await annex.run(c.sandbox, 'rm -f out/*; MPLBACKEND=Agg python3 run.py 2>&1', { cwd: '/tmp/work', timeout: 300 }) return `exit code ${r.exit_code}\n${r.output.slice(-20_000)}` }, }) -
Bring the chart back
The model is told to save every chart in one folder. After each run the server lists that folder, downloads any picture in it and sends it to the page with the answer.
The same few lines return any file: a cleaned spreadsheet, a report, a zip. The sandbox is where work is made; your app decides what to show.
server.mjs
for (const f of await annex.files(c.sandbox, '/tmp/work/out')) { if (!f.name.endsWith('.png')) continue const png = await annex.download(c.sandbox, f.path) images.push(`data:image/png;base64,${png.toString('base64')}`) } -
Run it
Three commands. The page opens on your own computer; the code the model writes runs on the provider account you connected, or on a machine of your own.
In your terminal
export ANTHROPIC_API_KEY=… annex template apply # saves annex.yml node server.mjs # then open http://localhost:3000
Before you rely on it
- There is no sign-in on the example, and every chat spends on your accounts. Put your own in front before anyone else can reach it.
- A sandbox left alone is stopped by Annex after 30 quiet minutes. To keep a person's files between visits, pause it instead, as the sandbox-for-every-user example does.
- Set a limit on sandboxes running at once in the dashboard, so a busy hour is a queue and not a bill.