Quickstart: Tracing (Python)

Overview

Phoenix supports three main options to collect traces:

  1. Use Phoenix's decorators to mark functions and code blocks.

  2. Use automatic instrumentation to capture all calls made to supported frameworks.

  3. Use base OpenTelemetry instrumentation. Supported in Python and TS / JS, among many other languages.

This example uses options 1 and 2.

Launch Phoenix

  1. Sign up for an Arize Phoenix account at https://app.phoenix.arize.com/login

  2. Grab your API key from the Keys option on the left bar.

  3. In your code, set your endpoint and API key:

import os

# Add Phoenix API Key for tracing
PHOENIX_API_KEY = "ADD YOUR API KEY"
PHOENIX_ENDPOINT = "https://app.phoenix.arize.com/v1/traces"

os.environ["PHOENIX_CLIENT_HEADERS"] = f"api_key={PHOENIX_API_KEY}"

Connect to Phoenix

To collect traces from your application, you must configure an OpenTelemetry TracerProvider to send traces to Phoenix.

pip install arize-phoenix-otel
from phoenix.otel import register

# configure the Phoenix tracer
tracer_provider = register(
  project_name="my-llm-app", # Default is 'default'
  auto_instrument=True, # See 'Trace all calls made to a library' below
  endpoint=PHOENIX_ENDPOINT,
)
tracer = tracer_provider.get_tracer(__name__)

Trace your own functions

Functions can be traced using decorators:

@tracer.chain
def my_func(input: str) -> str:
    return "output"

Input and output attributes are set automatically based on my_func's parameters and return.

Trace all calls made to a library

Phoenix can also capture all calls made to supported libraries automatically. Just install the respective OpenInference library:

pip install openinference-instrumentation-openai
# Add OpenAI API Key
import os
import openai

os.environ["OPENAI_API_KEY"] = "ADD YOUR OPENAI API KEY"

client = openai.OpenAI()
response = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "Write a haiku."}],
)
print(response.choices[0].message.content)

View your Traces in Phoenix

You should now see traces in Phoenix!

Next Steps

Last updated

Was this helpful?