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

ParameterTypeNotes
limit_idslist[str]
use_case_namestrMust follow the Use Case naming requirements.
use_case_idstrIf 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_versionintTakes 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_stepstr
user_idstr
account_namestr
request_propertiesdict[str, str]
use_case_propertiesdict[str, str]
price_as_categorystrOverride the ascerta Category for the instrumented inference calls. Useful when using the OpenAI client to send requests to non OpenAI models.
price_as_resourcestrOverride the ascerta 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 ascerta_instrument(). Disabling it will limit the functionality of the Ascerta system. See Request and Response Logging

Example

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

Did this page help you?