Cost traces

Prerequisites

Capture your AI call

To capture the AI call, there are 2 steps:

  1. Let Paid autoinstrument your AI libraries at the initialization of your application (preferably in the main thread).
  2. Wrap your AI call in the Paid tracing context. Whether they’re wrapped directly or somewhere down the stack does not matter.
    • If the AI call is not wrapped, the cost trace is still recorded, though unattributed to a customer or product.
1from openai import OpenAI
2from paid.tracing import initialize_tracing, paid_autoinstrument, paid_tracing
3
4initialize_tracing(api_key="PAID_API_KEY")
5paid_autoinstrument(libraries=["openai"]) # or leave empty to autoinstrument all libraries
6
7openai_client = OpenAI(api_key="OPENAI_API_KEY")
8
9with paid_tracing("your_external_customer_id", external_product_id="your_external_product_id"):
10openai_client.chat.completions.create(
11 model="gpt-4o-mini",
12 messages=[{"role": "user", "content": "Summarize this order."}],
13)