$ show huggingface/smolagents
smolagents is a HuggingFace library for building LLM-powered agents, with a strong emphasis on 'code agents' that generate and execute Python code as actions. The core architecture consists of a ReAct-style loop (MultiStepAgent), with two main implementations: CodeAgent (writes Python code) and ToolCallingAgent (writes JSON tool calls). Agents are model-agnostic, supporting dozens of backends (OpenAI, LiteLLM, Hugging Face Inference API, local Transformers, vLLM, MLX, Bedrock, etc.) via a clean Model abstraction. Tools are defined as Python classes with typed inputs/outputs and automatic JSON schema generation, or via a simple @tool decorator, and can be shared on the HuggingFace Hub. The library includes a built-in AST-based Python executor (LocalPythonExecutor) for safe agent code execution, though it is explicitly not a security boundary; production use is directed toward sandboxed remote executors (E2B, Docker, Modal, Blaxel). Memory is managed through structured step objects (ActionStep, PlanningStep, TaskStep) with callbacks for customization. Additional features include MCP client integration, Gradio-based interactive UI, CLI tools, telemetry via OpenTelemetry, and serialization with SafeSerializer.
The LLM identified 5 files worth highlighting for reuse:
Provides the foundational `Tool` base class and `@tool` decorator for defining tools with automatic JSON schema generation from type hints. Tools are self-contained: they have name, description, inputs, output_type, and a forward method. The pattern is highly reusable for any agent framework needing standardized tool interfaces.
Implements a custom Python interpreter that evaluates AST nodes directly instead of using `eval`/`exec`. Includes import whitelisting, dangerous function blocking, operation counting, and timeouts. This pattern can be extracted and adapted for any application needing restricted, sandboxed execution of untrusted Python code.
Defines `AgentMemory` with a sequence of `MemoryStep` objects (ActionStep, PlanningStep, TaskStep) and a `CallbackRegistry` that fires typed callbacks on each step. This pattern provides structured, inspectable agent history and can be reused for any system needing replayable, hookable execution traces.
Implements `SafeSerializer` that converts arbitrary Python objects (numpy, PIL, dataclasses) to JSON-safe dicts with type markers, using a prefix protocol ('safe:' vs 'pickle:') that avoids pickle by default. The extensible type-converter pattern is reusable for any project needing robust serialization of complex objects without introducing security holes.
Wraps Model Context Protocol (MCP) servers into SmolAgents tools, supporting Stdio, Streamable HTTP, and SSE transports. Includes structured output support and a FutureWarning for a planned default change. The adapter pattern cleanly separates MCP protocol details from the tool interface, making it reusable for integrating other external tool protocols.