smolagents
Type: full-code · Vendor: Hugging Face (Paris, France) · Language: Python · License: Apache-2.0 · Status: active · Status in practice: emerging · First released: 2024-12-05
Provide a barebones (~1k LoC) Python library for multi-step ReAct agents whose default action format is executable Python code rather than JSON tool calls, with first-class sandboxed execution and Hugging Face Hub sharing.
Description. smolagents is Hugging Face's Apache-2.0 Python library for 'agents that think in code'. Its distinguishing design choice is CodeAgent: the LLM writes each action as a Python code snippet that is parsed and executed by a custom AST-walking interpreter (LocalPythonExecutor) or a remote sandbox (e2b, blaxel, modal, docker, wasm), with the print output returned as the observation for the next ReAct step. Alongside CodeAgent it also ships ToolCallingAgent for the conventional JSON tool-calling shape. The framework deliberately keeps abstractions minimal — the docs claim 'The logic for agents fits in ~thousand lines of code' — and integrates with the Hugging Face Hub for sharing agents and tools.
Agent loop shape. MultiStepAgent runs a textbook ReAct loop: SystemPromptStep + TaskStep go to memory, then while budget allows the agent serialises memory to chat messages, calls the Model, parses the completion into an action (Python code for CodeAgent, JSON tool call for ToolCallingAgent), executes the action (LocalPythonExecutor or remote sandbox), and records the result as an ActionStep observation. Iteration stops on final_answer or max_steps (default 20). An optional planning_interval inserts PlanningStep revisions without performing a tool call.
Primary use cases
- code-as-action agents that emit Python instead of JSON tool calls
- ReAct agents with sandboxed execution (local or e2b/modal/docker)
- Hub-shareable agents and tools
- minimal-footprint replacements for larger frameworks
Key concepts
- CodeAgent → code-as-action (docs) — Writes actions as Python code snippets that are parsed and executed; enables natural composability (function nesting, loops, conditionals).
- ToolCallingAgent (docs) — Conventional JSON/text-based tool-calling agent for users who prefer that paradigm.
- Tool / @tool → tool-use (docs) — A callable function with metadata the LLM uses; @tool decorator is the recommended way to define simple tools.
- executor_type (docs) — Sandbox selector — local | blaxel | e2b | modal | docker | wasm. Default 'local' uses LocalPythonExecutor.
- LocalPythonExecutor (docs) — AST-walking interpreter: imports allow-listed, op count capped; safer than exec() but not fully secure.
- managed_agents → agent-as-tool-embedding (docs) — Constructor parameter that gives an agent a list of sub-agents to call like tools.
Patterns this full-code implements —
- ★Code-as-Action Agent
The framework's defining thesis. CodeAgent writes actions as code rather than JSON tool calls. Bumped from first-class to core because removing this pattern removes the framework.
- ★★ReAct
MultiStepAgent IS a ReAct abstraction per upstream docs. Bumped from first-class to core because the whole agent class is described as that.
- ★Agent-as-Tool Embedding
Managed agents are passed via managed_agents=[...] on a parent agent; the parent calls them through the same prompt scaffolding as tools.
- ★★Step Budget
max_steps is a top-level constructor parameter with default 20.
- ★★Tool Use
Tools are the primary extension point; @tool decorator + first-class loaders for MCP, LangChain, and Hub Spaces.
- ★★Structured Output
Structured outputs are opt-in: structured_output=True on MCPClient/ToolCollection, use_structured_outputs_internally=False default on CodeAgent.
- ★Subagent Isolation
Managed agents are supported but Approach-1 sandboxing (per-snippet executor_type=e2b/blaxel/modal/docker) doesn't cover multi-agents; full multi-agent isolation requires running the whole agent in E…
Neighbourhood
Click any neighbour to follow the lineage. Scroll to zoom, drag to pan.