LLMClientAPIKey Model

class smarter.apps.llmclient.models.LLMClientAPIKey(*args, **kwargs)[source]

Bases: TimestampedModel

Represents 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 LLMClient instance.

  • Each LLMClientAPIKey references one SmarterAuthToken representing 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

ForeignKey to SmarterAuthToken

Api key (related name: llmclientapikey)

The API key (SmarterAuthToken) associated with the LLMClient.

Type:

Type

api_key_id

Internal field, use api_key instead.

classmethod attach(llmclient, api_key)[source]

Attach an api key to an llmclient, in place of any other, as applying spec.apiKey does.

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_at is touched, so that its cached manifest, whose spec.apiKey names the key, is rebuilt.

Parameters:
Returns:

The attachment.

Return type:

LLMClientAPIKey

created_at

DateTimeField

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.

Parameters:

llmclient (LLMClient) – The llmclient.

Returns:

The number of api keys detached.

Return type:

int

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:
  • invalidate (bool | None) – Whether to invalidate the cache for this retrieval.

  • llmclient (LLMClient | None) – The LLMClient instance for which to retrieve API keys.

  • kwargs – Unused. Accepted for the signature of the base class.

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.

Parameters:
  • llmclient (LLMClient) – The LLMClient to check. If falsy, returns False.

  • invalidate (bool | None) – If True, discard the cached result and query the database again.

Return type:

bool

Returns:

True if an active SmarterAuthToken is linked to the LLMClient, otherwise False.

id: int

BigAutoField

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

ForeignKey to LLMClient

Llmclient (related name: llmclientapikey)

The LLMClient instance associated with this API key.

Type:

Type

llmclient_id

Internal field, use llmclient instead.

objects = <django.db.models.Manager object>
updated_at

DateTimeField

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