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

ParameterTypeNotes
limit_idslist[str]
use_case_namestr
use_case_idstrIf 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_stepstr
user_idstr
account_namestr
request_propertiesdict[str, str]
use_case_propertiesdict[str, str]
price_as_categorystrOverride the payi Category for the instrumented inference calls. Useful when using the OpenAI client to send requests to non OpenAI models.
price_as_resourcestrOverride the payi Resource for the instrumented inference calls. Useful when the inference calls specify a deployment name, not a model name.
resource_scopestrApplies a deployment scope to the instrumented Resource . Example values (but not limited to) are global, datazone, and region.
log_prompt_and_responseboolOverrides the global log_prompt_and_response setting created by payi_instrument()

Example

pip install payi openai dotenv
from 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"}
    	])


Did this page help you?