This release is a pre-release and may not be stable for production use.
OpenTelemetry Portkey AI Instrumentation
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
Metadata
Release files for opentelemetry-instrumentation-genai-portkey 1.3b0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| opentelemetry_instrumentation_genai_portkey-1.3b0.tar.gz | 31.8 kB | With provenance | Details for opentelemetry_instrumentation_genai_portkey-1.3b0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| opentelemetry_instrumentation_genai_portkey-1.3b0-py3-none-any.whl | Python 3 | none | any | Details for opentelemetry_instrumentation_genai_portkey-1.3b0-py3-none-any.whl |
Total release size: 48.8 kB
Release files / opentelemetry_instrumentation_genai_portkey-1.3b0.tar.gz
| Download URL | opentelemetry_instrumentation_genai_portkey-1.3b0.tar.gz |
|---|---|
| Size | 31.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
237b5fe68005a49314e831edc717f33c5bf9d24d51243b1122c8b198d63fb168
|
|
BLAKE2b-256 checksum How to use checksums |
649397ee393162f0ad2ba771551e756d51b01212edd911e32a2f726896406ab3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
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 Oct 23, 2026.
Transparency logRelease files / opentelemetry_instrumentation_genai_portkey-1.3b0-py3-none-any.whl
| Download URL | opentelemetry_instrumentation_genai_portkey-1.3b0-py3-none-any.whl |
|---|---|
| Size | 17.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
9cbfbe42772c79d1e6f13e03de01f7a49d01271ebc6abfa9b65bedd5b7d0d883
|
|
BLAKE2b-256 checksum How to use checksums |
69db09639ccb9d20f3c3dd68ea338ccf5b8b76741dd859a93c34b748b18722ff
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
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 Oct 23, 2026.
Transparency logSecurity for opentelemetry-instrumentation-genai-portkey 1.3b0
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.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for opentelemetry_instrumentation_genai_portkey-1.3b0.tar.gz
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 Oct 23, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.