Guides

Day 1: Basic Instrumentation with payi_instrument()

📋 Overview

Welcome to our learning path to onboard to Pay-i! This series is designed to progressively introduce you to Pay-i's features, starting with the simplest concepts and building toward more advanced functionalities.

🕹️Interactive Getting Started Guide

This guided tutorial will walk you through getting set up with your first app. For the best experience, maximize the interactive tutorial and turn on audio.

🕹️Interactive Instrumentation Guide

Pay-i supports multiple types of instrumentation to support a wide variety of use case types and environments. This interactive guide walks through the available instrumentation options. For the best experience, maximize the interactive tutorial and turn on audio.

🚀 Day 1: Getting Started

Instrument your GenAI provider calls with Pay-i to automatically track costs, manage budgets, analyze usage patterns, and much more This quickstart will guide you through the complete process from creating an application to viewing your tracked events.

1️⃣ Create an Application in Pay-i Developer Portal

Before you begin coding, you'll need to set up your application and get your API key:

  1. Log in to developer.pay-i.com

  2. Click on the +Create Application button in the top-right corner

  3. Enter an application name (e.g., "Quickstart"). You can optionally upload an icon and and select an Application Group.

  4. Once created, the Application Settings page for the new app will open automatically. Click on Application API Keys, and then copy your Primary API Key for use in the next steps.

Note about Groups: Each user gets their own personal group for private applications that aren't shared with others. If you need to share applications with team members, you'll need to move them to a shared group. Groups help organize related applications and provide aggregated metrics. For more information, see Organizational Structure.

2️⃣ Install SDK

Install the Pay-i SDK along with your AI provider's SDK. We will use OpenAI as an example:

pip install --upgrade payi openai
%pip install --upgrade payi openai

3️⃣ Set API keys

Store API keys in a .env file in your project directory (recommended):

  1. Install python-dotenv:
pip install python-dotenv
  1. Create a .env file with your API keys:
OPENAI_API_KEY=sk-...  # Your OpenAI API key
PAYI_API_KEY=sk-...   # Your Pay-i API key from step 1
  1. Add .env to your .gitignore file to prevent accidentally committing your API keys.

4️⃣ Set up Pay-i Instrumentation

Initialize Pay-i instrumentation to track your GenAI usage:

import os
from dotenv import load_dotenv
from openai import OpenAI
from payi.lib.instrument import payi_instrument

# Load environment variables from .env file
load_dotenv()

# Initialize Pay-i instrumentation
payi_instrument()

# Initialize OpenAI
client = OpenAI()

5️⃣ Use Normally

Make API calls as usual with the AI Provider's client:

response = client.chat.completions.create(
    model="gpt-3.5-turbo",
    messages=[{"role": "user", "content": "Hello, world!"}]
)

print(response.choices[0].message.content)

6️⃣ View Your Events

After running your code, you can view your tracked events:

  1. Return to developer.pay-i.com
  2. Select your application (created in step 1)
  3. Click the Refresh button to view your request activity, including costs and usage details in the dashboard

Note: While Pay-i tracks the data in real-time, the dashboard does not update in real-time to prevent the data from shifting while you are viewing an app with live traffic. Each page has a "Refresh Data" button to bring in the latest data.

Default Use Case

When you use payi_instrument() without any additional configuration, Pay-i automatically assigns all tracked calls to a default Use Case.

The default use case is named after your Python file name which calls payi_instrument(). For example, if your code is running in a file called summarizer.py, all your GenAI calls will be assigned to a Use Case named "summarizer".

This automatic naming is great for development and getting started quickly. For testing and production environments, however, you'll want to specify a more meaningful default Use Case name as a parameter to payi_instrument(). The available parameters for payi_instrument() can be found in payi_instrument().