ScriptsApr 6, 2026·2 min read

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.

TL;DR
Smolagents lets you build AI agents with tool calling in under 30 lines of Python code.
§01

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.

§02

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.

§03

How to use

  1. Install smolagents: pip install smolagents.
  2. Import the agent class and define your tools as Python functions with docstrings.
  3. Create a CodeAgent or ToolCallingAgent and call agent.run('your task').
§04

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)
§05

Related on TokRepo

§06

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

What is the difference between CodeAgent and ToolCallingAgent?+

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.

Does smolagents work with models other than HuggingFace?+

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.

How does smolagents compare to LangChain or CrewAI?+

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.

Can smolagents handle multi-step tasks?+

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.

Is smolagents production-ready?+

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)
🙏

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

Sign in to join the discussion.
No comments yet. Be the first to share your thoughts.

Related Assets