Secure code execution
A CodeAgent executes model-generated Python. Running that on your own machine is risky, so smolagents ships several sandboxes.
Local Python interpreter (default)
By default code runs in a restricted interpreter that blocks dangerous builtins and only allows imports you explicitly whitelist:
agent = CodeAgent(
tools=[...],
model=model,
additional_authorized_imports=["numpy", "json"],
)
This is safer than raw exec, but not a hard security boundary — untrusted tasks should use a real sandbox.
E2B — cloud sandbox
agent = CodeAgent(tools=[...], model=model, executor_type="e2b")
Docker — local sandbox
agent = CodeAgent(tools=[...], model=model, executor_type="docker")
Each agent step runs inside an isolated container, so even if the model writes destructive code it cannot touch your host. Use a sandbox whenever the agent handles untrusted input.