---
source: https://iinventedlight.com/library/notes/smolagents-code-execution.md
title: "Run smolagents CodeAgent tools in a secure sandbox"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "Apache-2.0 (Hugging Face smolagents); Apache-2.0 (huggingface/smolagents (GitHub)); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "Summary by iinventedlight (AI-assisted). Sources: Hugging Face smolagents, Guided tour; huggingface/smolagents (GitHub), Secure code execution. Apache-2.0; Apache-2.0."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-19
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-19
as_of: 2026-11-19
confidence: medium
untested: "Not reproduced (2026-11-19). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: agent-operations
topic_area: "agent operations: smolagents code execution"
tags: [smolagents, code-agent, sandbox, tools]
client: Librarian
sources:
  - id: S1
    title: "Guided tour"
    publisher: Hugging Face smolagents
    url: https://huggingface.co/docs/smolagents/en/guided_tour
    accessed: 2026-11-19
    license: "Apache-2.0"
  - id: S2
    title: "Secure code execution"
    publisher: huggingface/smolagents (GitHub)
    url: https://github.com/huggingface/smolagents/blob/main/docs/source/en/tutorials/secure_code_execution.md
    accessed: 2026-11-19
    license: "Apache-2.0"
---
# Run smolagents CodeAgent tools in a secure sandbox
**Answer:** smolagents CodeAgent writes tool calls as Python code, which can run locally or in a sandbox. Local execution is convenient but is not robust isolation. The secure-execution guide says the robust options are remote sandboxes such as E2B or Docker.

## CodeAgent versus ToolCallingAgent

smolagents comes with two agent classes: CodeAgent and ToolCallingAgent. The key difference is how actions are specified and executed: code generation versus structured tool calling. CodeAgent generates tool calls as Python code snippets. Execution can be local (potentially unsecure) or a secure sandbox. Tools are exposed as Python functions via bindings. Strengths include expressiveness and flexibility; limitations include the risk of errors and the need for a secure execution environment. ToolCallingAgent writes tool calls as structured JSON, a common format used in many frameworks such as the OpenAI API, without code execution. Tools are defined with a JSON schema: name, description, and parameter types.

## Sandbox approaches

No sandbox can ever be completely secure. Running generated code in your local environment always carries some inherent risk. Robust security isolation for generated code comes from remote execution options like E2B or Docker. A malicious attack is less likely with well-known models from trusted inference providers, but the risk is not zero. Prefer a remote execution sandbox for high-security work or less trusted models.

| CodeAgent | Docker |
|---|---|
| CodeAgent | Python code snippets, local or sandboxed |
| ToolCallingAgent | structured JSON tool calls, no code execution |
| E2B or Docker | remote execution sandboxes |

## See also

- [ao-security-gemini-cli-sandbox](https://iinventedlight.com/library/notes/ao-security-gemini-cli-sandbox/)
- [deno-permissions](https://iinventedlight.com/library/notes/deno-permissions/)
- [ao-security-prompt-injection-design](https://iinventedlight.com/library/notes/ao-security-prompt-injection-design/)

## Claims

- id: C1
  claim: CodeAgent generates tool calls as Python code that can run locally or in a sandbox.
  source: S1
  evidence: "executed either locally (potentially unsecure) or in a secure sandbox"

- id: C2
  claim: ToolCallingAgent writes structured JSON tool calls without executing generated code.
  source: S1
  evidence: "ToolCallingAgent writes tool calls as structured JSON"

- id: C3
  claim: Robust isolation for LLM-generated code uses remote execution such as E2B or Docker.
  source: S2
  evidence: "use remote execution options like E2B or Docker"

## Sources

- [S1] Guided tour. Hugging Face smolagents. https://huggingface.co/docs/smolagents/en/guided_tour . License: Apache-2.0. Accessed 2026-11-19.

- [S2] Secure code execution. huggingface/smolagents (GitHub). https://github.com/huggingface/smolagents/blob/main/docs/source/en/tutorials/secure_code_execution.md . License: Apache-2.0. Accessed 2026-11-19.

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Hugging Face smolagents, Guided tour; huggingface/smolagents (GitHub), Secure code execution. Apache-2.0; Apache-2.0. Text was condensed and changed from the originals.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
