aixplain.modules.api_key
APIKeyLimits Objects
class APIKeyLimits()
Rate limits configuration for an API key.
This class defines the rate limits that can be applied either globally to an API key or specifically to a model.
Attributes:
token_per_minute
int - Maximum number of tokens allowed per minute.token_per_day
int - Maximum number of tokens allowed per day.request_per_minute
int - Maximum number of requests allowed per minute.request_per_day
int - Maximum number of requests allowed per day.model
Optional[Model] - The model these limits apply to, if any.
__init__
def __init__(token_per_minute: int,
token_per_day: int,
request_per_minute: int,
request_per_day: int,
model: Optional[Union[Text, Model]] = None)
Initialize an APIKeyLimits instance.
Arguments:
token_per_minute
int - Maximum number of tokens per minute.token_per_day
int - Maximum number of tokens per day.request_per_minute
int - Maximum number of requests per minute.request_per_day
int - Maximum number of requests per day.model
Optional[Union[Text, Model]], optional - The model to apply limits to. Can be a model ID or Model instance. Defaults to None.
APIKeyUsageLimit Objects
class APIKeyUsageLimit()
__init__
def __init__(daily_request_count: int,
daily_request_limit: int,
daily_token_count: int,
daily_token_limit: int,
model: Optional[Union[Text, Model]] = None)
Get the usage limits of an API key globally (model equals to None) or for a specific model.
Arguments:
daily_request_count
int - number of requests madedaily_request_limit
int - limit of requestsdaily_token_count
int - number of tokens useddaily_token_limit
int - limit of tokensmodel
Optional[Union[Text, Model]], optional - Model which the limits apply. Defaults to None.
APIKey Objects
class APIKey()
An API key for accessing aiXplain services.
This class represents an API key with its associated limits, budget, and access controls. It can have both global rate limits and model-specific rate limits.
Attributes:
id
int - The ID of this API key.name
Text - A descriptive name for the API key.budget
Optional[float] - Maximum spending limit, if any.global_limits
Optional[APIKeyLimits] - Rate limits applied globally.asset_limits
List[APIKeyLimits] - Rate limits for specific models.expires_at
Optional[datetime] - Expiration date and time.access_key
Optional[Text] - The actual API key value.is_admin
bool - Whether this is an admin API key.
__init__
def __init__(name: Text,
expires_at: Optional[Union[datetime, Text]] = None,
budget: Optional[float] = None,
asset_limits: List[APIKeyLimits] = [],
global_limits: Optional[Union[Dict, APIKeyLimits]] = None,
id: int = "",
access_key: Optional[Text] = None,
is_admin: bool = False)
Initialize an APIKey instance.
Arguments:
name
Text - A descriptive name for the API key.expires_at
Optional[Union[datetime, Text]], optional - When the key expires. Can be a datetime or ISO format string. Defaults to None.budget
Optional[float], optional - Maximum spending limit. Defaults to None.asset_limits
List[APIKeyLimits], optional - Rate limits for specific models. Defaults to empty list.global_limits
Optional[Union[Dict, APIKeyLimits]], optional - Global rate limits. Can be a dict with tpm/tpd/rpm/rpd keys or an APIKeyLimits instance. Defaults to None.id
int, optional - Unique identifier. Defaults to empty string.access_key
Optional[Text], optional - The actual API key value. Defaults to None.is_admin
bool, optional - Whether this is an admin key. Defaults to False.
Notes:
The global_limits dict format should have these keys:
- tpm: tokens per minute
- tpd: tokens per day
- rpm: requests per minute
- rpd: requests per day
validate
def validate() -> None
Validate the APIKey configuration.
This method checks that all rate limits are non-negative and that referenced models exist and are valid.
Raises:
AssertionError
- If any of these conditions are not met:- Budget is negative
- Global rate limits are negative
- Asset-specific rate limits are negative
Exception
- If a referenced model ID is not a valid aiXplain model.
Notes:
- For asset limits, both the model reference and limits are checked
- Models can be specified by ID or Model instance
- Model IDs are resolved to Model instances during validation
to_dict
def to_dict() -> Dict
Convert the APIKey instance to a dictionary representation.
This method serializes the APIKey and its associated limits into a format suitable for API requests or storage.
Returns:
Dict
- A dictionary containing:- id (int): The API key's ID
- name (Text): The API key's name
- budget (Optional[float]): The spending limit
- assetsLimits (List[Dict]): Model-specific limits with:
- tpm: tokens per minute
- tpd: tokens per day
- rpm: requests per minute
- rpd: requests per day
- assetId: model ID
- expiresAt (Optional[Text]): ISO format expiration date
- globalLimits (Optional[Dict]): Global limits with tpm/tpd/rpm/rpd
Notes:
- Datetime objects are converted to ISO format strings
- Model instances are referenced by their ID
delete
def delete() -> None
Delete this API key from the system.
This method permanently removes the API key from the aiXplain platform. The operation cannot be undone.
Raises:
Exception
- If deletion fails, which can happen if:- The API key doesn't exist
- The user doesn't have permission to delete it
- The API request fails
- The server returns a non-200 status code
Notes:
- This operation is permanent and cannot be undone
- Only the API key owner can delete it
- Uses the team API key for authentication
get_usage
def get_usage(asset_id: Optional[Text] = None) -> APIKeyUsageLimit
Get current usage statistics for this API key.
This method retrieves the current usage counts and limits for the API key, either globally or for a specific model.
Arguments:
asset_id
Optional[Text], optional - The model ID to get usage for. If None, returns usage for all models. Defaults to None.
Returns:
APIKeyUsageLimit
- A list of usage statistics objects containing:- daily_request_count: Number of requests made today
- daily_request_limit: Maximum requests allowed per day
- daily_token_count: Number of tokens used today
- daily_token_limit: Maximum tokens allowed per day
- model: The model ID these stats apply to (None for global)
Raises:
Exception
- If the request fails, which can happen if:- The API key doesn't exist
- The user doesn't have permission to view usage
- The API request fails
- The server returns an error response
Notes:
- Uses the team API key for authentication
- Counts reset at the start of each day
- Filtered by asset_id if provided
set_token_per_day
def set_token_per_day(token_per_day: int,
model: Optional[Union[Text, Model]] = None) -> None
Set the daily token limit for this API key.
Arguments:
token_per_day
int - Maximum number of tokens allowed per day.model
Optional[Union[Text, Model]], optional - The model to set limit for. If None, sets global limit. Defaults to None.
Raises:
Exception
- If the model isn't configured in this API key's asset_limits.
Notes:
- Model can be specified by ID or Model instance
- For global limits, model should be None
- The new limit takes effect immediately
set_token_per_minute
def set_token_per_minute(token_per_minute: int,
model: Optional[Union[Text, Model]] = None) -> None
Set the per-minute token limit for this API key.
Arguments:
token_per_minute
int - Maximum number of tokens allowed per minute.model
Optional[Union[Text, Model]], optional - The model to set limit for. If None, sets global limit. Defaults to None.
Raises:
Exception
- If the model isn't configured in this API key's asset_limits.
Notes:
- Model can be specified by ID or Model instance
- For global limits, model should be None
- The new limit takes effect immediately
set_request_per_day
def set_request_per_day(request_per_day: int,
model: Optional[Union[Text, Model]] = None) -> None
Set the daily request limit for this API key.
Arguments:
request_per_day
int - Maximum number of requests allowed per day.model
Optional[Union[Text, Model]], optional - The model to set limit for. If None, sets global limit. Defaults to None.
Raises:
Exception
- If the model isn't configured in this API key's asset_limits.
Notes:
- Model can be specified by ID or Model instance
- For global limits, model should be None
- The new limit takes effect immediately
set_request_per_minute
def set_request_per_minute(request_per_minute: int,
model: Optional[Union[Text, Model]] = None) -> None
Set the per-minute request limit for this API key.
Arguments:
request_per_minute
int - Maximum number of requests allowed per minute.model
Optional[Union[Text, Model]], optional - The model to set limit for. If None, sets global limit. Defaults to None.
Raises:
Exception
- If the model isn't configured in this API key's asset_limits.
Notes:
- Model can be specified by ID or Model instance
- For global limits, model should be None
- The new limit takes effect immediately