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

# Microsoft Semantic Kernel integration

> Connect Microsoft Semantic Kernel to Toqan Router's AI Gateway for complete observability, built-in reliability, and access to 500+ LLMs across 30+ providers.

> [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)
>
> Send Semantic Kernel traces to **Toqan Router** with OpenTelemetry.

## AI Gateway

### Overview

Microsoft Semantic Kernel is an SDK that integrates Large Language Models (LLMs) with conventional programming languages. By connecting Semantic Kernel to Toqan Router's AI Gateway, you transform experimental AI agents into production-ready systems with enterprise-grade capabilities.

### Key Benefits

Toqan Router's AI Gateway enhances your Semantic Kernel applications with:

> [card] Complete Observability
>
> Track every agent step, tool use, and interaction with detailed traces and analytics

> [card] Built-in Reliability
>
> Automatic fallbacks, retries, and load balancing for production resilience

> [card] Cost Optimization
>
> Real-time cost tracking and spend management across all your AI operations

> [card] Multi-Provider Access
>
> Access 500+ LLMs and 30+ providers through a single, unified integration

### Prerequisites

Before integrating Semantic Kernel with Toqan Router, ensure you have:

* A Toqan Router account and API Key
* Python 3.10 or higher
* Semantic Kernel SDK installed

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

### Installation

Install Semantic Kernel and the OpenAI SDK:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
pip install semantic-kernel openai
```

### Configuration

Configure Semantic Kernel to use Toqan Router's AI Gateway by creating an OpenAI client with a custom base URL:

```python Python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from openai import AsyncOpenAI
from semantic_kernel import Kernel
from semantic_kernel.connectors.ai.open_ai import OpenAIChatCompletion
import os

# Configure OpenAI client with Toqan Router AI Gateway
client = AsyncOpenAI(
    api_key=os.getenv("TOQAN_API_KEY"),
    base_url="https://router.toqan.ai/eu/v3/router"
)

# Create kernel
kernel = Kernel()

# Add chat completion service
chat_service = OpenAIChatCompletion(
    ai_model_id="openai/gpt-5.6-sol",
    async_client=client
)

kernel.add_service(chat_service)
```

> **base\_url**: `https://router.toqan.ai/eu/v3/router`

### Basic Example

Here's a complete example of using Semantic Kernel with Toqan Router:

```python Python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from openai import AsyncOpenAI
from semantic_kernel import Kernel
from semantic_kernel.connectors.ai.open_ai import (
    OpenAIChatCompletion,
    OpenAIChatPromptExecutionSettings
)
from semantic_kernel.contents import ChatHistory
import asyncio
import os

async def main():
    # Configure client with Toqan Router
    client = AsyncOpenAI(
        api_key=os.getenv("TOQAN_API_KEY"),
        base_url="https://router.toqan.ai/eu/v3/router"
    )

    # Create kernel
    kernel = Kernel()

    # Add chat completion service
    chat_service = OpenAIChatCompletion(
        ai_model_id="openai/gpt-5.6-sol",
        async_client=client
    )
    kernel.add_service(chat_service)

    # Create execution settings
    settings = OpenAIChatPromptExecutionSettings(
        max_tokens=2000,
        temperature=0.7
    )

    # Create chat history
    history = ChatHistory()
    history.add_user_message("What is quantum computing?")

    # Get response
    response = await chat_service.get_chat_message_content(
        chat_history=history,
        settings=settings,
        kernel=kernel
    )

    print(response.content)

if __name__ == "__main__":
    asyncio.run(main())
```

### Using Plugins (Functions)

Semantic Kernel's power comes from combining LLMs with plugins. Here's how to use them with Toqan Router:

```python Python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from openai import AsyncOpenAI
from semantic_kernel import Kernel
from semantic_kernel.connectors.ai.open_ai import OpenAIChatCompletion
from semantic_kernel.functions import kernel_function
from semantic_kernel.contents import ChatHistory
import asyncio
import os

# Define a plugin
class WeatherPlugin:
    @kernel_function(
        name="get_weather",
        description="Get the weather for a location"
    )
    def get_weather(self, location: str) -> str:
        """Get weather for a location."""
        return f"The weather in {location} is sunny and 72°F"

async def main():
    # Configure client
    client = AsyncOpenAI(
        api_key=os.getenv("TOQAN_API_KEY"),
        base_url="https://router.toqan.ai/eu/v3/router"
    )

    # Create kernel
    kernel = Kernel()

    # Add chat completion service
    chat_service = OpenAIChatCompletion(
        ai_model_id="openai/gpt-5.6-sol",
        async_client=client
    )
    kernel.add_service(chat_service)

    # Add plugin
    kernel.add_plugin(
        WeatherPlugin(),
        plugin_name="WeatherPlugin"
    )

    # Create chat history
    history = ChatHistory()
    history.add_user_message("What's the weather in San Francisco?")

    # Enable automatic function calling
    from semantic_kernel.connectors.ai.open_ai import OpenAIChatPromptExecutionSettings
    from semantic_kernel.connectors.ai.function_choice_behavior import FunctionChoiceBehavior

    execution_settings = OpenAIChatPromptExecutionSettings(
        function_choice_behavior=FunctionChoiceBehavior.Auto()
    )

    # Get response with function calling
    response = await chat_service.get_chat_message_content(
        chat_history=history,
        settings=execution_settings,
        kernel=kernel
    )

    print(response.content)

if __name__ == "__main__":
    asyncio.run(main())
```

### Model Selection

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

```python Python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from openai import AsyncOpenAI
from semantic_kernel import Kernel
from semantic_kernel.connectors.ai.open_ai import OpenAIChatCompletion
import os

# Configure client
client = AsyncOpenAI(
    api_key=os.getenv("TOQAN_API_KEY"),
    base_url="https://router.toqan.ai/eu/v3/router"
)

kernel = Kernel()

# Use Claude
claude_service = OpenAIChatCompletion(
    ai_model_id="anthropic/claude-sonnet-5",
    async_client=client,
    service_id="claude"
)
kernel.add_service(claude_service)

# Use Gemini
gemini_service = OpenAIChatCompletion(
    ai_model_id="google-ai/gemini-2.5-flash",
    async_client=client,
    service_id="gemini"
)
kernel.add_service(gemini_service)

# Use any other model
groq_service = OpenAIChatCompletion(
    ai_model_id="groq/qwen/qwen3.8-27b",
    async_client=client,
    service_id="groq"
)
kernel.add_service(groq_service)
```

### Streaming Responses

Semantic Kernel supports streaming with Toqan Router:

```python Python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from openai import AsyncOpenAI
from semantic_kernel import Kernel
from semantic_kernel.connectors.ai.open_ai import (
    OpenAIChatCompletion,
    OpenAIChatPromptExecutionSettings
)
from semantic_kernel.contents import ChatHistory
import asyncio
import os

async def main():
    client = AsyncOpenAI(
        api_key=os.getenv("TOQAN_API_KEY"),
        base_url="https://router.toqan.ai/eu/v3/router"
    )

    kernel = Kernel()
    chat_service = OpenAIChatCompletion(
        ai_model_id="openai/gpt-5.6-sol",
        async_client=client
    )
    kernel.add_service(chat_service)

    settings = OpenAIChatPromptExecutionSettings(
        max_tokens=2000,
        temperature=0.7
    )

    history = ChatHistory()
    history.add_user_message("Write a short story about AI")

    # Stream response
    async for message in chat_service.get_streaming_chat_message_content(
        chat_history=history,
        settings=settings,
        kernel=kernel
    ):
        print(message.content, end="", flush=True)

if __name__ == "__main__":
    asyncio.run(main())
```

## Observability

The **AI Gateway** records model requests routed through it. To capture Semantic Kernel's own spans, including kernel function calls, configure OpenTelemetry in the application.

### Install dependencies

```bash Bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
pip install semantic-kernel openai opentelemetry-sdk opentelemetry-exporter-otlp-proto-http
```

### Enable model diagnostics

Set this variable before starting Python so Semantic Kernel also emits model spans:

```bash Bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
export SEMANTICKERNEL_EXPERIMENTAL_GENAI_ENABLE_OTEL_DIAGNOSTICS=true
```

This experimental setting includes model names and token usage without enabling prompt or response capture. See [Semantic Kernel's telemetry documentation](https://learn.microsoft.com/en-us/semantic-kernel/concepts/enterprise-readiness/observability/telemetry-with-console) for the available diagnostics settings.

### Send traces to Toqan Router

Set `TOQAN_API_KEY` to a **Toqan Router** API key, then run this example. Initialize one tracer provider for the application and shut it down when the application exits.

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

from openai import AsyncOpenAI
from opentelemetry import trace
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
from semantic_kernel import Kernel
from semantic_kernel.connectors.ai.open_ai import OpenAIChatCompletion

tracer_provider = TracerProvider(
    resource=Resource.create({"service.name": "semantic-kernel-app"})
)
tracer_provider.add_span_processor(
    BatchSpanProcessor(
        OTLPSpanExporter(
            endpoint="https://router.toqan.ai/eu/v2/otel/v1/traces",
            headers={"Authorization": f"Bearer {os.environ['TOQAN_API_KEY']}"},
        )
    )
)
trace.set_tracer_provider(tracer_provider)


async def main():
    async with AsyncOpenAI(
        api_key=os.environ["TOQAN_API_KEY"],
        base_url="https://router.toqan.ai/eu/v3/router",
    ) as client:
        kernel = Kernel()
        kernel.add_service(
            OpenAIChatCompletion(
                ai_model_id="openai/gpt-5.6-sol",
                async_client=client,
            )
        )
        answer = await kernel.invoke_prompt("Explain quantum computing in one sentence.")
        print(answer)


if __name__ == "__main__":
    try:
        asyncio.run(main())
    finally:
        tracer_provider.force_flush()
        tracer_provider.shutdown()
```

Open [Traces](/docs/ai-studio/observability/traces.md) to inspect the exported spans from `semantic-kernel-app`.


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