Cost attributed signals

Prerequisites

Track how much each billable event costs you

This is directly related to understanding your margins. Given that you determine how much you charge based on signals, it’s useful to know the cost of the work that results in those signals.

This is a combination of the two previous sections, with a special API call for sending a signal.

There’s only one new requirement: the signals should be sent from the paid_tracing context where AI calls are made.

1from openai import OpenAI
2from paid.tracing import initialize_tracing, paid_autoinstrument, paid_tracing, cost_attributed_signal
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"):
10# ... your business logic, occasional AI calls ...
11openai_client.chat.completions.create(
12 model="gpt-4o-mini",
13 messages=[{"role": "user", "content": "Summarize this order."}],
14)
15# ... your business logic, occasional AI calls ...
16
17# emit a signal (order relative to the AI call doesn't matter)
18res = cost_attributed_signal(
19 event_name="email_sent",
20 data={"some_data": "some_value"},
21)
22print("Signal sent:", res)