Limit APIs

Install the following packages to run the samples on this page and run the following code

pip install payi dotenv

Create a limit: limits.create()

create() will succeed and return the previously created limitwhen a previously createdlimit_id is specifed to a subsequent call to limits.create() and the configuration max and thresholdparameters match the initially created `limit. If they do not different match, the operation will fail with an error and raises an exception.

ParameterTypeDescription
limit_namestrFriendly name
limit_idOptional strLimit identifier. If none provided, Payi will return a limit_id in the response.
maxfloatMaximum limit in USD
thresoldOptional floatPercentage of the limit's maximum, valid valures are between 0.75 and 0.99
limit_typestr = allow or blockallow limits will allow all requests to proceed, even if the max has been reached or exceeded.

block limits will prevent requests from proceeding if the max has been reached or exceeded. block limits are only allowed to valid when Payi is a proxy for the inference request.
propertiesOptional dict[str, str]Property Bag associated with the limit
from payi import Payi
from dotenv import load_dotenv

load_dotenv()

payi = Payi()

# more common: specify a deterministic limit_id so it can be reused later without additional storage
# less common: Payi generates a limit_id
response = payi.limits.create(
  limit_name="Limit 1",
  limit_id="limit_1_id",
  max=10.0,           
  limit_type="allow",  
  threshold=0.80       
)

# less common: Payi generates a limit_id
response = payi.limits.create(
  limit_name="Limit 2",
  max=10.0,           
  limit_type="allow",  
  threshold=0.80       
)
limit2_id = response.limit.limit_id

Updating a limit: limits.update()

ParamterType
limit_idstr
limit_nameOptional str
maxOptional str
from payi import Payi
from dotenv import load_dotenv

load_dotenv()

payi = Payi()  
limit_id = "update_limit"

response = payi.limits.create(
  limit_name="Updated Limit",
  limit_id=limit_id,
  max=15.0,           
  limit_type="allow",  
  threshold=0.80       
)

# Update max 
updated_response = payi.limits.update(
  limit_id=limit_id,
  max=30.0
)

# Update name
updated_response = payi.limits.update(
  limit_id=limit_id,
  limit_name="New Updated Limit"
)

Enumerating limits: limits.list()

The SDK automatically handles server request pagination.

ParameterTypeDescription
limitOptional intLimit the the number of limitobjects returned by the underlying Payi service API
limit_nameOptional strFilter the results to limitobjects matching limit_name
sort_ascendingOptional boolSort the results by creation date. Default is True
from payi import Payi
from dotenv import load_dotenv

load_dotenv()

payi = Payi()  

response = payi.limits.create(
  limit_name="1 Limit",
  max=15.0,           
  limit_type="allow",  
  threshold=0.80       
)
response = payi.limits.create(
  limit_name="2 Limit",
  max=20.0,           
  limit_type="allow",  
  threshold=0.90       
)

for limit in payi.limits.list():
  print(f"Limit: {limit.limit_name}")

You don't need to manually handle pagination or worry about cursor management - the SDK takes care of making multiple API calls as needed when you iterate through the results. This makes it easy to work with large collections of limits without worrying about pagination implementation details.

Resetting a limit: limits.reset()

Reset a limit back to zero

ParameterType
limit_idstr
from payi import Payi
from dotenv import load_dotenv

load_dotenv()

payi = Payi()  
limit_id = "reset_limit"

response = payi.limits.create(
  limit_name="Reset limit",
  limit_id=limit_id,
  max=15.0,           
  limit_type="allow",  
  threshold=0.80       
)

response = payi.limits.reset(limit_id=limit_id)

Deleting a limit: limits.delete()

It is NOT RECOMMEND to delete limts unless absolutely necessary. Deleting a limit does not delete inference requests already associated with the limit. Deletion only prevents future use of the limit.

NOTE: When experimenting with limits or running the examples in this guide multiple times, you may need to delete created limits.

from payi import Payi
from dotenv import load_dotenv

load_dotenv()

payi = Payi()  
limit_id = "delete_limit"

response = payi.limits.create(
  limit_name="Delete limit",
  limit_id=limit_id,
  max=15.0,           
  limit_type="allow",  
  threshold=0.80       
)

payi.limits.delete(limit_id=limit_id)