Smolagents AI Agent Framework

 HuggingFace’s NEW Agent Framework ,Create Powerful AI Agents with Minimal Effort

SmolAgents - Powerful AI agents, minimal code, on Hugging Face framework | Product Hunt

Deep Research build By Smolagents FREE

Powered By Smolagents

Ai code Smolagents | FAST&FREE

Powered By Together AI and LlamaCoder

smolagents and tools gallery

best smolagents in huggingface

 What are agents?

AI Agents are programs where LLM outputs control the workflow.

The table illustrates how agency varies across systems:

Agency LevelDescriptionHow that’s calledExample Pattern
☆☆☆LLM output has no impact on program flowSimple processorprocess_llm_output(llm_response)
★☆☆LLM output determines basic control flowRouterif llm_decision(): path_a() else: path_b()
★★☆LLM output determines function executionTool callrun_function(llm_chosen_tool, llm_chosen_args)
★★★LLM output controls iteration and program continuationMulti-step Agentwhile llm_should_continue(): execute_next_step()
★★★One agentic workflow can start another agentic workflowMulti-Agentif llm_trigger(): execute_agent()

Feature of SmolAgents

🤗 Smolagents is a minimalist AI agent framework developed by the Hugging Face team, crafted to enable developers to deploy robust agents with just a few lines of code. Embracing simplicity and efficiency, smolagents empowers large language models (LLMs) to interact seamlessly with the real world.

✨ Simplicity

Compact Codebase: With approximately 1,000 lines of core code in agents.py, smolagents keeps abstractions to a minimum for straightforward development.

User-Friendly

Developers can swiftly define agents, supply the necessary tools, and run them immediately without intricate configurations.

Code Agents

 smolagents focuses on code agents, where agents write and execute Python code snippets to perform actions, instead of generating actions as JSON or text blobs.

High Efficiency

Compared to standard tool-calling methods, code agents offer enhanced efficiency and accuracy, reducing steps and LLM calls by about 30%, and achieving superior performance on complex benchmarks.

Secure Execution

 To ensure safe code execution, smolagents supports running code in sandboxed environments like E2B, providing a protected and isolated execution space.

Various LLMs

 smolagents integrates effortlessly with any large language model, including models hosted on the Hugging Face Hub via Transformers, as well as models from OpenAI, Anthropic, and others through LiteLLM integration.

SmolAgents :Integration with Hugging Face Hub

Share and Load Tools: Deep integration with the Hugging Face Hub allows developers to easily share and import tools, fostering collaboration and sharing within the community.

Ecosystem Growth: Leveraging the Hub’s capabilities, smolagents continually expands with more functionalities and tools, offering limitless potential for development.

Support for Traditional Tool-Calling Agents

Variety of Choices: In addition to code agents, smolagents also supports traditional tool-calling agents, where actions are written as JSON or text blocks, suitable for specific scenarios and requirements.

Reasons to Opt for Code smolAgents

Superior Composability: Code naturally supports function nesting and reuse, allowing for the expression of complex logic.

Efficient Object Handling: Compared to JSON, code simplifies the management and transfer of objects.

Ultimate Flexibility: Code can represent any operation a computer can perform, providing immense versatility.

Extensive Training Data: LLMs have been trained on vast amounts of high-quality code, enhancing their ability to generate and comprehend code.

Performance Benefits of smolagents

In various benchmarks, smolagents utilizing open-source models have matched the performance of agents using proprietary models. Thanks to the efficiency of code agents and the continuous advancements of open-source models, developers can build powerful agent systems without relying on closed-source services.

Application example of SmolAgents

In Smolagents, agents are encapsulated in classes that allow them to perform specific tasks. For instance, a ManagedAgent can be created to manage various tasks by utilizing different agent types. Below is a simplified example of how two agents—one for web searching and another for image generation—can be implemented,Here’s how to make a custom tool that gets travel times from Google Maps, and how to use it into a travel planner agent:

from typing import Optional
from smolagents import CodeAgent, HfApiModel, tool

@tool
def get_travel_duration(start_location: str, destination_location: str, transportation_mode: Optional[str] = None) -> str:
    """Gets the travel time between two places.

    Args:
        start_location: the place from which you start your ride
        destination_location: the place of arrival
        transportation_mode: The transportation mode, in 'driving', 'walking', 'bicycling', or 'transit'. Defaults to 'driving'.
    """
    import os   # All imports are placed within the function, to allow for sharing to Hub.
    import googlemaps
    from datetime import datetime

    gmaps = googlemaps.Client(os.getenv("GMAPS_API_KEY"))

    if transportation_mode is None:
        transportation_mode = "driving"
    try:
        directions_result = gmaps.directions(
            start_location,
            destination_location,
            mode=transportation_mode,
            departure_time=datetime(2025, 6, 6, 11, 0), # At 11, date far in the future
        )
        if len(directions_result) == 0:
            return "No way found between these places with the required transportation mode."
        return directions_result[0]["legs"][0]["duration"]["text"]
    except Exception as e:
        print(e)
        return e

agent = CodeAgent(tools=[get_travel_duration], model=HfApiModel(), additional_authorized_imports=["datetime"])

agent.run("Can you give me a nice one-day trip around Paris with a few locations and the times? Could be in the city or outside, but should fit in one day. I'm travelling only with a rented bicycle.")

After a few steps of gathering travel times and running calculations, the agent returns this final proposition:One-day Paris bike trip itinerary:


1. Start at Eiffel Tower at 9:00 AM.
2. Sightseeing at Eiffel Tower until 10:30 AM.
3. Travel to Notre-Dame Cathedral at 10:46 AM.
4. Sightseeing at Notre-Dame Cathedral until 12:16 PM.
5. Travel to Montmartre at 12:41 PM.
6. Sightseeing at Montmartre until 2:11 PM.
7. Travel to Jardin du Luxembourg at 2:33 PM.
8. Sightseeing at Jardin du Luxembourg until 4:03 PM.
9. Travel to Louvre Museum at 4:12 PM.
10. Sightseeing at Louvre Museum until 5:42 PM.
11. Lunch break until 6:12 PM.
12. Planned end time: 6:12 PM.

After building a tool, sharing it to the Hub is as simple as:

get_travel_duration.push_to_hub("{your_username}/get-travel-duration-tool")

Resources of SmolAgents

Frequently Asked Questions about smolagents

smolagents is a minimalist AI agent library developed by the Hugging Face team. It allows developers to create and run powerful AI agents with minimal code. By focusing on simplicity and efficiency, smolagents enables large language models (LLMs) to interact seamlessly with real-world tasks.

Unlike many AI agent frameworks that have complex abstractions and configurations, smolagents maintains a compact codebase of about 1,000 lines. It emphasizes ease of use and supports code agents, where agents execute Python code snippets directly, enhancing efficiency and reducing the number of required steps and LLM calls.

 Code agents are a key feature of smolagents. Instead of generating actions as JSON or text blobs, code agents write and execute Python code snippets to perform tasks. This approach leverages the LLM’s ability to generate and understand code, offering better composability and flexibility in expressing complex logic.

You can install smolagents using pip with the command:

复制pip install smolagents

To get started, define an agent by importing the necessary classes, provide the required tools and LLM model, and run your tasks. For example:

python复制from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel

agent = CodeAgent(tools=[DuckDuckGoSearchTool()], model=HfApiModel())
agent.run("Your task here")

 smolagents is designed for broad compatibility with LLMs. It integrates seamlessly with models hosted on the Hugging Face Hub via Transformers, as well as with models from OpenAI, Anthropic, and others through LiteLLM integration. This flexibility allows you to choose the most suitable LLM for your project needs.

 Yes, in addition to code agents, smolagents supports traditional tool-calling agents. In this mode, actions are written as JSON or text blobs, which can be suitable for specific scenarios where executing code snippets may not be ideal.

 smolagents prioritizes secure code execution by supporting sandboxed environments. For instance, it can utilize E2B (Execution to Binary), which provides a secure and isolated environment for running code snippets. This ensures that code execution does not compromise the host system’s security.

 Code agents offer several benefits:

  • Better Composability: Code naturally allows for function nesting and reuse, making it easier to express complex logic.
  • Efficient Object Management: Managing and passing objects is more straightforward in code compared to JSON.
  • High Flexibility: Code can represent any computational operation, providing immense versatility.
  • Rich Training Data Utilization: Since LLMs are trained on large amounts of code, they are proficient at generating and understanding code snippets.

 smolagents has deep integration with the Hugging Face Hub, enabling developers to easily share and load tools. This fosters a collaborative ecosystem where tools and resources can be contributed and accessed by the community, enhancing the functionality and reach of smolagents.

You can explore the following resources to learn more about smolagents:

  • Official Documentation: Visit the smolagents documentation on the Hugging Face website.
  • Getting Started Guides: Check out introductory guides and tutorials provided by the Hugging Face team.
  • Example Projects: Explore sample projects and code examples on GitHub to see smolagents in action.
  • Community Forums: Engage with the developer community through forums and discussions for insights and support.

Choose from Your languange