Skip to content
GitHubLog inStart free
cetra is not released yet. These docs describe the development version.

Semantic Kernel

cetra traces Semantic Kernel agent runs as agent, model call and tool spans, from Semantic Kernel’s own diagnostics.

Terminal window
pip install cetra-sdk semantic-kernel openai

Turn on Semantic Kernel’s own diagnostics, then send its spans to cetra.

Semantic Kernel writes its spans to the global OpenTelemetry tracer provider. Start cetra with use_global_provider=True so that it joins that provider. Set CETRA_API_KEY to your sk-cetra-… key and OPENAI_API_KEY to your OpenAI key.

Semantic Kernel reads its switches when it is imported, so set them before the import.

import asyncio
import os
os.environ["SEMANTICKERNEL_EXPERIMENTAL_GENAI_ENABLE_OTEL_DIAGNOSTICS"] = "true"
os.environ["SEMANTICKERNEL_EXPERIMENTAL_GENAI_ENABLE_OTEL_DIAGNOSTICS_SENSITIVE"] = "true"
import cetra
cetra.init(use_global_provider=True)
from openai import AsyncOpenAI # noqa: E402
from semantic_kernel.agents import ChatCompletionAgent # noqa: E402
from semantic_kernel.connectors.ai.open_ai import OpenAIChatCompletion # noqa: E402
from semantic_kernel.functions import kernel_function # noqa: E402
class WeatherPlugin:
"""Plugin that answers the weather question."""
@kernel_function(name="get_weather", description="Get the weather")
def get_weather(self, city: str) -> str:
return f"sunny in {city}"
service = OpenAIChatCompletion(
ai_model_id="gpt-4o-mini",
async_client=AsyncOpenAI(api_key=os.environ["OPENAI_API_KEY"]),
)
async def run() -> None:
agent = ChatCompletionAgent(
service=service,
name="WeatherAgent",
instructions="You are a weather bot.",
plugins=[WeatherPlugin()],
)
await agent.get_response(messages="What is the weather in Paris?")
asyncio.run(run())
cetra.flush()
  • Python

    With one plugin function, a run produces:

    • an agent span named invoke_agent WeatherAgent, with the input and output messages and the tool definitions;
    • an llm span for each model call, with the provider openai, the model, the token counts and the cost computed from the built-in prices;
    • a tool span for each plugin function, with the arguments as its input and the result as its output.
  • .NET

    With one plugin function, a chat call produces:

    • an llm span named chat gpt-4o-mini for each model call, with the provider openai, the model, the input and output messages, the token counts and the cost computed from the built-in prices;
    • a tool span for each plugin function, with the arguments as its input and the result as its output.
  • Python
    • Semantic Kernel sends the message content of model calls as OTLP log records. cetra does not store log records, so the llm spans show no messages. The agent span still carries its input and output.
    • The fixture was captured with semantic-kernel 1.47.0 and the OpenTelemetry SDK 1.45.2.
  • .NET
    • Without the two AppContext switches, Semantic Kernel emits no model-call spans at all: only the tool spans arrive.
    • The spans have no parent links: every span is its own trace, so a tool span is not linked to the model call that requested it.
    • The per-message finish reason from Semantic Kernel’s gen_ai.choice event is read and lowercased, so ToolCalls becomes toolcalls (not tool_calls). Only the span-level gen_ai.response.finish_reason and the URL-shaped server.address are not read.
    • OpenTelemetry .NET 1.21.0 ignores OTEL_EXPORTER_OTLP_TRACES_ENDPOINT. Set OTEL_EXPORTER_OTLP_ENDPOINT, the base URL without the /v1/traces path; the exporter appends the path itself.
    • The capture sent no Authorization header; the header setting above is the library’s documented option, not part of the captured run.
    • The .NET fixture was captured with Microsoft.SemanticKernel 1.82.0 and OpenTelemetry .NET 1.21.0.
  • If a field is missing, look at the span’s Attributes tab and compare it with Span attributes.