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

# Semantic Kernel

> Trace Microsoft Semantic Kernel LLM calls and plugin executions with Zespan.

<Note>
  Available for: **Python** only.
</Note>

## Installation

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

## Usage

Build your kernel, then call `instrument_semantic_kernel(kernel, zespan.get_client())` to register Zespan's tracing filters on it. This patches Semantic Kernel's function-invocation and prompt-rendering filters to forward spans to Zespan — it takes both the `Kernel` instance and the active Zespan client explicitly, so call it after `zespan.init()` and after the kernel is constructed.

```python theme={"theme":{"light":"github-light","dark":"one-dark-pro"}}
import asyncio
import zespan
from zespan import instrument_semantic_kernel
import semantic_kernel as sk
from semantic_kernel.connectors.ai.open_ai import OpenAIChatCompletion

zespan.init(api_key="zsp_your_api_key_here")

kernel = sk.Kernel()
kernel.add_service(OpenAIChatCompletion(
    service_id="chat",
    ai_model_id="gpt-4o",
))

instrument_semantic_kernel(kernel, zespan.get_client())

async def main():
    result = await kernel.invoke_prompt(
        "What is the capital of France?",
        service_id="chat",
    )
    print(result)

asyncio.run(main())
```

All kernel invocations, plugin function calls, and LLM completions are traced automatically after calling `instrument_semantic_kernel()`.

## What gets captured

| Field           | Details                                                                                                       |
| --------------- | ------------------------------------------------------------------------------------------------------------- |
| Span kind       | `llm` for prompt functions, `tool` for native/plugin functions                                                |
| Model           | The `ai_model_id` of the service backing the invoked function                                                 |
| Provider        | Inferred from the model name (`openai`, `anthropic`, `google`, `mistralai`, `meta`, `deepseek`, or `unknown`) |
| Input tokens    | From the invocation result's `usage.prompt_tokens` metadata, when the connector reports it                    |
| Output tokens   | From the invocation result's `usage.completion_tokens` metadata, when the connector reports it                |
| Latency         | Duration of the function invocation                                                                           |
| Rendered prompt | Captured when `store_prompts=True` (the default), redacted before storage                                     |

<Note>
  Semantic Kernel connectors vary in whether they populate token usage metadata on the invocation context. If input/output tokens show as `0`, check whether your connector (e.g. `OpenAIChatCompletion`) surfaces `usage` on the result — this is a Semantic Kernel connector limitation, not something `instrument_semantic_kernel()` controls.
</Note>
