Using Opik with Smolagents

Opik integrates seamlessly with Smolagents, a framework from HuggingFace to create Agents. The integration leverages Opik’s built-in OpenTelemetry support.

Creating an account on Comet.com

Comet provides a hosted version of the Opik platform, simply create an account and grab your API Key.

You can also run the Opik platform locally, see the installation guide for more information.

1%pip install 'smolagents[telemetry,toolkit]' opik
1import opik
2
3opik.configure(use_local=False)

Preparing our environment

The Opik integration supports all Smolagents clients, for this demo we will be using the OpenAI client and will therefore set our OpenAI API keys.

1import os
2import getpass
3
4if "OPENAI_API_KEY" not in os.environ:
5 os.environ["OPENAI_API_KEY"] = getpass.getpass("Enter your OpenAI API key: ")

We now need to configure the OpenTelemetry environment variables:

1import opik
2import os
3
4opik_config = opik.config.get_from_user_inputs()
5
6os.environ["OTEL_EXPORTER_OTLP_ENDPOINT"] = (
7 "https://www.comet.com/opik/api/v1/private/otel"
8)
9os.environ["OTEL_EXPORTER_OTLP_HEADERS"] = (
10 f"Authorization={opik_config.api_key},projectName={opik_config.project_name},Comet-Workspace={opik_config.workspace}"
11)

Note 1: The projectName and Comet-Workspace headers are optional and can be set to Default and default respectively.

Note 2: If you are using the self-hosted version of Opik, you will need to update the environment variables defined above. You can learn more about this in the Opik OpenTelemetry documentation.

Setting up OpenTelemetry for SmolAgents

To set up the Telemetry, you will need to add the following to your code:

1from opentelemetry.sdk.trace import TracerProvider
2
3from openinference.instrumentation.smolagents import SmolagentsInstrumentor
4from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
5from opentelemetry.sdk.trace.export import SimpleSpanProcessor
6
7trace_provider = TracerProvider()
8trace_provider.add_span_processor(SimpleSpanProcessor(OTLPSpanExporter()))
9
10SmolagentsInstrumentor().instrument(tracer_provider=trace_provider)

Using the Smolagents library

Now that everything is set up, we can use the Smolagents library and track the corresponding traces in Opik:

1from smolagents import CodeAgent, WebSearchTool, OpenAIServerModel
2
3model = OpenAIServerModel(
4 model_id="gpt-4o",
5)
6
7agent = CodeAgent(tools=[WebSearchTool()], model=model, stream_outputs=True)
8
9agent.run(
10 "How many seconds would it take for a leopard at full speed to run through Pont des Arts?"
11)

As we have configured the OpenTelemetry logging, the agent execution and all it’s intermediate steps will be logged in Opik:

Smolagents Integration