Code execution for AI agents

Code execution API for AI agents: stateless API or stateful sandbox?

A stateless API runs one program per call in a fresh sandbox and returns the output. A stateful sandbox gives the agent a machine for a session. Pick the API for one-shot runs, many languages and judging against tests; pick the sandbox for installs, network, long sessions and files that persist.

By the OnlineCompiler.io team. Updated . We make OnlineCompiler.io, so we're one of the options here. Every competitor figure links to the vendor's own page, and we say where our API falls short.

Which one does your agent need?

  • Use a stateful sandbox (E2B, Northflank, Daytona, Modal, Vercel Sandbox and others) if the agent installs packages, needs the network, keeps files or processes between steps, runs longer than 30 seconds, or works in a notebook. We do none of those.
  • Use a stateless run-and-judge API like OnlineCompiler.io if each step is one program: check a function, compute an answer, run a candidate's solution against tests, in any of our 46 languages, with an exit code and a verdict back. There is no session to manage or pay for while the model thinks.
  • Use both when an agent needs a persistent machine for building and a cheap, isolated runner for checking: the sandbox for the project, an API call for each "does this code pass?"

What does each kind do well?

A stateful sandbox is a computer the agent rents for a session. The agent installs what it needs, writes files, starts processes and comes back to them in the next step, and some vendors pause the machine and keep its disk (and, at E2B, memory) until you resume it. That is what a coding agent working on a repository needs, and it is more than a code runner.

A stateless run-and-judge API does less on purpose. Each call is a program, optional input and files; the answer is the program's output, errors, exit code, time and memory. Because nothing carries over, there is no machine to start, keep warm, pause or clean up, and a bad run cannot affect the next one. The same endpoint can also judge: send test cases and get a verdict per test and a score, which a plain sandbox leaves for you to build.

How do the options compare?

Every row below is what that vendor's own page says, linked, read on 6 November 2026. Their prices and limits change, so check the page before you buy. Where a cell says nothing about a topic, the page we read did not.

ServiceSession modelLanguagesPackages and networkPricingIsolation
OnlineCompiler.ioOne run per call, up to 30 s; nothing kept between calls46; Python has a fixed data science setNo installs; no network on any run100,000 runs a month free; Starter $10 a month for 150,000 runs ($0.067 per 1,000)A fresh gVisor sandbox per run
E2B"A fast, secure Linux VM created on demand", pausable and resumable; pausing saves filesystem and memory, and paused sandboxes are kept until you kill them. Sessions up to 1 hour (Hobby) or 24 hours (Pro) per pricingPython, JavaScript and TypeScript, R, Java and Bash out of the boxInternet access is enabled by default, and can be turned offHobby $0 with a one-time $100 credit; Pro $150 a month plus usage, charged per vCPU-second and GiB-secondSays "isolated" on the page we read
NorthflankPausing "preserves attached-volume files, but stops processes and discards ephemeral files"Its blog says command-execution APIs "can run code using runtimes installed in the sandbox", so the languages depend on the runtimes you installEnvironment you build; network controls are not described thereNorthflank Cloud's general pricing, not sandbox-specific: a free tier with up to $20 a month of resources, then billed by the second ($0.01667 per vCPU-hour, $0.00833 per GB-hour)On Northflank Cloud, "CPU sandboxes run in microVMs and GPU sandboxes use gVisor"
DaytonaStart, pause, stop, archive and fork; auto-stop and auto-archive settingsPython, TypeScript and JavaScript runtimes (Python by default)Agents can "install packages, run servers, compile code, and manage processes"$200 free compute, then per second ($0.0504 per vCPU-hour, $0.0162 per GiB-hour)Daytona's docs say each sandbox gets its own kernel, filesystem and network stack, and that sandboxes run as Linux containers by default, with VM sandboxes also offered
ModalDefault maximum lifetime 5 minutes, up to 24 hours; volumes and filesystem snapshots keep stateYour own image; SDKs for Python, JavaScript and GoCustom, named or registry images (for example python:3.13-slim); the page says little about network policyStarter plan with $30 a month in credits; sandboxes at $0.00003942 per core-second and $0.00000667 per GiB-secondgVisor, or VMs each with their own Linux kernel
Vercel SandboxPersistent sandboxes that save state when stopped and resume; snapshotsDefault image includes Node.js LTS and Python 3.14; your own images allowedFull root access in an Ubuntu image; builds on images you can extendBilled on Active CPU (no CPU charge while your code waits on a model) plus provisioned memory"A secure Firecracker microVM with its own filesystem and network"

Daytona, Vercel and Northflank describe a general machine, and Modal lets you bring your own image. None of the pages above describe judging code against test cases, so checking output against expected answers is code you write.

When is a stateful sandbox the better choice?

  • Installing packages. We have no pip install and no network. A sandbox can fetch what the task needs.
  • Network access. An agent that calls APIs, clones repositories or downloads data needs a sandbox with the network on.
  • Long sessions. A run here ends after 30 seconds at most. Sandboxes run for minutes to hours.
  • Files and processes that persist. Dev servers, databases, a repository checked out once and edited for fifty steps.
  • Notebooks and stateful interpreters. Variables and loaded data that survive from one call to the next.
  • GPUs. Every run here is CPU only; Modal's sandbox guide covers GPU sandboxes.

When is a stateless API the better choice?

  • One-shot runs in many languages. Each of our 46 languages is one compiler value, so the agent can write C++, Go, R or SQL without you building images.
  • Judging. runTests and /api/run-tests/ take test cases and return an overall verdict, a score and a verdict per test, with checkers for exact, trimmed and float output. For an agent that must know whether code is right, the verdict is the tool.
  • No session to manage. No sandbox to create, keep alive or pay for while the model is thinking. An agent that fans out to a thousand checks makes a thousand calls.
  • Existing clients. Judge0- and Piston-compatible endpoints mean tools written for those work by changing the base URL; see the wider comparison of code execution APIs.
  • A free tier that fits a prototype: 100,000 runs a month, which is the figure to compare with the credits above.

What does an agent tool call look like?

A model that is asked for the 40th Fibonacci number calls a runCode tool with the program; your handler sends it here. This is that request, with the program run in our sandbox. The block below is what came back, shortened: the API also returns time and memory.

tool.js
// The body of a runCode tool call: what the model asked to run.
const response = await fetch('https://api.onlinecompiler.io/api/run-code-sync/', {
  method: 'POST',
  headers: {
    Authorization: process.env.ONLINECOMPILER_API_KEY,
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    compiler: 'python-3.14',
    code: `def fib(n):
    a, b = 0, 1
    for _ in range(n):
        a, b = b, a + b
    return a
print(fib(40))`,
  }),
});
console.log(await response.json());
Result
{
  "status": "success",
  "exit_code": 0,
  "output": "102334155\n",
  "error": ""
}

With the Vercel AI SDK you do not write that handler: the AI SDK tools in the onlinecompiler npm package give a model runCode and runTests in a few lines.

agent.js
import { generateText, stepCountIs } from 'ai';
import { onlinecompilerTools } from 'onlinecompiler/ai-sdk';

const { text } = await generateText({
  model: 'openai/gpt-5-mini',
  tools: onlinecompilerTools(), // runCode and runTests; reads ONLINECOMPILER_API_KEY
  stopWhen: stepCountIs(5),
  prompt: 'What is the 40th Fibonacci number? Compute it, don\'t guess.',
});

For Claude, ChatGPT, Cursor or VS Code the MCP server offers the same runs and judging as tools, with nothing to code. If the agent interviews or tutors, the AI interview page shows how it judges against stored problems. For data analysis, see the code interpreter API.

Limits to check before you choose

A run here has 30 seconds, 512 MB of memory and no network, and give the agent's API key its own monthly limit in the dashboard so a loop cannot run away; see limits. Output is capped at 64 KB per stream. A program cannot read what an earlier call wrote, so an agent that needs that has to send the data again as files. If any of those rule you out, the sandboxes above are the right tool.

Sources

All read on 6 November 2026. Vendors change prices and limits; their pages are the authority.

Frequently asked questions

Do AI agents need a stateful sandbox to run code?

Only some do. An agent that writes a function and checks it, answers a maths question by computing it, or grades a candidate against tests needs one isolated run per step, and a stateless API does that. An agent that installs packages, keeps a dev server running, edits a repository over many steps or works in a notebook needs a sandbox that persists.

What is the difference between a stateless code execution API and a sandbox?

A stateless API runs one program in a fresh, isolated environment and returns its output; nothing is kept for the next call. A sandbox is a machine the agent holds for a session, with a filesystem, processes and often a network that stay between commands. The first is simpler and billed by run; the second is more capable and billed by the time it exists.

Can an agent install packages with OnlineCompiler.io?

No. Python has a fixed, published set of data science packages, and every run has no network, so there is no pip install. If the agent needs a package outside that set, use a sandbox where you control the image.

Is there a free code execution API for AI agents?

Yes. The free plan includes 100,000 runs a month, commercial use allowed, no credit card. The sandbox vendors above give a one-time or monthly credit instead; E2B, Northflank, Daytona and Modal each give a figure, in the table with the date we read it.

Which languages can an agent run?

46, compiled and interpreted, each in a fresh sandbox per run. The sandbox vendors in the table list fewer by default (typically Python and JavaScript, plus what you install), but a persistent machine can run anything you install on it.