Smolagents Tutorial Beginners 2026
smolagents lets the model write Python as its action: one decorator defines a tool, ten lines run the first task. Setup, local models, tool design, sandboxing and cost control explained.
📜 Table of Contents
📖 Keep reading
Smolagents Tutorial for Beginners 2026
Most agent frameworks teach the model to emit JSON tool calls. smolagents takes another route: the model writes Python, and that code calls your functions. Loops, conditionals and combined calls become ordinary code instead of a bespoke DSL - at the price of executing generated code locally, which means sandboxing is not optional.
Install
pip install -U smolagents
pip install -U "smolagents[litellm]" # for cloud or local model backends
First agent
from smolagents import CodeAgent, InferenceClientModel
model = InferenceClientModel()
agent = CodeAgent(tools=[], model=model)
print(agent.run("Explain vector databases in three sentences"))
The agent writes code, runs it, reads the output and decides the next step, bounded by a max-step limit. Older tutorials use HfApiModel; it is now InferenceClientModel.
Adding tools
Tools are plain functions with @tool, type hints and a docstring - the docstring is the description the model reads, so vague docs mean wrong calls.
from smolagents import tool
@tool
def convert_currency(amount: float, rate: float) -> float:
'''Convert an amount by an exchange rate.
Args:
amount: Original amount.
rate: Target rate.
'''
return round(amount * rate, 2)
One tool, one job. Handle errors inside the tool and return a readable message.
Local models
LiteLLMModel for Ollama, OpenAIServerModel for any OpenAI-compatible endpoint (vLLM, LM Studio, llama-server), TransformersModel for raw weights. A 7B can handle one tool and two or three steps; multi-tool tasks need a bigger model or fewer tools.
CodeAgent vs ToolCallingAgent
| CodeAgent | ToolCallingAgent | |
|---|---|---|
| Action | Python code | JSON tool call |
| Composition | Loops, branches | One call per step |
| Steps used | Fewer | More |
| Risk | Higher - local execution | Lower |
| Best model | Strong coder | Trained for tool use |
If the task says "do this for every item in a list", use CodeAgent. Otherwise prefer the safer option.
Security
Restrict imports with additional_authorized_imports, move execution into Docker or E2B for production, and cap max_steps with an outer timeout - an infinite loop can burn a day of API budget.
Multi-agent
Pass sub-agents as managed_agents with a name and description; the manager decides delegation from those two fields, so write them like a job description.
Cost and debugging
Watch the per-step trace, set planning_interval to replan every few steps on long tasks, and truncate tool outputs to a few hundred characters - that is the single biggest token saving.
FAQ
ImportError on HfApiModel? Renamed to InferenceClientModel.
Model never calls my tool? The docstring is too thin or types are missing.
Can I use a local 7B? Yes, but validate the flow on a cloud model first.
Related
Written by our editorial team; tools listed here are tested or verified against public sources. Links point to official sites or GitHub repos for reference only — no paid placements.