> ## Documentation Index
> Fetch the complete documentation index at: /docs/llms.txt
> Use this file to discover all available pages before exploring further.

# SmolAgents integration

> Connect HuggingFace SmolAgents to Toqan Router's AI Gateway for 500+ LLMs and send traces via OpenTelemetry for full agent observability.

> [card] [AI Gateway](#ai-gateway)
>
> Route your LLM calls through the AI Gateway with a single base URL change. Zero vendor lock-in: always run on the best model at the lowest cost for your use case.

> [card] [Observability](#observability)
>
> Instrument your code with OpenTelemetry to capture traces, logs, and metrics for every LLM call, agent step, and tool use.

## AI Gateway

### Overview

SmolAgents is a lightweight Python agent framework by Hugging Face. Using `OpenAIServerModel` with a custom `api_base`, you can route all LLM calls through Toqan Router's AI Gateway for access to 500+ models, cost tracking, and reliability features.

### Prerequisites

* A Toqan Router account and API Key
* Python 3.9 or higher

> [!NOTE]
>
> To setup your API key, see API keys & Endpoints.

### Installation

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
pip install smolagents
```

### Configuration

```python Python theme={"theme":{"light":"github-light","dark":"github-dark"}}
import os
from smolagents import OpenAIServerModel

model = OpenAIServerModel(
    model_id="openai/gpt-5.6-sol",
    api_base="https://router.toqan.ai/eu/v3/router",
    api_key=os.environ["TOQAN_API_KEY"],
)
```

> **api\_base**: `https://router.toqan.ai/eu/v3/router`

### Basic Agent Example

```python Python theme={"theme":{"light":"github-light","dark":"github-dark"}}
import os
from smolagents import CodeAgent, OpenAIServerModel

model = OpenAIServerModel(
    model_id="openai/gpt-5.4-mini",
    api_base="https://router.toqan.ai/eu/v3/router",
    api_key=os.environ["TOQAN_API_KEY"],
)

agent = CodeAgent(tools=[], model=model)
agent.run("What is the capital of France?")
```

### Model Selection

With Toqan Router, you can use any supported model from 30+ providers:

```python Python theme={"theme":{"light":"github-light","dark":"github-dark"}}
import os
from smolagents import OpenAIServerModel

# Use Claude
claude_model = OpenAIServerModel(
    model_id="anthropic/claude-sonnet-5",
    api_base="https://router.toqan.ai/eu/v3/router",
    api_key=os.environ["TOQAN_API_KEY"],
)

# Use Gemini
gemini_model = OpenAIServerModel(
    model_id="google/gemini-2.5-flash",
    api_base="https://router.toqan.ai/eu/v3/router",
    api_key=os.environ["TOQAN_API_KEY"],
)

# Use Groq
groq_model = OpenAIServerModel(
    model_id="groq/qwen/qwen3.8-27b",
    api_base="https://router.toqan.ai/eu/v3/router",
    api_key=os.environ["TOQAN_API_KEY"],
)
```

## Observability

### Overview

SmolAgents uses the `openinference-instrumentation-smolagents` library to automatically instrument agent runs and export traces via OpenTelemetry.

### Prerequisites

* A Toqan Router account and API Key
* Python 3.9 or higher

### Installation

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
pip install smolagents \
    opentelemetry-api \
    opentelemetry-sdk \
    "opentelemetry-exporter-otlp-proto-http" \
    openinference-instrumentation-smolagents
```

### Configuring Toqan Router Observability

Set up the tracer provider and instrument SmolAgents before creating any agents:

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
import os
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
from openinference.instrumentation.smolagents import SmolagentsInstrumentor

exporter = OTLPSpanExporter(
    endpoint="https://router.toqan.ai/eu/v2/otel/v1/traces",
    headers={"Authorization": f"Bearer {os.environ['TOQAN_API_KEY']}"},
)
tracer_provider = TracerProvider()
tracer_provider.add_span_processor(BatchSpanProcessor(exporter))

SmolagentsInstrumentor().instrument(tracer_provider=tracer_provider)
```

> [!NOTE]
>
> `SmolagentsInstrumentor` must be called before any agent is instantiated. Once instrumented, all `CodeAgent` and `ToolCallingAgent` runs are automatically traced.

### Basic Example

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
import os
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
from openinference.instrumentation.smolagents import SmolagentsInstrumentor

exporter = OTLPSpanExporter(
    endpoint="https://router.toqan.ai/eu/v2/otel/v1/traces",
    headers={"Authorization": f"Bearer {os.environ['TOQAN_API_KEY']}"},
)
tracer_provider = TracerProvider()
tracer_provider.add_span_processor(BatchSpanProcessor(exporter))
SmolagentsInstrumentor().instrument(tracer_provider=tracer_provider)

from smolagents import CodeAgent, OpenAIServerModel

agent = CodeAgent(
    tools=[],
    model=OpenAIServerModel(
        model_id="openai/gpt-5.4-mini",
        api_base="https://router.toqan.ai/eu/v3/router",
        api_key=os.environ["TOQAN_API_KEY"],
    ),
)
agent.run("What is the capital of France?")
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.