Skip to content

smolagents

smolagents is Hugging Face’s minimal library for building agents that “think in code”. You can send full traces of every agent step, tool call, and LLM request to Logfire.

smolagents works with Logfire via the OpenInference instrumentor. Because logfire.configure() sets up the global OpenTelemetry tracer provider, the instrumentor’s spans are exported to Logfire automatically.

Installation

Terminal
pip install logfire 'smolagents[toolkit]' openinference-instrumentation-smolagents

Usage

Call logfire.configure() and then SmolagentsInstrumentor().instrument() before building and running your agent:

import os

from openinference.instrumentation.smolagents import SmolagentsInstrumentor
from smolagents import CodeAgent, OpenAIServerModel, WebSearchTool

import logfire

logfire.configure()
SmolagentsInstrumentor().instrument()

model = OpenAIServerModel(
    model_id='gpt-4o',
    api_base='https://api.openai.com/v1',
    api_key=os.environ['OPENAI_API_KEY'],
)
agent = CodeAgent(tools=[WebSearchTool()], model=model)
agent.run('What is the current population of Tokyo? Search the web.')

You’ll see a trace in Logfire with the agent run at the top and a span for each step, including the code it generated, the tools it called, and the underlying LLM requests. smolagents runs also appear in the specialized Agents view with per-run token counts; the support matrix shows which columns each view populates.

Managed prompts

Keep the user-facing instructions you send to your agent in Prompt Management and fetch them at runtime:

Terminal
pip install 'logfire[variables]'
from smolagents import CodeAgent, OpenAIServerModel, WebSearchTool
from pydantic import BaseModel

import logfire

logfire.configure()


class TaskInputs(BaseModel):
    city: str


task_var = logfire.template_var(
    name='prompt__population_task',
    type=str,
    default='What is the current population of {{city}}? Search the web.',
    inputs_type=TaskInputs,
)

with task_var.get(TaskInputs(city='Tokyo'), label='production') as resolved:
    task = resolved.value

model = OpenAIServerModel(model_id='gpt-4o')
agent = CodeAgent(tools=[WebSearchTool()], model=model)
agent.run(task)

See Use Prompts in Your Application for the full workflow.