smolagents
A barebones library for agents that think in code
About
smolagents is Hugging Face's minimal Python library for building agents in a few lines of code. Its headline idea is the CodeAgent: instead of emitting JSON tool calls, the agent writes its actions as Python code, which composes naturally with loops, conditionals, and function nesting; a classic ToolCallingAgent covers JSON/text tool-calling when that paradigm fits better.
It is model-agnostic (Hugging Face Inference providers, OpenAI, Anthropic, 100+ providers via LiteLLM, or local models via Transformers/Ollama), tool-agnostic (tools from any MCP server, from LangChain, or even a Hub Space as a tool), and modality-agnostic (text, vision, video, audio). Code execution can run locally in a restricted interpreter or in a remote sandbox (Modal, Blaxel, E2B, Docker), and it ships CLI utilities (smolagent, webagent) for running agents without boilerplate.
What it can do
- • CodeAgent: agents act by writing Python code (natural loops, conditionals, composability) • ToolCallingAgent: classic JSON/text tool-calling • Model-agnostic: HF Inference providers, OpenAI, Anthropic, 100+ via LiteLLM, local via Transformers/Ollama • Tool-agnostic: MCP servers, LangChain tools, Hub Spaces as tools • Vision, video, and audio inputs • Share and load agents/tools to/from the Hugging Face Hub as Gradio Spaces • CLI tools (smolagent, webagent) for zero-boilerplate runs • Sandboxed code execution via Modal, Blaxel, E2B, or Docker • Multi-agent systems and managed agents
Setup
- Install with the default toolkit:
pip install 'smolagents[toolkit]'(includes default tools like web search) - Authenticate to Hugging Face (set
HF_TOKEN/ log in withhuggingface-cli) if you use the hosted Inference API; for other providers, have your API keys ready for LiteLLM - Create your first agent per the docs:
from smolagents import CodeAgent, InferenceClientModel→agent = CodeAgent(tools=[], model=InferenceClientModel())→agent.run("your task") - Add tools, e.g.
DuckDuckGoSearchTool(), or point at other models:LiteLLMModelfor OpenAI/Anthropic (needssmolagents[litellm]),TransformersModelfor local models (needssmolagents[transformers]) - If the agent will run untrusted or production workloads, switch code execution to a sandboxed backend (Modal, Blaxel, E2B, or Docker) instead of the default local interpreter
Prerequisites
Python 3.10+; pip; a Hugging Face token for the Inference API, or API keys for OpenAI/Anthropic/etc. via LiteLLM; Docker or a Modal/E2B/Blaxel account only if you want remote sandboxed execution.
Use cases
Works with
Trust score — 8/10
Scored 8/10 (verified tier, metrics-v1). Evidence: 29,800 GitHub stars (log-scaled vs catalog peers); blended with ~425,605 monthly package downloads; last push 6d ago; 4 releases/12mo; 924 open issues. Manual review: CodeAgent executes LLM Python — sandbox recommended; no built-in telemetry found. License: Apache-2.0.
★ 29,800 GitHub stars
How the trust score is built
Full methodology →- Adoption · stars/downloads, log-scaled vs peers
- 8.3/10
- Maintenance · recency, releases, issue load
- 7.6/10
- Openness · license + docs
- 10.0/10
- Manual review · sanity read + red-flag check
- 8.0/10
- · 29,800 GitHub stars (log-scaled vs catalog peers)
- · blended with ~425,605 monthly package downloads
- · last push 6d ago; 4 releases/12mo; 924 open issues
- · manual review: CodeAgent executes LLM Python — sandbox recommended; no built-in telemetry found
Sources
Every fact above was checked against these sources. Where a claim comes from the provider and couldn't be independently verified, the text says so.