# Smolagents

**Smolagents** 是 [Hugging Face](https://www.edgepedia.org/zh/hugging-face) 推出的一个轻量级开源 Python 库，用于构建 AI 智能体——即由大语言模型的输出控制工具调用工作流的程序。其标志性设计是 CodeAgent，一种将动作写成可执行 Python 代码而非 JSON 工具调用数据块的智能体，同时还提供输出 JSON 的传统 ToolCallingAgent。该库于 2024 年 12 月 31 日发布，采用 Apache 2.0 许可证，在 GitHub 上已获得约 29,800 颗星和 3,100 次复刻。<sup>[1](https://huggingface.co/blog/smolagents)</sup><sup> • </sup><sup>[2](https://github.com/huggingface/smolagents)</sup>

| 关键事实 | 详情 |
|---|---|
| 开发者与许可证 | Hugging Face；Apache 2.0<sup>[2](https://github.com/huggingface/smolagents)</sup> |
| 发布时间 | 2024 年 12 月 31 日，作为 `transformers.agents` 框架的独立后继版本<sup>[1](https://huggingface.co/blog/smolagents)</sup><sup> • </sup><sup>[3](https://huggingface.co/blog/beating-gaia)</sup> |
| 智能体类型 | `CodeAgent`（将动作写成 Python 代码）和 `ToolCallingAgent`（写出 JSON 工具调用），二者均构建在单一的 `MultiStepAgent` 类之上<sup>[4](https://huggingface.co/docs/hub/main/agents-libraries)</sup><sup> • </sup><sup>[5](https://huggingface.co/docs/smolagents/v1.9.0/conceptual_guides/react)</sup> |
| 架构 | 多步 ReAct 循环：每一步包含一个推理阶段和一个动作阶段，补全结果被解析为代码片段或 JSON 数据块<sup>[5](https://huggingface.co/docs/smolagents/v1.9.0/conceptual_guides/react)</sup> |
| 模型支持 | 通过 `TransformersModel`、`InferenceClientModel`、`LiteLLMModel`、`OpenAIServerModel` 和 `AzureOpenAIServerModel` 支持任意 LLM<sup>[6](https://huggingface.co/learn/agents-course/unit2/smolagents/why_use_smolagents)</sup> |
| 代码执行 | 默认使用基于抽象语法树的自定义 `LocalPythonExecutor`；可为 E2B、Modal、Docker、Blaxel 和 WebAssembly 提供远程沙箱<sup>[7](https://huggingface.co/docs/smolagents/en/tutorials/secure_code_execution)</sup><sup> • </sup><sup>[8](https://github.com/huggingface/smolagents/blob/main/SECURITY.md)</sup> |
| 最新版本 | v1.26.0，5 月 29 日发布<sup>[9](https://github.com/huggingface/smolagents/releases)</sup> |

## 为何采用代码动作

大多数智能体框架（包括 [Anthropic](https://www.edgepedia.org/zh/anthropic) 和 OpenAI 的框架）让模型将动作写成由工具名称和参数构成的 JSON 对象，然后由框架解析以决定执行什么。Smolagents 则让模型编写直接调用工具的 Python 代码，因此输出无需解析步骤即可执行。<sup>[10](https://huggingface.co/docs/smolagents/v1.9.2/en/conceptual_guides/intro_agents)</sup><sup> • </sup><sup>[6](https://huggingface.co/learn/agents-course/unit2/smolagents/why_use_smolagents)</sup>

其理据源自 CodeAct 研究路线，即[伊利诺伊大学厄巴纳-香槟分校](https://www.edgepedia.org/zh/university-of-illinois-urbana-champaign)的 Xingyao Wang 及其同事在 ICML 2024 上发表的论文《可执行代码动作能激发更好的 LLM 智能体》，该论文主张代码是比 JSON 更自然的 LLM 智能体动作空间。代码具有可组合性，因为动作可以嵌套或作为函数复用；对象管理更简便，因为生成的图像或其他输出可以直接赋值给变量；具有通用性，因为代码可以表达计算机能做的任何事情；并且在 LLM 训练数据中具有良好的代表性，因为训练数据中本就包含大量高质量代码。<sup>[10](https://huggingface.co/docs/smolagents/v1.9.2/en/conceptual_guides/intro_agents)</sup><sup> • </sup><sup>[11](https://arxiv.org/abs/2402.01030)</sup> Hugging Face 自己的基准测试总结报告称，代码动作所需的步骤比 JSON 少 30%，生成的词元数量也同样减少 30%。<sup>[3](https://huggingface.co/blog/beating-gaia)</sup>

这种方法存在一种可测量的失败模式。Hugging Face 的结构化生成研究发现，当无法从 LLM 输出中提取 Python 代码时，第一步的解析错误会使智能体成功率降低 21.3%，并将平均步骤数从 3.18 提高到 4.63，这促使研究者致力于将代码生成约束为有效语法。<sup>[12](https://github.com/huggingface/blog/blob/main/structured-codeagent.md)</sup>

## 智能体循环的工作方式

所有 smolagents 智能体都派生自同一个 `MultiStepAgent` 类，该类对 ReAct 框架进行了抽象——在 ReAct 框架中，模型交替进行推理和行动。每一步包含一个推理阶段，然后是一个动作阶段，模型在动作阶段中构思推动任务解决的工具调用。系统解析补全结果以获取动作——对于 `ToolCallingAgent` 是 JSON 数据块，对于 `CodeAgent` 是代码片段——执行该动作，并将结果作为下一步的新提示词反馈回去，同时保留先前迭代的记忆。<sup>[5](https://huggingface.co/docs/smolagents/v1.9.0/conceptual_guides/react)</sup><sup> • </sup><sup>[10](https://huggingface.co/docs/smolagents/v1.9.2/en/conceptual_guides/intro_agents)</sup>

**工具与模型。** 工具通过包裹 Python 函数的 `@tool` 装饰器定义，或通过 `Tool` 类定义。模型连接通过若干预定义类提供：`TransformersModel` 用于本地 `transformers` 流水线，`InferenceClientModel` 用于 Hugging Face 无服务器推理和第三方推理提供商，`LiteLLMModel` 用于访问众多云提供商，`OpenAIServerModel` 用于任何兼容 OpenAI 的 API，`AzureOpenAIServerModel` 用于 Azure 部署。<sup>[6](https://huggingface.co/learn/agents-course/unit2/smolagents/why_use_smolagents)</sup> 该库还与 MCP 服务器集成，使其智能体可以使用外部工具服务器；智能体可以作为 Spaces 推送到 [Hugging Face Hub](https://www.edgepedia.org/zh/hugging-face-hub) 并从其中加载。<sup>[4](https://huggingface.co/docs/hub/main/agents-libraries)</sup>

**规模。** Hugging Face 称智能体逻辑约在一千行代码之内，抽象层保持在原始代码之上的最小形态。<sup>[13](https://huggingface.co/docs/smolagents/v1.10.0/index)</sup> 一份针对 1.26.0 版本的独立源码审查测量出，完整的 pip 包包含 12 个文件、共 13,355 行 Python 代码，其中仅 `agents.py` 就有 1,813 行，并得出结论：“约 1,000 行”指的是核心智能体循环，而非整个包。<sup>[14](https://benchclaw.io/smolagents-review/)</sup>

## 安全的代码执行

由于 CodeAgent 执行模型编写的代码，安全性是核心设计关切。smolagents 中的代码执行不使用原版 Python 解释器：内置的 `LocalPythonExecutor` 加载生成代码的抽象语法树，并在一组规则下逐操作执行。除非明确添加到授权列表，否则禁止导入；默认禁用对子模块的访问（可使用 `numpy.*` 等模式授权某个包及其子包）；基本操作的总数设有上限，以防止无限循环和资源耗尽；自定义解释器中未定义的任何操作都会引发错误。在文档示例中，一个 `while True: pass` 循环在最多 1,000,000 次迭代后被中断。<sup>[7](https://huggingface.co/docs/smolagents/en/tutorials/secure_code_execution)</sup>

Hugging Face 明确指出，这个本地执行器并非安全沙箱：坚定的攻击者或经过微调的恶意模型仍可能找到漏洞，例如通过 Pillow 等已授权包生成数千个大文件来填满磁盘。对于不受信任的代码，该项目建议使用远程沙箱执行。文档中记录的选项包括 E2B 沙箱、Modal 沙箱、隔离的 Docker 容器，以及使用 Pyodide 和 Deno 安全运行时的 [WebAssembly](https://www.edgepedia.org/zh/webassembly) 沙箱；教程还描述了通过 `executor_type="blaxel"` 选择的 Blaxel 沙箱。支持两种沙箱策略：仅在沙箱中运行生成的代码片段，而系统其余部分保留在本地——这种方式更简单，但不支持多智能体设置；或者将整个智能体系统（包括模型和工具）都放在沙箱内运行，以获得更强的隔离，代价是需要将凭证传入沙箱。<sup>[8](https://github.com/huggingface/smolagents/blob/main/SECURITY.md)</sup><sup> • </sup><sup>[7](https://huggingface.co/docs/smolagents/en/tutorials/secure_code_execution)</sup>

## 生态角色与评价

Smolagents 为 Hugging Face 的智能体课程提供支持，并被定位为该公司智能体技术栈中最简单的入口，与 `transformers` 和 `datasets` 并列。<sup>[6](https://huggingface.co/learn/agents-course/unit2/smolagents/why_use_smolagents)</sup><sup> • </sup><sup>[15](https://andrew.ooo/posts/smolagents-huggingface-code-first-agent-library/)</sup> 命令行界面 `smolagent` 通过一条命令，用提示词、模型标识符和工具列表启动智能体。<sup>[4](https://huggingface.co/docs/hub/main/agents-libraries)</sup> 该库的开源深度研究实现已被用作学术智能体评估的基线，包括在 HAL 评估项目中作为通用基线脚手架。<sup>[16](https://www.researchgate.net/publication/405371282_A_Unified_Framework_for_the_Evaluation_of_LLM_Agentic_Capabilities/fulltext/6a17b29ccd020102aaef07a9/A-Unified-Framework-for-the-Evaluation-of-LLM-Agentic-Capabilities.pdf?origin=scientificContributions)</sup>

独立评测认为，该库适合快速原型开发和单智能体 Python 脚本，而非生产工作流运行时：1.26.0 版本没有内置检查点，没有进程崩溃后的原生可恢复性，也没有结构化并发模型。评测者还指出，代码智能体模式对能力强的模型更有利，因为参数量低于约 70 亿的小模型往往会写出格式错误的 Python；默认本地执行器可能会误导那些忽视安全警告的新手。<sup>[14](https://benchclaw.io/smolagents-review/)</sup><sup> • </sup><sup>[15](https://andrew.ooo/posts/smolagents-huggingface-code-first-agent-library/)</sup> 在社区框架比较中，smolagents 始终被归类为轻量级、代码优先的选项；在一项由社区维护的工具使用智能体基准测试中，其测得延迟为 910 毫秒（四分位距 720 至 1,300 毫秒），任务成功率为 88%（83% 至 93%）。<sup>[17](https://agentsdk.dev/benchmarks)</sup>

## References

1. [Introducing smolagents: simple agents that write actions in code, Hugging Face Blog](https://huggingface.co/blog/smolagents)
2. [huggingface/smolagents GitHub repository](https://github.com/huggingface/smolagents)
3. [Our Transformers Code Agent beats the GAIA benchmark, Hugging Face Blog](https://huggingface.co/blog/beating-gaia)
4. [Agent Libraries, Hugging Face Documentation](https://huggingface.co/docs/hub/main/agents-libraries)
5. [How do multi-step agents work?, Hugging Face Documentation](https://huggingface.co/docs/smolagents/v1.9.0/conceptual_guides/react)
6. [Why use smolagents, Hugging Face Agents Course](https://huggingface.co/learn/agents-course/unit2/smolagents/why_use_smolagents)
7. [Secure code execution, Hugging Face Documentation](https://huggingface.co/docs/smolagents/en/tutorials/secure_code_execution)
8. [smolagents SECURITY.md, GitHub](https://github.com/huggingface/smolagents/blob/main/SECURITY.md)
9. [Releases, huggingface/smolagents, GitHub](https://github.com/huggingface/smolagents/releases)
10. [Introduction to Agents, Hugging Face Documentation](https://huggingface.co/docs/smolagents/v1.9.2/en/conceptual_guides/intro_agents)
11. [Executable Code Actions Elicit Better LLM Agents (CodeAct), arXiv](https://arxiv.org/abs/2402.01030)
12. [structured-codeagent.md, Hugging Face Blog repository](https://github.com/huggingface/blog/blob/main/structured-codeagent.md)
13. [smolagents documentation, v1.10.0](https://huggingface.co/docs/smolagents/v1.10.0/index)
14. [smolagents Review: Real LOC Count & Agent Types Tested, BenchClaw](https://benchclaw.io/smolagents-review/)
15. [smolagents Review: Hugging Face's Code-First Agent Library, andrew.ooo](https://andrew.ooo/posts/smolagents-huggingface-code-first-agent-library/)
16. [A Unified Framework for the Evaluation of LLM Agentic Capabilities](https://www.researchgate.net/publication/405371282_A_Unified_Framework_for_the_Evaluation_of_LLM_Agentic_Capabilities/fulltext/6a17b29ccd020102aaef07a9/A-Unified-Framework-for-the-Evaluation-of-LLM-Agentic-Capabilities.pdf?origin=scientificContributions)
17. [Agent Framework Benchmarks, AgentSDK](https://agentsdk.dev/benchmarks)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 现代人工智能：基础模型、生成式人工智能与人工智能产业 › AI companies, people, and products › AI products and assistants › AI agents and developer tools

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/smolagents

English original: https://www.edgepedia.org/smolagents
