Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

OpenTelemetry Portkey AI Instrumentation

pypi

This library provides OpenTelemetry instrumentation for Portkey AI.

Installation

pip install opentelemetry-instrumentation-genai-portkey

Usage

from portkey_ai import Portkey
from opentelemetry.instrumentation.genai.portkey import PortkeyInstrumentor

# Instrument Portkey AI
PortkeyInstrumentor().instrument()

client = Portkey(api_key="PORTKEY_API_KEY")

# Chat completion example
response = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "Hello!"}],
)

# Prompt completion example
prompt_response = client.prompts.completions.create(
    prompt_id="YOUR_PROMPT_ID",
    variables={"user_input": "Hello!"},
)

Configuration

Token Usage

Chat and prompt completions record detailed usage when Portkey returns it, for synchronous, asynchronous, and streaming calls:

  • usage.prompt_tokens_details.cache_write_tokens records gen_ai.usage.cache_write.input_tokens, falling back to usage.cache_creation_input_tokens when the nested count is unavailable.

  • usage.prompt_tokens_details.cached_tokens records gen_ai.usage.cache_read.input_tokens, falling back to usage.cache_read_input_tokens when the nested count is unavailable. The two representations are not added together.

  • Explicit text_tokens, image_tokens, and audio_tokens under usage.prompt_tokens_details record the corresponding gen_ai.usage.{text,image,audio}.input_tokens attributes.

  • Explicit text_tokens, image_tokens, and audio_tokens under usage.completion_tokens_details record gen_ai.usage.{text,image,audio}.output_tokens.

  • usage.completion_tokens_details.reasoning_tokens records gen_ai.usage.reasoning.output_tokens without adding to the reported output-token total.

Availability depends on the provider and model. Aggregate input and output totals are preserved, and modality counts are never inferred from those totals. No per-modality cache counts are inferred from the aggregate cache-read count. Only non-negative integer counts, excluding booleans, are accepted. Missing or invalid counts are omitted; streaming updates retain previously reported counts when later chunks omit them. These attributes do not require message-content capture.

The fallback cache fields follow Portkey’s prompt caching response format; nested details include fields preserved by its OpenAI-compatible response passthrough.

Capture Message Content

By default, prompts and completions are not captured. To capture message content, set the environment variable OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT to one of no_content, span_only, event_only, or span_and_event:

export OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT=span_and_event

Uploading Prompts and Completions

To enable the built-in upload completion hook, set:

  • OTEL_INSTRUMENTATION_GENAI_COMPLETION_HOOK=upload

  • OTEL_INSTRUMENTATION_GENAI_UPLOAD_BASE_PATH to an fsspec-compatible URI/path (e.g. /path/to/prompts or gs://my_bucket).

Install the upload extra to pull in fsspec:

pip install opentelemetry-util-genai[upload]

You can also programmatically pass a custom hook when calling instrument():

PortkeyInstrumentor().instrument(completion_hook=my_custom_hook)

Uninstrument

To remove instrumentation from Portkey AI clients, call uninstrument():

PortkeyInstrumentor().uninstrument()

References

Release files for opentelemetry-instrumentation-genai-portkey 1.2b0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for opentelemetry-instrumentation-genai-portkey 1.2b0
File Size Uploaded
opentelemetry_instrumentation_genai_portkey-1.2b0.tar.gz 31.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for opentelemetry-instrumentation-genai-portkey 1.2b0
File Interpreter ABI Platform
opentelemetry_instrumentation_genai_portkey-1.2b0-py3-none-any.whl Python 3 none any Details

Total release size: 48.3 kB

Release files / opentelemetry_instrumentation_genai_portkey-1.2b0.tar.gz

Download URL opentelemetry_instrumentation_genai_portkey-1.2b0.tar.gz
Size 31.4 kB
Tags Source
SHA-256 checksum
How to use checksums
7b4cd134ff676f742529b548ce38ed603458bc6a12f41c539561e72a486c0c20
BLAKE2b-256 checksum
How to use checksums
c7dd28d2676b78689afc9e707746ff2d9583d1929ac083e739e0969c20aef88d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 24, 2026.

Transparency log

Release files / opentelemetry_instrumentation_genai_portkey-1.2b0-py3-none-any.whl

Download URL opentelemetry_instrumentation_genai_portkey-1.2b0-py3-none-any.whl
Size 16.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
879e4858503625171925d41fa3b5840afaa0ca79eb3d741a1ecae13b98325e75
BLAKE2b-256 checksum
How to use checksums
9a6fcc658f4a674ad57a5abfec83253cd1cb4ca26c896a7c5ad29ee96d933eef
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 24, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

1.2b0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page