Support huggingface-hub >= 1 (#2075)
Co-authored-by: Julien Chaumond julien@huggingface.co Co-authored-by: Claude Agents+claude@huggingface.co Co-authored-by: Albert Villanova del Moral 8515462+albertvillanova@users.noreply.github.com
版权所有:中国计算机学会技术支持:开源发展技术委员会
京ICP备13000930号-9
京公网安备 11010802047560号
Agents that think in code!
smolagentsis a library that enables you to run powerful agents in a few lines of code. It offers:✨ Simplicity: the logic for agents fits in ~1,000 lines of code (see agents.py). We kept abstractions to their minimal shape above raw code!
🧑💻 First-class support for Code Agents. Our
CodeAgentwrites its actions in code (as opposed to “agents being used to write code”). To make it secure, we support executing in sandboxed environments via Blaxel, E2B, Modal, Docker, or Pyodide+Deno WebAssembly sandbox.🤗 Hub integrations: you can share/pull tools or agents to/from the Hub for instant sharing of the most efficient agents!
🌐 Model-agnostic: smolagents supports any LLM. It can be a local
transformersorollamamodel, one of many providers on the Hub, or any model from OpenAI, Anthropic and many others via our LiteLLM integration.👁️ Modality-agnostic: Agents support text, vision, video, even audio inputs! Cf this tutorial for vision.
🛠️ Tool-agnostic: you can use tools from any MCP server, from LangChain, you can even use a Hub Space as a tool.
Full documentation can be found here.
Quick demo
First install the package with a default set of tools:
Then define your agent, give it the tools it needs and run it!
https://github.com/user-attachments/assets/84b149b4-246c-40c9-a48d-ba013b08e600
You can even share your agent to the Hub, as a Space repository:
Our library is LLM-agnostic: you could switch the example above to any inference provider.
InferenceClientModel, gateway for all inference providers supported on HF
LiteLLM to access 100+ LLMs
OpenAI-compatible servers: Together AI
OpenAI-compatible servers: OpenRouter
Local `transformers` model
Azure models
Amazon Bedrock models
CLI
You can run agents from CLI using two commands:
smolagentandwebagent.smolagentis a generalist command to run a multi-stepCodeAgentthat can be equipped with various tools.Interactive mode guides you through:
Meanwhile
webagentis a specific web-browsing agent using helium (read more here).For instance:
How do Code agents work?
Our
CodeAgentworks mostly like classical ReAct agents - the exception being that the LLM engine writes its actions as Python code snippets.flowchart TB Task[User Task] Memory[agent.memory] Generate[Generate from agent.model] Execute[Execute Code action - Tool calls are written as functions] Answer[Return the argument given to 'final_answer'] Task -->|Add task to agent.memory| Memory subgraph ReAct[ReAct loop] Memory -->|Memory as chat messages| Generate Generate -->|Parse output to extract code action| Execute Execute -->|No call to 'final_answer' tool => Store execution logs in memory and keep running| Memory end Execute -->|Call to 'final_answer' tool| Answer %% Styling classDef default fill:#d4b702,stroke:#8b7701,color:#ffffff classDef io fill:#4a5568,stroke:#2d3748,color:#ffffff class Task,Answer ioActions are now Python code snippets. Hence, tool calls will be performed as Python function calls. For instance, here is how the agent can perform web search over several websites in one single action:
Writing actions as code snippets is demonstrated to work better than the current industry practice of letting the LLM output a dictionary of the tools it wants to call: uses 30% fewer steps (thus 30% fewer LLM calls) and reaches higher performance on difficult benchmarks. Head to our high-level intro to agents to learn more on that.
Since code execution can be a serious security concern (arbitrary code execution!), you should run agent code in a sandbox. We support several options:
The built-in
LocalPythonExecutoris not a security sandbox. It applies some restrictions but can be bypassed and must not be used as a security boundary.Alongside
CodeAgent, we also provide the standardToolCallingAgentwhich writes actions as JSON/text blobs. You can pick whichever style best suits your use case.How smol is this library?
We strived to keep abstractions to a strict minimum: the main code in
agents.pyhas <1,000 lines of code. Still, we implement several types of agents:CodeAgentwrites its actions as Python code snippets, and the more classicToolCallingAgentleverages built-in tool calling methods. We also have multi-agent hierarchies, import from tool collections, remote code execution, vision models…By the way, why use a framework at all? Well, because a big part of this stuff is non-trivial. For instance, the code agent has to keep a consistent format for code throughout its system prompt, its parser, the execution. So our framework handles this complexity for you. But of course we still encourage you to hack into the source code and use only the bits that you need, to the exclusion of everything else!
How strong are open models for agentic workflows?
We’ve created
CodeAgentinstances with some leading models, and compared them on this benchmark that gathers questions from a few different benchmarks to propose a varied blend of challenges.Find the benchmarking code here for more detail on the agentic setup used, and see a comparison of using LLMs code agents compared to vanilla (spoilers: code agents works better).
This comparison shows that open-source models can now take on the best closed models!
Security
Security is a critical consideration when working with code-executing agents. Ensure you are using one of the sandboxed execution options that provide isolation from untrusted code.
Warning:
LocalPythonExecutorprovides best-effort mitigations only and is not a security boundary. Do not use it to run untrusted code.For security policies, vulnerability reporting, and more information on secure agent execution, please see our Security Policy.
Contribute
Everyone is welcome to contribute, get started with our contribution guide.
Cite smolagents
If you use
smolagentsin your publication, please cite it by using the following BibTeX entry.