docs/source/scale-with-bentocloud/deployment/sandboxes.rst
.. important::
Sandbox is currently a preview feature. If you'd like early access, please reach out to us <https://www.bentoml.com/contact>_.
Modern AI applications like agentic systems often generate and execute code autonomously. However, letting these agents use tools, write scripts, and make decisions without human review is risky. Consider these common scenarios:
In each case, you can't assume the code is safe. Untrusted code can damage your infrastructure or expose sensitive data.
A BentoML Sandbox provides an isolated, secure, and ephemeral environment for running untrusted or dynamically generated code. With Sandboxes, you can:
You can create a Sandbox and specify its :doc:runtime </build-with-bentoml/runtime-environment>:
.. code-block:: python
client = bentoml.BentoCloudClient()
sb = client.sandbox.create( image=bentoml.images.Image(python_version="3.12") .run("apt-get install -y curl") .python_packages("fastapi") )
sb.wait_until_ready(log_polling=True)
To run commands in a Sandbox:
.. code-block:: python
result = sb.exec("python", "-c", "import sys;print(sys.version)") print(result.stdout.read())
result = sb.exec("python", "-c", "import sys;print(sys.version)") for line in result.stdout: print(line)
On the Monitoring tab of the Sandbox details page, you can view real-time metrics such as replica count and resource usage:
.. image:: ../../_static/img/build-with-bentoml/sandboxes/bento-sandboxes-scaling.png :alt: Sandbox scaling based on real-time traffic
.. image:: ../../_static/img/build-with-bentoml/sandboxes/bento-sandboxes-resources.png :alt: Sandbox resource usage
Previously created Sandboxes can be reused:
.. code-block:: python
sb = bentoml.sandbox.get(name=sandbox_name)
client = bentoml.BentoCloudClient() sb = client.sandbox.get(name=sandbox_name)
You can customize sandbox behavior via more parameters. For example:
.. code-block:: python
sb = bentoml.sandbox.create(cmd=["python", "-m", "http.server"])
sb = bentoml.sandbox.create(secrets=["my-secret"], envs={"HF_TOKEN": "abdef"})
sb = bentoml.sandbox.create(labels={"foo": "bar"})
Full signature:
.. list-table:: :header-rows: 1 :widths: 20 20 60
imageImage | NonelabelsMapping[str, str] | Nonecmdlist[str] | Nonesecretslist[str] | NoneenvsMapping[str, str] | Noneclusterstr | NoneclientBentoCloudClientdefault_client... code-block:: python
sb.destroy()
bentoml.sandbox.destroy(name=sandbox_name)