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

OpenAI

cetra traces OpenAI chat completions, Responses API calls and embeddings, with their messages, tool calls, token counts and cost.

Terminal window
pip install "cetra-sdk[openai]" openai

Turn on the instrumentation when you start cetra, then call OpenAI as usual. The spans nest under your own spans and carry the session, user, tags and metadata of the current context.

import cetra
from openai import OpenAI
cetra.init(instrument=["openai"])
client = OpenAI()
client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "What is the weather in Paris?"}],
)
cetra.flush()
  • Each chat completion and each Responses API call is an llm span with the provider openai, the input and output as chat messages, and the tool definitions you passed.
  • A tool call in the answer shows as a tool call in the output messages. The tool result you send back shows in the next call’s input.
  • Token counts include the cached input tokens when OpenAI reports them. cetra computes the cost from its price table.
  • Streamed answers record the time to first token with the OpenInference and OpenLLMetry instrumentations. The streamed fixture of the official OpenTelemetry instrumentation has no time to first token.
  • An embeddings call is an embedding span with the input token count and a cost.
  • The fixtures were captured with the OpenAI Python SDK 3.45.0 and these instrumentations: OpenInference 0.1.69, OpenLLMetry 0.62.4 and the official opentelemetry-instrumentation-openai-v2 2.4b0. Other versions are not covered.
  • Embedding vectors are not stored. Pass capture_content=False (Python) or captureContent: false to the helper and to init (TypeScript) to keep prompts and answers out of cetra.
  • If you use another instrumentation library, see OpenInference, OpenLLMetry or OpenTelemetry GenAI.