Smolagents by Hugging Face: The Fastest Path to a Single-Agent Loop

Smolagents turns a language model into a working agent that writes and runs Python code. This guide shows the fastest path to a single-agent loop.

By Galactic News Editorial · Published 2026-11-27T14:14:48+00:00 · Updated 2026-11-27T14:14:48+00:00

Smolagents Hugging Face code agents building a single-agent loop

Smolagents by Hugging Face: The Fastest Path to a Single-Agent Loop

Smolagents turns a language model into a working agent that writes and runs Python code. It is Hugging Face's lightweight agent library. You need a model, a list of tools, and a few lines of code. This guide shows the fastest path to a single-agent loop, the two agent types, and how to keep code execution safe.

What Smolagents is

Smolagents is an experimental library from Hugging Face for building agents in Python. An agent is a system that uses a language model as its engine. The model thinks, calls tools, reads the results and repeats until the task is done. Smolagents keeps that loop small. There is no heavy framework to learn. The library is open source and installs from PyPI.

Install it with:

pip install smolagents

You also need a model. Smolagents works with models through the Hugging Face Inference API, local transformers pipelines, Ollama or LiteLLM. The Hugging Face API is free to use without a token, with rate limits. A token raises those limits and unlocks gated models. Set the HF_TOKEN environment variable or pass the token when you create the model.

Your first single-agent loop

A minimal agent needs two arguments: a model and a list of tools. The tools list can be empty. Add the default toolbox with add_base_tools=True. That adds a web search tool, a Python interpreter tool and a few others.

from smolagents import CodeAgent, HfApiModel

model = HfApiModel(model_id="Qwen/Qwen2.5-72B-Instruct")
agent = CodeAgent(tools=[], model=model, add_base_tools=True)
agent.run("How many seconds would it take for a leopard at full speed to run through Pont des Arts?")

That is the whole loop. The agent plans a step, writes code, runs it, reads the result and continues. You can inspect what happened afterwards through agent.logs. The logs store each thought, tool call and result.

CodeAgent versus ToolCallingAgent

Smolagents provides two agent types. Both inherit from MultiStepAgent. Both run a ReAct loop: one thought, then one tool call and execution per step. The difference is how the model expresses its actions.

AgentHow actions are writtenBest forWatch out for
CodeAgentPython code snippetsMost tasks; the defaultExecutes code, so sandbox it
ToolCallingAgentJSON-like tool callsModels tuned for function callingNo code execution at all

CodeAgent is the default. The model writes its tool calls as Python code. It can combine several calls in one snippet, do arithmetic inline and store intermediate values in variables. That makes it flexible. ToolCallingAgent writes actions as JSON blobs instead. It works the same way otherwise, without executing code. Pick ToolCallingAgent when your model is strong at function calling but weak at writing code, or when you want no code execution at all.

Both agents accept a max_steps argument. The default is 20. Raise it for long tasks. Lower it to cap cost and runaway loops.

Tools: the agent's hands

A tool is an atomic function the agent can call. Each tool has a name, a description, typed inputs and an output type. The description is what the model reads to decide when to use it. Write clear descriptions. Vague tools get misused or ignored.

You can write your own tools in two ways. The simplest is the @tool decorator on a Python function. Smolagents also supports tools defined as classes with a forward method. Tools can also come from the Hugging Face Hub. The library loads shared tools by name, so you can reuse what others published.

The default toolbox covers common needs. It includes web search, a Python interpreter and image generation tools. Pass add_base_tools=True to include it on top of your own tools.

Managed agents: one agent calling another

A single agent can manage other agents. Pass them in the managed_agents argument. The parent agent then calls a managed agent as if it were a tool. This is the simplest form of multi-agent design in Smolagents. Use it when a task splits cleanly: one agent searches the web, another checks the facts, a third writes the summary.

Keep the split small. Two or three managed agents are enough for most jobs. Each managed agent needs its own name and description so the parent knows when to call it. The parent's instruction should say which agent handles which part.

Safe code execution

CodeAgent executes the code the model writes. By default it runs in your local environment. That is fast and simple. It is also risky. The model can generate arbitrary code. Treat that code as untrusted.

Smolagents limits the risk in three ways. The Python interpreter blocks imports outside a safe list. You can authorise extra imports with additional_authorized_imports. Execution stops on any illegal operation or Python error. For stronger isolation, run code in a sandbox instead. Set the E2B_API_KEY environment variable and pass use_e2b_executor=True. Docker and Modal executors are also supported. Never authorise unsafe imports. Never run a CodeAgent on a machine with secrets it does not need.

A planning interval helps too. Set planning_interval to make the agent stop and replan every few steps. That keeps long tasks on track and limits wasted calls.

Smolagents Hugging Face code agents compared with heavier frameworks

Smolagents trades control for speed. LangGraph gives you a full state machine and fine-grained control over every transition. Google ADK gives you hierarchies, workflow agents and managed deployment. Smolagents gives you a working agent in ten lines. That is the right trade for prototypes, scripts and single-purpose tools. It is the wrong trade for a production system that needs retries, human approval steps and audit trails.

The library is marked experimental by Hugging Face. APIs can change. Results vary with the underlying model. Pin your version in requirements and test after every upgrade.

Bottom line

Start with CodeAgent, HfApiModel and add_base_tools=True. Write one custom tool for your own data. Keep max_steps low while you test. Sandbox code execution before you run anything untrusted. When a single loop stops being enough, add managed agents one at a time. Smolagents earns its name: small, fast, and enough for most single-agent jobs.