Portkey (tracing)

The Portkey Python SDK routes requests through Portkey Gateway. respan-instrumentation-portkey activates the Portkey OpenInference wrapper and normalizes emitted spans to the Respan tracing pipeline.

  1. Sign up - Create an account at platform.respan.ai
  2. Create an API key - Generate one on the API keys page

See Portkey gateway setup to replace Portkey Gateway routing with the Respan gateway.

Setup

1

Install packages

$pip install respan-ai respan-instrumentation-portkey portkey-ai python-dotenv
2

Set environment variables

$export RESPAN_API_KEY="YOUR_RESPAN_API_KEY"
$export PORTKEY_API_KEY="YOUR_PORTKEY_API_KEY"
$export PORTKEY_PROVIDER="@YOUR_PROVIDER"

Use PORTKEY_CONFIG instead of PORTKEY_PROVIDER if your Portkey account uses a config slug.

Optional:

$export RESPAN_BASE_URL="https://api.respan.ai/api"
$export PORTKEY_MODEL="gpt-4o-mini"
3

Initialize and run

1import os
2
3from dotenv import load_dotenv
4from portkey_ai import Portkey
5from respan import Respan, workflow
6from respan_instrumentation_portkey import PortkeyInstrumentor
7
8load_dotenv()
9
10respan = Respan(
11 api_key=os.environ["RESPAN_API_KEY"],
12 base_url=os.getenv("RESPAN_BASE_URL", "https://api.respan.ai/api"),
13 instrumentations=[PortkeyInstrumentor()],
14)
15client = Portkey(
16 api_key=os.environ["PORTKEY_API_KEY"],
17 provider=os.getenv("PORTKEY_PROVIDER"),
18 config=os.getenv("PORTKEY_CONFIG"),
19)
20
21
22@workflow(name="portkey_chat_completion")
23def run_chat() -> str:
24 response = client.chat.completions.create(
25 model=os.getenv("PORTKEY_MODEL", "gpt-4o-mini"),
26 messages=[{"role": "user", "content": "Say hello from Portkey."}],
27 )
28 return response.choices[0].message.content or ""
29
30
31try:
32 print(run_chat())
33finally:
34 respan.flush()
35 respan.shutdown()
4

View your trace

Open the Traces page and search for the workflow name portkey_chat_completion.

Configuration

ParameterTypeDefaultDescription
**instrumentor_kwargsdict{}Extra keyword arguments forwarded to the upstream Portkey OpenInference instrumentor.

Attributes

Attach customer identifiers, thread IDs, workflow names, and metadata to Portkey calls with propagate_attributes.

1from respan import propagate_attributes
2
3with propagate_attributes(
4 customer_identifier="user_123",
5 thread_identifier="conversation_456",
6 trace_group_identifier="portkey_support_chat.workflow",
7 metadata={"plan": "pro", "workflow_name": "portkey_support_chat.workflow"},
8):
9 response = client.chat.completions.create(
10 model="gpt-4o-mini",
11 messages=[{"role": "user", "content": "Summarize our support policy."}],
12 )
AttributeTypeDescription
customer_identifierstrIdentifies the end user in Respan analytics.
thread_identifierstrGroups related messages into a conversation.
trace_group_identifierstrGroups spans by workflow name.
metadatadictCustom key-value pairs merged with default metadata.

Examples

Async chat

1import asyncio
2import os
3
4from portkey_ai import AsyncPortkey
5
6
7async def main() -> None:
8 client = AsyncPortkey(
9 api_key=os.environ["PORTKEY_API_KEY"],
10 provider=os.getenv("PORTKEY_PROVIDER"),
11 config=os.getenv("PORTKEY_CONFIG"),
12 )
13 try:
14 response = await client.chat.completions.create(
15 model="gpt-4o-mini",
16 messages=[{"role": "user", "content": "Write a short haiku about traces."}],
17 )
18 print(response.choices[0].message.content)
19 finally:
20 await client.close()
21
22
23asyncio.run(main())