Skip to main content
0

In the Weeds: Building AI Agents with Smolagents

joey-io's avatarjoey-io4 min read

A technical guide to building AI agents in Python with Hugging Face's Smolagents — lightweight, composable, and surprisingly capable.

The Agent Problem

Most AI agent frameworks have the same issue: they're enormous. You pull in a library to make a simple tool-calling agent and suddenly you're managing a dependency tree that rivals a small operating system. Configuration files breed. Abstractions pile up. By the time you've built something, you've spent more time learning the framework than solving your actual problem.

SSmolagents takes the opposite approach. Built by Hugging Face, it's deliberately small — a few hundred lines of core code — and it does one thing well: it lets language models use tools.

What SSmolagents Actually Is

At its core, Smolagents is a lightweight Python library for building AI agents that can call tools. An "agent" here means an LLM that can decide which tools to use, call them, interpret the results, and decide what to do next. It's the AI equivalent of giving someone a toolbox and a problem.

The key architectural decisions:

  • Code-based agents. Instead of generating JSON tool calls, Smolagents agents write Python code. This sounds risky. In practice, it's more capable — the agent can use loops, conditionals, and variable assignment, not just flat function calls.
  • Minimal abstraction. A tool is a Python function with a docstring. That's it. No schema files, no registration boilerplate, no configuration layers.
  • Model agnostic. Works with any LLM that can generate text. OpenAI, Anthropic, Hugging Face models, local models via LLocalAI — same interface.

Getting Started

Install:

bashpip install smolagents

Build your first agent:

pythonfrom smolagents import CodeAgent, tool, HfApiModel

@tool
def get_weather(city: str) -> str:
"""Get current weather for a city.

Args:
city: Name of the city to check weather for.
"""
# In production, call a real weather API
return f"Weather in {city}: 72F, partly cloudy"

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

result = agent.run("What's the weather like in Portland?")
print(result)

That's a complete agent. The @tool decorator turns a Python function into something the agent can discover and call. The docstring is the tool's description — the agent reads it to decide when and how to use the tool.

Building Real Tools

The weather example is a toy. Let's build something useful. Here's a research agent that can search the web and summarize findings:

pythonfrom smolagents import CodeAgent, tool, HfApiModel
import requests

@tool
def web_search(query: str) -> str:
"""Search the web for information.

Args:
query: The search query string.
"""
# Use your preferred search API
response = requests.get(
"https://api.search.example/search",
params={"q": query}
)
results = response.json()
return "\n".join(
f"- {r['title']}: {r['snippet']}"
for r in results[:5]
)

@tool
def read_webpage(url: str) -> str:
"""Read the text content of a webpage.

Args:
url: The URL to read.
"""
response = requests.get(url)
# Simplified — use a real HTML parser in production
return response.text[:5000]

agent = CodeAgent(
tools=[web_search, read_webpage],
model=HfApiModel()
)

result = agent.run(
"Research the current state of solid-state batteries "
"and summarize the key developments in 2025."
)

The agent will search, read relevant pages, synthesize the information, and produce a summary. The code-based approach means it can do this in a loop — search, read, assess, search again if needed — without you programming the loop logic.

Multi-Agent Patterns

Smolagents supports multi-agent architectures where agents delegate to each other. A manager agent breaks a complex task into subtasks and assigns them to specialist agents.

pythonfrom smolagents import CodeAgent, ManagedAgent

web_agent = CodeAgent(
tools=[web_search, read_webpage],
model=HfApiModel()
)

managed_web = ManagedAgent(
agent=web_agent,
name="web_researcher",
description="Searches the web and reads pages to gather information."
)

manager = CodeAgent(
tools=[],
managed_agents=[managed_web],
model=HfApiModel()
)

result = manager.run("Compare three competing approaches to AI safety.")

The manager breaks the comparison into research subtasks, delegates them to the web researcher, then synthesizes. This pattern scales well for complex tasks that benefit from specialization.

Integrating with the Ecosystem

Smolagents plays well with other tools:

Smolagents + Apify: AApify MCP provides web scraping capabilities. Build a Smolagents tool that calls Apify for structured data extraction, and your agent can pull information from any website with consistent formatting.

Smolagents + ttxtai: ttxtai handles vector search. Build a tool that searches your local document embeddings, giving your agent access to a knowledge base.

Smolagents + SSemantic Kernel: SSemantic Kernel provides enterprise-grade AI orchestration. Use Smolagents for quick prototyping and Semantic Kernel for production deployment. The tool definitions transfer cleanly.

Smolagents + nn8n: nn8n can trigger Smolagents scripts as part of larger automation workflows, combining the agent's reasoning with n8n's integration capabilities.

Safety and Sandboxing

Code-executing agents need guardrails. Smolagents includes a sandboxed execution environment that restricts what the generated code can do. You can whitelist specific imports, block file system access, and limit execution time.

pythonagent = CodeAgent(
    tools=[web_search],
    model=HfApiModel(),
    additional_authorized_imports=["json", "re"],
    max_steps=10
)

Never run unsandboxed agents with access to sensitive systems. The sandbox isn't perfect — it's a mitigation, not a guarantee. For production, run agents in containers with minimal permissions.

When to Use Smolagents

Good fit: Rapid prototyping, research tasks, multi-step workflows where the logic isn't easily prespecified, internal tools where you control the environment.

Not ideal: High-reliability production systems (use Semantic Kernel or similar), agents that need deterministic behavior, scenarios where code execution is unacceptable.

Getting Started

  1. pip install smolagents
  2. Write one tool function with a clear docstring
  3. Create a CodeAgent and give it a task
  4. Watch what code it generates — that's where the learning happens

The beauty of SSmolagents is its refusal to be complicated. In a world of enterprise agent frameworks, sometimes you just need a language model with a toolbox.

Share this post:

Ratings & Reviews

0.0

out of 5

0 ratings

No reviews yet. Be the first to share your experience.