Semantic Kernel (tracing)

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.

Create an account at platform.respan.ai and grab an API key.

Run npx @respan/cli setup to set up with your coding agent.

See Semantic Kernel gateway setup to route model calls through the Respan gateway.

Setup

1

Install packages

$pip install respan-ai respan-instrumentation-semantic-kernel semantic-kernel openai
2

Set environment variables

$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.

3

Initialize and run

1import asyncio
2import os
3
4from respan import Respan, workflow
5from respan_instrumentation_semantic_kernel import SemanticKernelInstrumentor
6from semantic_kernel import Kernel
7from semantic_kernel.connectors.ai.open_ai import OpenAIChatCompletion
8from semantic_kernel.connectors.ai.open_ai import OpenAIChatPromptExecutionSettings
9from semantic_kernel.functions import KernelArguments
10
11respan = Respan(
12 app_name="semantic-kernel-demo",
13 instrumentations=[SemanticKernelInstrumentor()],
14)
15
16kernel = Kernel()
17kernel.add_service(
18 OpenAIChatCompletion(
19 service_id="chat",
20 ai_model_id="gpt-4o-mini",
21 api_key=os.environ["OPENAI_API_KEY"],
22 )
23)
24
25@workflow(name="semantic_kernel_chat")
26async def run_chat() -> str:
27 result = await kernel.invoke_prompt(
28 "Reply in one sentence: what does Semantic Kernel help developers build?",
29 arguments=KernelArguments(
30 settings=OpenAIChatPromptExecutionSettings(
31 service_id="chat",
32 temperature=0.2,
33 max_tokens=80,
34 )
35 ),
36 )
37 return str(result)
38
39async def main() -> None:
40 print(await run_chat())
41
42asyncio.run(main())
43respan.flush()
44respan.shutdown()
4

View your trace

Open the Traces page to see kernel function, chat completion, plugin/tool, and error spans.

Configuration

ParameterTypeDefaultDescription
api_keystr | NoneRESPAN_API_KEYRespan API key used to export traces.
base_urlstr | NoneRESPAN_BASE_URLOptional Respan trace export API URL.
instrumentationslist[]Include SemanticKernelInstrumentor() to activate Semantic Kernel tracing.
customer_identifierstr | NoneNoneDefault customer identifier for all spans.
metadatadict | NoneNoneDefault metadata attached to all spans.
environmentstr | NoneNoneEnvironment tag, for example "production".

What gets traced

Semantic Kernel operationRespan spanNotes
Kernel function invocationtaskCaptures function name, arguments, result, and failures.
Chat or text completionchat or textCaptures prompt/completion content, model, and token usage when Semantic Kernel exposes it.
Plugin tool executiontoolCaptures plugin function name, arguments, result, and errors.

Attributes

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

1from respan import Respan
2from respan_instrumentation_semantic_kernel import SemanticKernelInstrumentor
3
4respan = Respan(
5 instrumentations=[SemanticKernelInstrumentor()],
6 customer_identifier="user_123",
7 metadata={"service": "semantic-kernel-api", "version": "1.0.0"},
8 environment="production",
9)

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