Run untrusted code
Use a sandbox to run code you did not write, such as code a model produced, and send the result back. Outbound traffic from the sandbox is blocked, the code runs as a non-root user, and the sandbox is deleted afterwards.
Next step: run the script below. It needs a key with the models and sandboxes scopes, a stored google key, and a stored daytona key.
The whole flow
A model is asked a question and given one tool, run_python. It answers with code. The script runs that code in a new sandbox and sends the output back, and the model writes the final answer. The model call goes through the OpenAI SDK, and the sandbox calls use requests.
Output from a run against the hosted beta, which took 10 seconds:
The sandbox was running 2 seconds after the create. The first three lines come from the model’s code, so their wording changes from run to run. The model sometimes asks for the tool a second time, so the script loops until the response is text. An earlier version of this script made one tool round only and printed None in those runs. With the loop, six runs in a row ended with a sentence, and two of them used two tool rounds, each in its own sandbox.
What each setting does
What bad code does
We ran each of these as nobody in a Daytona sandbox with 1 CPU and 1024 MB. Every call returned 200. Read exit_code and timed_out to see how the command ended.
The disk and output rows come from an earlier run on the hosted beta. We reran the others for this page.
Twelve blocking commands sent to one sandbox at the same time all ran, and finished in 3.6 seconds for a 2 second command. Meadow sets no limit on commands per sandbox.
Things to know
- Exec
userneeds a tool in the image that can switch users:setpriv,runuser,su, orpython3. Debian and Ubuntu images have one. A missing user, or an image with none of these, returns 400. - Uploaded files belong to
rootwith mode 0644.nobodycan read and run/job/main.pythroughpython, and cannot write next to it. Give the code a writable place:/tmp, or a directory youchownfirst. See Files, ports, and SSH. - Code in the sandbox can read the sandbox’s environment. Put no secret in
envthat the code should not see. See Security. - With
block_all, nothing can be installed at run time. Build the packages you need into an image. A sandbox from a built image takesresourceslike any other. - Background commands keep their state in
/tmp/.meadow-execinside the sandbox. Code that removes or takes over that directory breaks later background commands in the same sandbox. With one sandbox per job this does not matter. - On Daytona,
stderrof a command that could not start begins with the path of Meadow’s wrapper script. Strip it before you sendstderrto a model. See Running commands. - A sandbox takes about 2 seconds to start on Daytona. To reuse one for several jobs, keep it and run each job with exec. The jobs then share one file system, so do that only for jobs that may see each other’s files.
More in Known limits.
API reference
Operations on this page: createChatCompletion, createSandbox, uploadFile, execInSandbox, deleteSandbox.