track_context()
track_context() creates a context manager which defines a Pay-i instrumentation scope. Every inference request in this scope will be instrumented by Pay-i with the properties defined.
Parameters
All parameters are optional and default to None
| Parameter | Type | Notes |
|---|---|---|
limit_ids | list[str] | |
use_case_name | str | |
use_case_id | str | If Noneand the passed use_case_name 1. creates a new payi instrumentation scope 2. or is different value comapred to the current payi instrumentation scope Then in a new use_case_id will be automatically generated for the new payi instrumenation scope. This evaluation occurs every time the decorated function (do_work() in the example) is called. |
use_case_step | str | |
user_id | str | |
account_name | str | |
request_properties | dict[str, str] | |
use_case_properties | dict[str, str] | |
price_as_category | str | Override the payi Category for the instrumented inference calls. Useful when using the OpenAI client to send requests to non OpenAI models. |
price_as_resource | str | Override the payi Resource for the instrumented inference calls. Useful when the inference calls specify a deployment name, not a model name. |
resource_scope | str | Applies a deployment scope to the instrumented Resource . Example values (but not limited to) are global, datazone, and region. |
log_prompt_and_response | bool | Overrides the global log_prompt_and_response setting created by payi_instrument() |
Example
pip install payi openai dotenvfrom payi.lib.instrument import track_context, payi_instrument
from openai import OpenAI
from dotenv import load_dotenv
load_dotenv()
payi_instrument()
openai = OpenAI()
# A new use_case_id will be generated
with track_context(use_case_name="teach_me", use_case_step="one"):
response = openai.chat.completions.create(
model="gpt-5.5",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Hello, how can I use the API?"}
])
# Same use_case_id will be used since use_case_name has not changed
with track_context(use_case_step="two"):
response = openai.chat.completions.create(
model="gpt-5.5",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Tell me a one line joke"}
])Updated about 1 month ago
Did this page help you?