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.
先审查再安装
这个资产需要先审查。复制的指令会要求 Agent dry-run、列出写入项,确认后再继续。
npx -y tokrepo@latest install c3ffefc8-dda1-4723-896e-d5c1b8422b41 --target codex先 dry-run,确认写入项后再运行此命令。
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.
常见问题
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.
引用来源 (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
来源与感谢
Created by HuggingFace. Licensed under Apache 2.0.
smolagents — ⭐ 15,000+
讨论
相关资产
Smolagents — Lightweight AI Agent Framework
Hugging Face minimalist agent framework. Build AI agents in ~1000 lines of code with code-based actions, tool calling, and multi-step reasoning.
CAMEL — Multi-Agent Framework at Scale
CAMEL is a multi-agent framework for studying scaling laws of AI agents. 16.6K+ GitHub stars. Up to 1M agents, RAG, memory systems, data generation. Apache 2.0.
Feathers — Lightweight Real-Time API Framework for Node.js
Feathers is a lightweight web framework for building real-time applications and REST APIs with Node.js. It provides a service-oriented architecture that works with Express, Koa, or its own HTTP transport.
Javalin — Simple Lightweight Web Framework for Java and Kotlin
Javalin is a lightweight web framework for Java and Kotlin built on top of Jetty, designed for simplicity with a small learning curve and first-class support for both languages.