Skip to main content

AI Router

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 Orq.ai’s AI Router for access to 300+ models, cost tracking, and reliability features.

Prerequisites

  • An Orq.ai account and API Key
  • Python 3.9 or higher
To setup your API key, see API keys & Endpoints.

Installation

pip install smolagents

Configuration

Python
import os
from smolagents import OpenAIServerModel

model = OpenAIServerModel(
    model_id="openai/gpt-4o",
    api_base="https://api.orq.ai/v2/router",
    api_key=os.environ["ORQ_API_KEY"],
)
api_base: https://api.orq.ai/v2/router

Basic Agent Example

Python
import os
from smolagents import CodeAgent, OpenAIServerModel

model = OpenAIServerModel(
    model_id="openai/gpt-4o-mini",
    api_base="https://api.orq.ai/v2/router",
    api_key=os.environ["ORQ_API_KEY"],
)

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

Model Selection

With Orq.ai, you can use any supported model from 20+ providers:
Python
import os
from smolagents import OpenAIServerModel

# Use Claude
claude_model = OpenAIServerModel(
    model_id="anthropic/claude-sonnet-4-5-20250929",
    api_base="https://api.orq.ai/v2/router",
    api_key=os.environ["ORQ_API_KEY"],
)

# Use Gemini
gemini_model = OpenAIServerModel(
    model_id="google/gemini-2.5-flash",
    api_base="https://api.orq.ai/v2/router",
    api_key=os.environ["ORQ_API_KEY"],
)

# Use Groq
groq_model = OpenAIServerModel(
    model_id="groq/llama-3.3-70b-versatile",
    api_base="https://api.orq.ai/v2/router",
    api_key=os.environ["ORQ_API_KEY"],
)

Observability

Overview

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

Prerequisites

  • An Orq.ai account and API Key
  • Python 3.9 or higher

Installation

pip install smolagents \
    opentelemetry-api \
    opentelemetry-sdk \
    "opentelemetry-exporter-otlp-proto-http" \
    openinference-instrumentation-smolagents

Configuring Orq.ai Observability

Set up the tracer provider and instrument SmolAgents before creating any agents:
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://api.orq.ai/v2/otel/v1/traces",
    headers={"Authorization": f"Bearer {os.environ['ORQ_API_KEY']}"},
)
tracer_provider = TracerProvider()
tracer_provider.add_span_processor(BatchSpanProcessor(exporter))

SmolagentsInstrumentor().instrument(tracer_provider=tracer_provider)
SmolagentsInstrumentor must be called before any agent is instantiated. Once instrumented, all CodeAgent and ToolCallingAgent runs are automatically traced.

Basic Example

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://api.orq.ai/v2/otel/v1/traces",
    headers={"Authorization": f"Bearer {os.environ['ORQ_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-4o-mini",
        api_base="https://api.orq.ai/v2/router",
        api_key=os.environ["ORQ_API_KEY"],
    ),
)
agent.run("What is the capital of France?")