smolagents

Agentic AIFrameworks and SDKsPublished By Simon Budziak

smolagents is a minimalist agent library from Hugging Face where the core agent logic fits in about a thousand lines of code, and its signature idea is the code agent: instead of emitting JSON tool calls one at a time, the model writes its actions as short Python snippets and executes them directly.

Why write actions as code instead of JSON?

Because programming languages express computer behavior better than message formats do. One generated block can hold a loop over search results or pass intermediate values between tools, work that plain tool calling must spread across many round trips with no shared state. Fewer model calls for the same task means less latency and fewer places to drift, which matters most when running capable open-weight models. The tradeoff is safety: generated code needs a sandboxed interpreter, making execution boundaries as important as content guardrails.

When is a minimal library the right choice?

For prototypes, education, and small agentic systems where you want to read every line running. It integrates tightly with the Hugging Face ecosystem while staying model-agnostic. Once a system needs durable state, retries across hours, or human checkpoints, that infrastructure belongs in a fuller agentic workflow runtime, and teams typically graduate to something like CrewAI or another orchestration layer rather than growing the thousand lines themselves. Pick smolagents when you want an agent small enough to fully understand.

Frequently asked questions

What is a CodeAgent in smolagents?

It is an agent whose actions are written as Python code rather than JSON function calls. The model composes loops, variables, and several tool invocations in one block, the sandbox executes it, and the result feeds the next reasoning step.

Does smolagents only work with Hugging Face models?

No. It runs any model on the Hugging Face Hub and reaches hosted providers such as OpenAI and Anthropic through LiteLLM integration, so the same tiny agent definition works across open and closed weights.

Summarize this page with

See this working in a system we built