LLMClientAPIKey Model

class smarter.apps.llm_client.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 llm_client instances.

The LLMClientAPIKey model is essential for managing access to llm_client APIs, supporting use cases such as per-bot API key rotation, revocation, and auditing. By associating API keys with individual llm_clients, the platform can enforce security policies and monitor usage at the llm_client level.

Typical usage involves creating a LLMClientAPIKey instance whenever a new API key is provisioned for an llm_client, 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 llm_client
api_key = SmarterAuthToken.objects.create(...)
llm_client_api_key = LLMClientAPIKey.objects.create(llm_client=my_llm_client, api_key=api_key)

# Query API keys for an llm_client
keys = LLMClientAPIKey.objects.filter(llm_client=my_llm_client)

Notes

  • API key activation and deactivation are managed via the SmarterAuthToken model.

  • This model supports auditing and access control for llm_client API endpoints.

  • Intended for internal use within the Smarter platform to secure llm_client APIs.

Parameters:
  • id (Unknown) – Primary key: ID

  • created_at (Unknown) – Created at

  • updated_at (Unknown) – Updated at

Relationship fields:

Parameters:
  • llm_client (Unknown) –

    Llm client (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.

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 get_cached_objects(invalidate=False, llm_client=None)[source]

Retrieve a list of LLMClientAPIKey instances associated with a LLMClient using caching.

Example usage:

# Retrieve API keys for an llm_client with caching
api_keys = LLMClientAPIKey.get_cached_objects(my_llm_client, invalidate=True)
Parameters:
  • invalidate (Optional[bool]) – Whether to invalidate the cache for this retrieval.

  • llm_client (Optional[LLMClient]) – The LLMClient instance for which to retrieve API keys.

Returns:

A queryset of LLMClientAPIKey instances associated with the LLMClient.

Return type:

QuerySet

classmethod has_active_api_key(llm_client, invalidate=False)[source]

Returns True if the llm_client has at least one active API key.

Return type:

bool

id

BigAutoField

Primary key: ID

Type:

Type

llm_client

ForeignKey to LLMClient

Llm client (related name: llmclientapikey)

The LLMClient instance associated with this API key.

Type:

Type

llm_client_id

Internal field, use llm_client 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