LLMClientAPIKey Model
- class smarter.apps.llmclient.models.LLMClientAPIKey(*args, **kwargs)[source]
Bases:
TimestampedModelRepresents the mapping of API keys to LLMClient instances within the Smarter platform.
Important
If present, the LLMClient associated with this record will require Api Key authentication for all API requests. Otherwise, the LLMClient will allow anonymous unauthenticated access.
See
smarter.lib.drf.token_authentication.SmarterTokenAuthentication.This model establishes a relationship between a LLMClient and its associated API keys, enabling secure authentication and authorization for API access. Each entry in this model links a specific LLMClient to a unique API key, allowing fine-grained control over which keys can interact with which llmclient instances.
The LLMClientAPIKey model is essential for managing access to llmclient APIs, supporting use cases such as per-bot API key rotation, revocation, and auditing. By associating API keys with individual llmclients, the platform can enforce security policies and monitor usage at the llmclient level.
Typical usage involves creating a LLMClientAPIKey instance whenever a new API key is provisioned for an llmclient, and querying this model to validate incoming requests against active keys.
Model Relationships
Each LLMClientAPIKey is linked to one
LLMClientinstance.Each LLMClientAPIKey references one
SmarterAuthTokenrepresenting the API key.
Example
# Assign an API key to an llmclient api_key = SmarterAuthToken.objects.create(...) llmclient_api_key = LLMClientAPIKey.objects.create(llmclient=my_llmclient, api_key=api_key) # Query API keys for an llmclient keys = LLMClientAPIKey.objects.filter(llmclient=my_llmclient)
Notes
API key activation and deactivation are managed via the SmarterAuthToken model.
This model supports auditing and access control for llmclient API endpoints.
Intended for internal use within the Smarter platform to secure llmclient APIs.
- Parameters:
id (Unknown) – Primary key: ID
created_at (Unknown) – Created at
updated_at (Unknown) – Updated at
Relationship fields:
- Parameters:
llmclient (Unknown) –
Llmclient (related name:
llmclientapikey)The LLMClient instance associated with this API key.
api_key (Unknown) –
Api key (related name:
llmclientapikey)The API key (SmarterAuthToken) associated with the LLMClient.
- exception DoesNotExist
Bases:
ObjectDoesNotExist
- exception MultipleObjectsReturned
Bases:
MultipleObjectsReturned
- exception NotUpdated
Bases:
ObjectNotUpdated,DatabaseError
- api_key
ForeignKeytoSmarterAuthTokenApi key (related name:
llmclientapikey)The API key (SmarterAuthToken) associated with the LLMClient.
- Type:
Type
- classmethod attach(llmclient, api_key)[source]
Attach an api key to an llmclient, in place of any other, as applying
spec.apiKeydoes.An llmclient has one api key: the others are detached. The llmclient’s cached api keys are invalidated, so that it requires the key at once, and its
updated_atis touched, so that its cached manifest, whosespec.apiKeynames the key, is rebuilt.- Parameters:
llmclient (
LLMClient) – The llmclient.api_key (
SmarterAuthToken) – The api key, of the llmclient’s account.
- Returns:
The attachment.
- Return type:
- created_at
-
Created at
Timestamp indicating when the model instance was created.
This field is automatically set to the current date and time when the instance is first created. It is indexed in the database for efficient querying.
- Type:
Type
- classmethod detach(llmclient)[source]
Detach every api key from an llmclient, which is then public once it is deployed.
- classmethod get_cached_objects(invalidate=False, llmclient=None, **kwargs)[source]
Retrieve a list of LLMClientAPIKey instances associated with a LLMClient using caching.
Example usage:
# Retrieve API keys for an llmclient with caching api_keys = LLMClientAPIKey.get_cached_objects(my_llmclient, invalidate=True)
- Parameters:
- Returns:
A queryset of LLMClientAPIKey instances associated with the LLMClient.
- Return type:
QuerySet[LLMClientAPIKey]
- classmethod has_active_api_key(llmclient, invalidate=False)[source]
Return True if the llmclient has at least one active API key.
The result is cached per LLMClient, for
cache_expiration.
- id: int
-
Primary key: ID
The row’s primary key, which Django adds to every concrete model. Declared, without a value, only so that type checkers know it: Django ignores a bare annotation.
- Type:
Type
- llmclient
-
Llmclient (related name:
llmclientapikey)The LLMClient instance associated with this API key.
- Type:
Type
- objects = <django.db.models.Manager object>
- updated_at
-
Updated at
Timestamp indicating when the model instance was last updated.
This field is automatically updated to the current date and time whenever the instance is saved. It is indexed in the database for efficient querying.
- Type:
Type