Use case

Add a code interpreter to your AI app.

Your users ask a question, the model writes some Python, and somebody has to run it. Give each conversation a sandbox and show the answer, the chart or the file it made.

The problem

Code a model wrote a second ago has been read by nobody. Run it on your own servers and one bad script is an incident.

Building a safe place to run it yourself means virtual machines, images, clean-up and limits. That is a product of its own, and not the one you set out to build.

How Annex does it

  1. Start a sandbox for the conversation

    One request starts a fresh machine on a provider account you have connected. It has the public internet and nothing of yours.

  2. Run what the model wrote

    Write the file, run the command, and read what it printed and its exit code. Hand both back to the model so it can fix its own mistakes. A command that runs too long can be stopped.

  3. Show the result and stop

    Download the chart or the file it produced and show it to your user. Stop the sandbox when the conversation ends; Annex also stops one that has sat idle.

What it looks like

Start one, run the code, stop it.

From your own code

curl -X POST $ANNEX/sandboxes -H "Authorization: Bearer $KEY" \
  -d '{"route": "cheapest"}'

curl -X POST "$ANNEX/sandboxes/sbx_…/commands?wait=60" \
  -H "Authorization: Bearer $KEY" \
  -d '{"command": "python3 answer.py"}'

curl -X POST $ANNEX/sandboxes/sbx_…/stop -H "Authorization: Bearer $KEY"

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.

Your appthe questionThe modelwrites pythonIts own sandboxruns the codeThe chartback on the page

The whole thing is about 90 lines and one page. Get the code →

  1. 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
  2. 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: [] })
  3. 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)}`
      },
    })
  4. 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')}`)
    }
  5. 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.

Get the code on GitHub

Related use cases