Python SDK Guide
Install the KLA OpenTelemetry Python SDK and send a first trace to Lineage Explorer.
The KLA Python SDK package is kla-otel-python. Import it as kla_otel_python. It initializes OpenTelemetry, adds KLA GenAI attributes, and exports helpers for FastAPI middleware, LangChain instrumentation, sampling, and evaluation publishing.
Install
pip install kla-otel-python
For the FastAPI quickstart:
pip install "kla-otel-python[fastapi]" uvicorn
Set the service and collector before the app starts:
export OTEL_SERVICE_NAME=claims-agent-api
export KLA_TENANT_ID=kpmg-reporting
export OTEL_EXPORTER_OTLP_ENDPOINT=<kla-otlp-grpc-endpoint>
export KLA_PII_MASK=true
FastAPI First Trace
Load the SDK before creating the app and any model clients.
# main.py
import kla_otel_python
from fastapi import FastAPI
from kla_otel_python import add_genai_attributes, tracer
from kla_otel_python.middleware import KLATracingMiddleware
from kla_otel_python.semantic_conventions import GenAIAttributes
app = FastAPI()
app.add_middleware(KLATracingMiddleware)
@app.post("/messages")
async def create_message(payload: dict):
with tracer.start_as_current_span("agent.reply") as span:
prompt = str(payload.get("prompt", ""))
add_genai_attributes(
span,
{
GenAIAttributes.SYSTEM: "openai",
GenAIAttributes.REQUEST_MODEL: "gpt-4o-mini",
GenAIAttributes.USAGE_PROMPT_TOKENS: 24,
GenAIAttributes.USAGE_COMPLETION_TOKENS: 12,
GenAIAttributes.USAGE_TOTAL_TOKENS: 36,
},
)
return {"response": f"Handled: {prompt}"}
Run it:
uvicorn main:app --reload
curl -s http://localhost:8000/messages \
-H 'content-type: application/json' \
-d '{"prompt":"Summarize the claim"}'
Open KLA Control Plane, go to Lineage Explorer, and filter by claims-agent-api. The span should include gen_ai.system, gen_ai.request.model, and token usage attributes.
LangChain
Install the LangChain extra and enable the instrumentor before constructing chains, agents, or tools.
pip install "kla-otel-python[langchain]"
import kla_otel_python
from kla_otel_python.instrumentors import LangChainInstrumentor
LangChainInstrumentor().instrument()
For explicit model spans:
from kla_otel_python import add_genai_attributes, tracer
from kla_otel_python.semantic_conventions import GenAIAttributes
with tracer.start_as_current_span("langchain.agent.answer") as span:
result = chain.invoke({"input": "Review this transaction"})
add_genai_attributes(
span,
{
GenAIAttributes.SYSTEM: "openai",
GenAIAttributes.REQUEST_MODEL: "gpt-4o-mini",
GenAIAttributes.USAGE_TOTAL_TOKENS: result.get("usage", {}).get("total_tokens"),
},
)
Package Verification
The SDK package dry-run runs in CI with:
python -m pip install --upgrade build twine
python -m build
python -m twine check dist/*
