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

Portkey

Trace Portkey gateway calls with cetra through the OpenAI client.

Terminal window
pip install "cetra-sdk[openai]" openai
import os
import cetra
from openai import OpenAI
cetra.init(instrument=["openai"])
client = OpenAI(
base_url="https://api.portkey.ai/v1",
api_key=os.environ["PORTKEY_API_KEY"],
default_headers={
"x-portkey-provider": "openai",
"Authorization": f"Bearer {os.environ['OPENAI_API_KEY']}",
},
)
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "What is the capital of France?"}],
)
print(response.choices[0].message.content)
cetra.flush()
  • Each chat completion is an llm span with the provider portkey.
  • Input and output token counts come from the response.
  • Cost is the model publisher’s price, as described under Limits.
  • With cetra.init(instrument=["openai"]), the endpoint host is in server.address, and cetra names the provider from it.

The self-hosted and Enterprise gateway can export GenAI spans over OTLP behind experimental environment variables: EXPERIMENTAL_GEN_AI_OTEL_TRACES_ENABLED=true, EXPERIMENTAL_GEN_AI_OTEL_EXPORTER_OTLP_ENDPOINT, EXPERIMENTAL_GEN_AI_OTEL_EXPORTER_OTLP_HEADERS (comma-separated key=value pairs) and EXPERIMENTAL_GEN_AI_OTEL_EXPORTER_OTLP_RESOURCE_ATTRIBUTES. The hosted gateway has no export to an OTLP endpoint of your own, and the open-source npm gateway does not include this code.

Setup only: cetra has not verified this export. Portkey’s docs do not say whether it sends HTTP or gRPC, and their example’s endpoint path differs from the documented one, so check what your instance sends before you rely on it.

  • Setup only: no spans of Portkey are captured in the repository.
  • Cost is the model publisher’s price when the model names its publisher (vendor/model, or a bare name a publisher has a row for); Portkey’s fees are not included. Models no publisher row matches have no built-in price.
  • The OTLP export above is experimental and not verified by cetra.
  • Do not instrument both sides: an app with its own OpenTelemetry instrumentation behind a traced gateway counts each call twice, because the gateway joins the app’s trace through traceparent and its span carries the same tokens and cost. Trace the gateway (it covers several apps and languages) or the app, not both. This is not verified for Portkey.
  • With plain OpenInference or the official OpenAI instrumentation in Python, the provider shows as openai: they do not record the endpoint. Use cetra.init(instrument=["openai"]) or OpenLLMetry.
  • If a field is missing, look at the span’s Attributes tab and compare it with Span attributes.