Skip to main content
AgentOps provides seamless integration with Smolagents, HuggingFace’s lightweight framework for building AI agents. Monitor your agent workflows, tool usage, and execution traces automatically.

Core Concepts

Smolagents is designed around several key concepts:
  • Agents: AI assistants that can use tools and reason through problems
  • Tools: Functions that agents can call to interact with external systems
  • Models: LLM backends that power agent reasoning (supports various providers via LiteLLM)
  • Code Execution: Agents can write and execute Python code in sandboxed environments
  • Multi-Agent Systems: Orchestrate multiple specialized agents working together

Installation

Install AgentOps and Smolagents, along with any additional dependencies:
pip install agentops smolagents python-dotenv
poetry add agentops smolagents python-dotenv
uv pip install agentops smolagents python-dotenv

Setting Up API Keys

Before using Smolagents with AgentOps, you need to set up your API keys:
  • AGENTOPS_API_KEY: From your AgentOps Dashboard
  • LLM API Keys: Depending on your chosen model provider (e.g., OPENAI_API_KEY, ANTHROPIC_API_KEY)
Set these as environment variables or in a .env file.
export AGENTOPS_API_KEY="your_agentops_api_key_here"
export OPENAI_API_KEY="your_openai_api_key_here"
AGENTOPS_API_KEY="your_agentops_api_key_here"
OPENAI_API_KEY="your_openai_api_key_here"
Then load them in your Python code:
from dotenv import load_dotenv
import os

load_dotenv()

AGENTOPS_API_KEY = os.getenv("AGENTOPS_API_KEY")
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")

Usage

Initialize AgentOps before creating your Smolagents to automatically track all agent interactions:
import agentops
from smolagents import LiteLLMModel, ToolCallingAgent, DuckDuckGoSearchTool

# Initialize AgentOps
agentops.init()

# Create a model (supports various providers via LiteLLM)
model = LiteLLMModel("openai/gpt-4o-mini")

# Create an agent with tools
agent = ToolCallingAgent(
    tools=[DuckDuckGoSearchTool()],
    model=model,
)

# Run the agent
result = agent.run("What are the latest developments in AI safety research?")
print(result)

Examples

import agentops
from smolagents import LiteLLMModel, CodeAgent

# Initialize AgentOps
agentops.init()

# Create a model
model = LiteLLMModel("openai/gpt-4o-mini")

# Create a code agent that can perform calculations
agent = CodeAgent(
    tools=[],  # No external tools needed for math
    model=model,
    additional_authorized_imports=["math", "numpy"],
)

# Ask the agent to solve a math problem
result = agent.run(
    "Calculate the compound interest on $10,000 invested at 5% annual rate "
    "for 10 years, compounded monthly. Show your work."
)

print(result)
import agentops
from smolagents import (
    LiteLLMModel,
    ToolCallingAgent,
    DuckDuckGoSearchTool,
    tool
)

# Initialize AgentOps
agentops.init()

# Create a custom tool
@tool
def word_counter(text: str) -> str:
    """
    Counts the number of words in a given text.
    
    Args:
        text: The text to count words in.
        
    Returns:
        A string with the word count.
    """
    word_count = len(text.split())
    return f"The text contains {word_count} words."

# Create model and agent
model = LiteLLMModel("openai/gpt-4o-mini")

agent = ToolCallingAgent(
    tools=[DuckDuckGoSearchTool(), word_counter],
    model=model,
)

# Run a research task
result = agent.run(
    "Search for information about the James Webb Space Telescope's latest discoveries. "
    "Then count how many words are in your summary."
)

print(result)
import agentops
from smolagents import LiteLLMModel, CodeAgent, tool
import json

# Initialize AgentOps
agentops.init()

# Create tools for data processing
@tool
def save_json(data: dict, filename: str) -> str:
    """
    Saves data to a JSON file.
    
    Args:
        data: Dictionary to save
        filename: Name of the file to save to
        
    Returns:
        Success message
    """
    with open(filename, 'w') as f:
        json.dump(data, f, indent=2)
    return f"Data saved to {filename}"

@tool
def load_json(filename: str) -> dict:
    """
    Loads data from a JSON file.
    
    Args:
        filename: Name of the file to load from
        
    Returns:
        The loaded data as a dictionary
    """
    with open(filename, 'r') as f:
        return json.load(f)

# Create agent
model = LiteLLMModel("openai/gpt-4o-mini")

agent = CodeAgent(
    tools=[save_json, load_json],
    model=model,
    additional_authorized_imports=["pandas", "datetime"],
)

# Run a multi-step data processing task
result = agent.run("""
1. Create a dataset of 5 fictional employees with names, departments, and salaries
2. Save this data to 'employees.json'
3. Load the data back and calculate the average salary
4. Find the highest paid employee
5. Return a summary of your findings
""")

print(result)

More Examples

Multi-Agent System

Complex multi-agent web browsing system

Text to SQL Agent

Convert natural language queries to SQL
Visit your AgentOps Dashboard to see detailed traces of your Smolagents executions, tool usage, and agent reasoning steps.