Smolagents — Lightweight Agent Framework by HuggingFace
Minimalist Python agent framework by HuggingFace. Build agents with tool use in under 30 lines of code. Supports code-based agents that write and execute Python instead of JSON tool calls. 15,000+ stars.
What it is
Smolagents is a lightweight Python agent framework by HuggingFace. It provides a minimalist API for building AI agents that can use tools, write and execute code, and perform multi-step reasoning. The entire framework is roughly 1,000 lines of code, making it easy to understand and extend.
The project targets developers who want functional AI agents without the complexity of larger frameworks. If you need tool-calling agents that reason in code rather than JSON, smolagents is a focused choice.
The project is actively maintained with regular releases and a growing user community. Documentation covers common use cases, and the open-source nature means you can inspect the source code, contribute fixes, and adapt the tool to your specific requirements.
How it saves time or tokens
Smolagents uses code-based agent actions instead of JSON tool schemas. The agent writes Python code to call tools, which is more expressive and uses fewer tokens than verbose JSON function-calling formats. The minimal abstraction layer also means less framework overhead and faster iteration.
How to use
- Install smolagents:
pip install smolagents. - Import the agent class and define your tools as Python functions with docstrings.
- Create a
CodeAgentorToolCallingAgentand callagent.run('your task').
Example
from smolagents import CodeAgent, tool, HfApiModel
@tool
def get_weather(city: str) -> str:
'''Get current weather for a city.'''
return f'Weather in {city}: 22C, sunny'
agent = CodeAgent(
tools=[get_weather],
model=HfApiModel('Qwen/Qwen2.5-72B-Instruct')
)
result = agent.run('What is the weather in Paris?')
print(result)
Related on TokRepo
- AI Tools for Agents -- compare agent frameworks for building autonomous AI systems
- Multi-Agent Frameworks -- explore multi-agent orchestration options
Common pitfalls
- CodeAgent executes Python code generated by the LLM. Always run it in a sandboxed environment for untrusted inputs.
- Smolagents requires models that follow instruction format well. Smaller models may struggle with the code generation step.
- The framework is intentionally minimal. If you need persistence, memory, or complex orchestration, consider combining smolagents with dedicated memory or state management libraries.
Before adopting this tool, evaluate whether it fits your team's existing workflow. Read the official documentation thoroughly, and start with a small proof-of-concept rather than a full migration. Community forums, GitHub issues, and Stack Overflow are valuable resources when you encounter edge cases not covered in the documentation.
Frequently Asked Questions
CodeAgent generates Python code to call tools, which is more flexible and token-efficient. ToolCallingAgent uses the standard JSON function-calling format supported by most LLM APIs. Choose CodeAgent for expressiveness, ToolCallingAgent for API compatibility.
Yes. Smolagents supports any LLM through adapter classes. You can use OpenAI, Anthropic, or local models by wrapping them in the appropriate model class. HfApiModel is just the default.
Smolagents is deliberately minimal at about 1,000 lines of code. LangChain and CrewAI are full-featured frameworks with more abstractions. Smolagents trades features for simplicity and transparency.
Yes. The agent loops through plan-act-observe cycles. It generates code, executes it, observes the output, and decides whether to continue or return a final answer. Multi-step tasks are the default behavior.
Smolagents is suitable for production use cases with appropriate sandboxing. HuggingFace maintains it as an official project. For high-stakes applications, add input validation and execution timeouts.
Citations (3)
- Smolagents GitHub— Minimalist agent framework by HuggingFace
- Smolagents Docs— Code-based agent actions for tool calling
- HuggingFace Blog— Agents as code-writing reasoning systems
Related on TokRepo
Source & Thanks
Created by HuggingFace. Licensed under Apache 2.0.
smolagents — ⭐ 15,000+
Thanks to HuggingFace for proving agents do not need to be complicated.
Discussion
Related Assets
NAPI-RS — Build Node.js Native Addons in Rust
Write high-performance Node.js native modules in Rust with automatic TypeScript type generation and cross-platform prebuilt binaries.
Mamba — Fast Cross-Platform Package Manager
A drop-in conda replacement written in C++ that resolves environments in seconds instead of minutes.
Plasmo — The Browser Extension Framework
Build, test, and publish browser extensions for Chrome, Firefox, and Edge using React or Vue with hot-reload and automatic manifest generation.