track_context()
track_context() creates a context manager which defines an Ascerta instrumentation scope. Every inference request in this scope will be instrumented by Ascerta with the properties defined.
Parameters
All parameters are optional and default to None
| Parameter | Type | Notes |
|---|---|---|
limit_ids | list[str] | |
use_case_name | str | Must follow the Use Case naming requirements. |
use_case_id | str | If Noneand the passed use_case_name 1. creates a new ascerta instrumentation scope 2. or is different value comapred to the current ascerta instrumentation scope Then in a new use_case_id will be automatically generated for the new ascerta instrumenation scope. This evaluation occurs every time the decorated function (do_work() in the example) is called. |
use_case_version | int | Takes effect only when this scope creates a new use case Instance. The Ascerta service ignores a version passed for an instance that already exists, such as the one created by ascerta_instrument() global instrumentation. See Use Case Versioning. |
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 ascerta 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 ascerta 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 ascerta_instrument(). Disabling it will limit the functionality of the Ascerta system. See Request and Response Logging |
Example
pip install ascerta openai dotenvfrom ascerta import track_context, ascerta_instrument
from openai import OpenAI
from dotenv import load_dotenv
load_dotenv()
ascerta_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 3 days ago
Did this page help you?