> This page is for version v2 (default).
> For other versions, use one of these documentation indexes:
> - v2 (default): https://docs.paid.ai/v-2/llms.txt
> - v1: https://docs.paid.ai/v-1/llms.txt

> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.paid.ai/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://docs.paid.ai/_mcp/server.

# Cost traces

> Use telemetry to capture AI costs

## Prerequisites

* A [Paid API key](https://app.paid.ai/settings/api-keys).
* The Paid SDK installed in the service that makes AI provider calls.
* Optional: [customers](https://app.paid.ai/customers) and [products](https://app.paid.ai/products) with stable external IDs when you want costs attributed to a specific account or product. You can also create them through the [Products API](/api-reference/api-reference/products/create-product) and [Customers API](/api-reference/api-reference/customers/create-customer).

## 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.

#### Python

```python
from openai import OpenAI
from paid.tracing import initialize_tracing, paid_autoinstrument, paid_tracing

initialize_tracing(api_key="PAID_API_KEY")
paid_autoinstrument(libraries=["openai"]) # or leave empty to autoinstrument all libraries

openai_client = OpenAI(api_key="OPENAI_API_KEY")

with paid_tracing("your_external_customer_id", external_product_id="your_external_product_id"):
    openai_client.chat.completions.create(
        model="gpt-4o-mini",
        messages=[{"role": "user", "content": "Summarize this order."}],
    )
```