Smolagents

Smolagents (Hugging Face)

Minimalist framework where Agents write Python code to solve tasks.

1. Philosophy: Code as Action

Most agents (CrewAI, LangGraph) output JSON to call tools. Smolagents output Python Code. The framework runs this code in a sandbox. This handles loops, logic, and variables natively in one LLM call.

2. The CodeAgent

from smolagents import CodeAgent, HfApiModel

# 1. Connect to an LLM (e.g., Qwen-Coder or Llama via HF Inference)
model = HfApiModel(model_id="Qwen/Qwen2.5-Coder-32B-Instruct")

# 2. Create Agent
agent = CodeAgent(
    tools=[], 
    model=model,
    add_base_tools=True # Adds DuckDuckGo, Calculator, etc.
)

# 3. Run
# The agent will literally write python code to fetch the page, parse it, and print result.
agent.run("Go to wikipedia.org, find the featured article, and summarize it.")

Technique: Custom Tools

Decorate any python function.

from smolagents import tool

@tool
def get_weather(city: str) -> str:
    """
    Get the weather for a city.
    Args:
        city: The name of the city.
    """
    return "Sunny"

agent = CodeAgent(tools=[get_weather], model=model)

Technique: Hub Sharing

Smolagents is built to share agents like models.

# Push your agent configuration and tools to Hugging Face
agent.push_to_hub("my-username/my-weather-agent")

# Load someone else's agent
from smolagents import load_from_hub
agent = load_from_hub("my-username/my-weather-agent")