> ## Documentation Index
> Fetch the complete documentation index at: https://docs.openlayer.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Portkey

> Learn how to trace and monitor Portkey completions with Openlayer

[Portkey](https://portkey.ai/) is an LLM gateway. You send chat completions through a
single client, and Portkey routes them to the provider behind a model slug.

If you are building an AI system with Portkey and want to evaluate it, you can use
the Python [SDK](/api-reference/sdk/overview) to make Openlayer part of your workflow.
This integration is Python-only — the TypeScript SDK does not include a Portkey tracer.

This guide shows how you can trace Portkey `chat.completions.create` calls and
publish them to Openlayer.

## Evaluating Portkey completions

You can set up Openlayer tests to evaluate your Portkey completions in
[monitoring](/monitoring/overview). Instrument your code so each request is traced and
published to the platform.

Install the Portkey SDK alongside Openlayer:

```bash theme={null}
pip install portkey-ai
```

Traced calls show up as **Portkey Chat Completion** steps. If a Portkey call is just
one step of your AI system, use the snippets on this page together with
[tracing](/monitoring/tracing). In that case, the completion is added as a step of a
larger trace.

After requests are continuously published and logged by Openlayer, you can
[create tests](/tests/overview) that run at a regular cadence on top of them.

Refer to the [Monitoring overview](/monitoring/overview) for details on Openlayer's
monitoring mode, or to the [Tracing guide](/monitoring/tracing) to understand how to
trace more complex systems.

## Auto-instrumentation with `init()`

`init()` auto-instruments installed LLM SDKs, including Portkey when the `portkey_ai`
package is present (registry name `"portkey"`). Call it before you create a Portkey
client:

```python theme={null}
# 1. Set the environment variables
import os

os.environ["OPENLAYER_API_KEY"] = "YOUR_OPENLAYER_API_KEY_HERE"
os.environ["OPENLAYER_INFERENCE_PIPELINE_ID"] = "YOUR_OPENLAYER_INFERENCE_PIPELINE_ID_HERE"

# 2. Call `init` to auto-instrument the installed LLM SDKs (Portkey, etc.)
from portkey_ai import Portkey
from openlayer.lib import init

init()

portkey = Portkey(api_key="YOUR_PORTKEY_API_KEY")  # auto-traced by Openlayer

# 3. From now on, every chat completion call with
# the `portkey` client is traced and published to Openlayer. E.g.,
response = portkey.chat.completions.create(
    model="@YOUR_PROVIDER_SLUG/MODEL_NAME",
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "What is Portkey"},
    ],
    max_tokens=512,
)
```

Once the code is instrumented, Portkey chat completions are automatically published to
Openlayer. If you navigate to the "Data" page of your Openlayer data source, you can
see the traces for each request.

## Manual `trace_portkey()`

If you prefer not to auto-instrument every installed SDK, call `trace_portkey()`
before you use Portkey. Tracing then happens automatically on
`chat.completions.create`:

```python theme={null}
from portkey_ai import Portkey
from openlayer.lib import trace_portkey

# Enable tracing
trace_portkey()

# Use Portkey normally - tracing happens automatically
portkey = Portkey(api_key = "YOUR_PORTKEY_API_KEY")
response = portkey.chat.completions.create(
    model = "@YOUR_PROVIDER_SLUG/MODEL_NAME",
    messages = [
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "What is Portkey"}
    ],
    inference_id="custom-id-123",  # Optional Openlayer parameter
    max_tokens = 512
)
```

You still need `OPENLAYER_API_KEY` and `OPENLAYER_INFERENCE_PIPELINE_ID` in the
environment.

The optional `inference_id` kwarg is popped before the request is sent to Portkey.

## What is captured

Openlayer records streaming and non-streaming `chat.completions.create` calls. For
each **Portkey Chat Completion** step, the tracer captures:

* `start_time`
* `end_time`
* `latency`
* `tokens`
* `prompt_tokens`
* `completion_tokens`
* `model`
* `model_parameters`
* `raw_output`
* `inputs`
* Portkey-specific metadata (base URL, `x-portkey-*` headers if available)
