---
title: "Semantic Kernel (tracing)"
description: "Trace Microsoft Semantic Kernel functions, chat completions, plugin calls, and failures with Respan."
---

[Microsoft Semantic Kernel](https://github.com/microsoft/semantic-kernel) is a Python SDK for composing agents, plugins, prompts, and model services. Respan enables Semantic Kernel diagnostics and normalizes kernel function, chat completion, and tool spans into Respan traces.

This tracing setup uses your model provider credentials directly. To route model calls through Respan instead, use the [Semantic Kernel gateway setup](/docs/gateway/semantic-kernel).

<Accordion title="Set up Respan">
Create an account at [platform.respan.ai](https://platform.respan.ai) and grab an [API key](https://platform.respan.ai/platform/gateway/api-keys).
</Accordion>

Paste this into your coding agent:

```text
Help me set up Respan tracing with the Semantic Kernel integration.
Follow https://www.respan.ai/docs/integrations/semantic-kernel.md
```

## Setup

<Steps>
<Step title="Install packages">

```bash
pip install respan-ai respan-instrumentation-semantic-kernel semantic-kernel openai
```

</Step>

<Step title="Set environment variables">

```bash
export OPENAI_API_KEY="YOUR_OPENAI_API_KEY"
export RESPAN_API_KEY="YOUR_RESPAN_API_KEY"
```

`OPENAI_API_KEY` is used by Semantic Kernel for direct model requests. `RESPAN_API_KEY` exports traces to Respan.

</Step>

<Step title="Initialize and run">

```python
import asyncio
import os

from respan import Respan, workflow
from respan_instrumentation_semantic_kernel import SemanticKernelInstrumentor
from semantic_kernel import Kernel
from semantic_kernel.connectors.ai.open_ai import OpenAIChatCompletion
from semantic_kernel.connectors.ai.open_ai import OpenAIChatPromptExecutionSettings
from semantic_kernel.functions import KernelArguments

respan = Respan(
    app_name="semantic-kernel-demo",
    instrumentations=[SemanticKernelInstrumentor()],
)

kernel = Kernel()
kernel.add_service(
    OpenAIChatCompletion(
        service_id="chat",
        ai_model_id="gpt-4o-mini",
        api_key=os.environ["OPENAI_API_KEY"],
    )
)

@workflow(name="semantic_kernel_chat")
async def run_chat() -> str:
    result = await kernel.invoke_prompt(
        "Reply in one sentence: what does Semantic Kernel help developers build?",
        arguments=KernelArguments(
            settings=OpenAIChatPromptExecutionSettings(
                service_id="chat",
                temperature=0.2,
                max_tokens=80,
            )
        ),
    )
    return str(result)

async def main() -> None:
    print(await run_chat())

asyncio.run(main())
respan.flush()
respan.shutdown()
```

</Step>

<Step title="View your trace">

Open the [Traces page](https://platform.respan.ai/platform/traces) to see kernel function, chat completion, plugin/tool, and error spans.

</Step>
</Steps>

## Configuration

| Parameter | Type | Default | Description |
|-----------|------|---------|-------------|
| `api_key` | `str \| None` | `RESPAN_API_KEY` | Respan API key used to export traces. |
| `base_url` | `str \| None` | `RESPAN_BASE_URL` | Optional Respan trace export API URL. |
| `instrumentations` | `list` | `[]` | Include `SemanticKernelInstrumentor()` to activate Semantic Kernel tracing. |
| `customer_identifier` | `str \| None` | `None` | Default customer identifier for all spans. |
| `metadata` | `dict \| None` | `None` | Default metadata attached to all spans. |
| `environment` | `str \| None` | `None` | Environment tag, for example `"production"`. |

## What gets traced

| Semantic Kernel operation | Respan span | Notes |
|---------------------------|-------------|-------|
| Kernel function invocation | `task` | Captures function name, arguments, result, and failures. |
| Chat or text completion | `chat` or `text` | Captures prompt/completion content, model, and token usage when Semantic Kernel exposes it. |
| Plugin tool execution | `tool` | Captures plugin function name, arguments, result, and errors. |

## Attributes

Set defaults at initialization. These apply to every Semantic Kernel span emitted in the active process.

```python
from respan import Respan
from respan_instrumentation_semantic_kernel import SemanticKernelInstrumentor

respan = Respan(
    instrumentations=[SemanticKernelInstrumentor()],
    customer_identifier="user_123",
    metadata={"service": "semantic-kernel-api", "version": "1.0.0"},
    environment="production",
)
```

Pass `capture_content=False` to `SemanticKernelInstrumentor()` when you want diagnostics without prompt and completion payload content.
