Smolagents (tracing)

Smolagents is HuggingFace’s lightweight agent framework that lets you build agents with code-based actions. It focuses on simplicity and supports both code agents and tool-calling agents. Respan gives you full observability over every agent run, planning step, code execution, tool call, and LLM generation — and gateway routing through the OpenAI-compatible Respan endpoint.

Create an account at platform.respan.ai and grab an API key.

Run npx @respan/cli setup to set up with your coding agent.

See Smolagents gateway setup to route this integration through the Respan gateway.

Setup

1

Install packages

$pip install respan-ai respan-instrumentation-smolagents
2

Set environment variables

$export OPENAI_API_KEY="YOUR_OPENAI_API_KEY"
$export RESPAN_API_KEY="YOUR_RESPAN_API_KEY"

OPENAI_API_KEY is used for LLM requests. RESPAN_API_KEY is used to export traces to Respan.

3

Initialize and run

1from respan import Respan, workflow
2from respan_instrumentation_smolagents import SmolagentsInstrumentor
3from smolagents import CodeAgent, DuckDuckGoSearchTool, LiteLLMModel
4
5respan = Respan(instrumentations=[SmolagentsInstrumentor()])
6
7model = LiteLLMModel(model_id="openai/gpt-4o-mini")
8agent = CodeAgent(
9 tools=[DuckDuckGoSearchTool()],
10 model=model,
11)
12
13@workflow(name="smolagents_code_agent_workflow")
14def run_agent():
15 return agent.run("What is the latest news about AI agents?")
16
17result = run_agent()
18print(result)
19respan.flush()
20respan.shutdown()
4

View your trace

Open the Traces page to see your Smolagents workflow, identified by smolagents_code_agent_workflow, with planning steps, code execution, tool calls, and LLM generations.

Configuration

ParameterTypeDefaultDescription
api_keystr | NoneNoneFalls back to RESPAN_API_KEY env var.
base_urlstr | NoneNoneFalls back to RESPAN_BASE_URL env var.
instrumentationslist[]Plugin instrumentations to activate (e.g. SmolagentsInstrumentor()).
customer_identifierstr | NoneNoneDefault customer identifier for all spans.
metadatadict | NoneNoneDefault metadata attached to all spans.
environmentstr | NoneNoneEnvironment tag (e.g. "production").

Attributes

In Respan()

Set defaults at initialization — these apply to all spans.

1from respan import Respan
2from respan_instrumentation_smolagents import SmolagentsInstrumentor
3
4respan = Respan(
5 instrumentations=[SmolagentsInstrumentor()],
6 customer_identifier="user_123",
7 metadata={"service": "smolagents-api", "version": "1.0.0"},
8)

With propagate_attributes

Override per-request using a context scope.

1from respan import Respan, propagate_attributes
2from respan_instrumentation_smolagents import SmolagentsInstrumentor
3
4respan = Respan(instrumentations=[SmolagentsInstrumentor()])
5
6def handle_request(user_id: str, message: str):
7 with propagate_attributes(
8 customer_identifier=user_id,
9 thread_identifier="conv_abc_123",
10 metadata={"plan": "pro"},
11 ):
12 print(agent.run(message))
AttributeTypeDescription
customer_identifierstrIdentifies the end user in Respan analytics.
thread_identifierstrGroups related messages into a conversation.
metadatadictCustom key-value pairs. Merged with default metadata.