Charge Model
Account Charge Model and Constants
This module defines the Charge model for tracking periodic billing events associated with user profiles.
It also provides constants for charge types and providers, and emits a signal when a new charge is created.
Classes & Constants
Charge: Represents a single billing event for a user profile, including provider, charge type, token usage, and references.ChargeTypes: List of available charge types (completion, plugin, tool, compute).Actuals,get_actuals(): what a resource has actually spent in a period of time.
Key Features
Tracks provider, charge type, token usage, and references for each billing event.
Emits a signal (new_charge_created) when a new charge is created for downstream processing.
Integrates with Smarter logging and signal systems.
Example
from smarter.apps.account.models import Charge
charge = Charge.objects.create(
charge_type="completion",
prompt_tokens=100,
completion_tokens=200,
total_tokens=300,
)
- class smarter.apps.account.models.charge.Actuals(records=0, prompt_tokens=0, completion_tokens=0, total_tokens=0, total_cost=Decimal('0'))[source]
Bases:
objectWhat a resource has actually spent in a period of time.
- __init__(records=0, prompt_tokens=0, completion_tokens=0, total_tokens=0, total_cost=Decimal('0'))
- class smarter.apps.account.models.charge.AggregatedCharges(*args, **kwargs)[source]
Bases:
TimestampedModelAggregatedCharges model for tracking aggregated account billing events.
Represents aggregated billing data for a specific time period.
- Parameters:
year – Integer. The year of the aggregated data.
month – Integer. The month of the aggregated data.
day – Integer. The day of the aggregated data.
hour – Integer. The hour of the aggregated data.
id (Unknown) – Primary key: ID
created_at (Unknown) – Created at
updated_at (Unknown) – Updated at
resource_locator (Unknown) – Resource locator. The TimestampedModel.resource_locator of the resource that this charge is associated with.
charge_type (Unknown) – Charge type
records (Unknown) – Records
prompt_tokens (Unknown) – Prompt tokens
completion_tokens (Unknown) – Completion tokens
total_tokens (Unknown) – Total tokens
total_cost (Unknown) – Total cost
- exception DoesNotExist
Bases:
ObjectDoesNotExist
- exception MultipleObjectsReturned
Bases:
MultipleObjectsReturned
- exception NotUpdated
Bases:
ObjectNotUpdated,DatabaseError
- completion_tokens
-
Completion tokens
- Type:
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
- day
-
Day
- Type:
Type
- get_charge_type_display(*, field=<django.db.models.CharField: charge_type>)
Shows the label of the
charge_type. Seeget_FOO_display()for more information.
- hour
-
Hour
- Type:
Type
- 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
- month
-
Month
- Type:
Type
- objects = <django.db.models.Manager object>
- prompt_tokens
-
Prompt tokens
- Type:
Type
- records
-
Records
- Type:
Type
- resource_locator
-
Resource locator. The TimestampedModel.resource_locator of the resource that this charge is associated with.
- Type:
Type
- total_cost
-
Total cost
- Type:
Type
- total_tokens
-
Total tokens
- Type:
Type
- 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
- year
-
Year
- Type:
Type
- class smarter.apps.account.models.charge.Charge(*args, **kwargs)[source]
Bases:
TimestampedModelCharge model for tracking periodic account billing events.
A signal is emitted when a new charge is created, enabling downstream billing and analytics workflows.
Represents a single billing event for a resource.
- Parameters:
resource_locator – String. The TimestampedModel.resource_locator of the resource that this charge is associated with.
charge_type – String. The type of charge (e.g., completion, plugin, tool).
prompt_tokens – Integer. Number of prompt tokens used.
completion_tokens – Integer. Number of completion tokens used.
total_tokens – Integer. Total tokens used.
Example usage:
charge = Charge.objects.create( resource_locator=resource_locator, charge_type="completion", prompt_tokens=100, completion_tokens=200, total_tokens=300, )
- Parameters:
id (Unknown) – Primary key: ID
created_at (Unknown) – Created at
updated_at (Unknown) – Updated at
total_cost (Unknown) – Total cost
- exception DoesNotExist
Bases:
ObjectDoesNotExist
- exception MultipleObjectsReturned
Bases:
MultipleObjectsReturned
- exception NotUpdated
Bases:
ObjectNotUpdated,DatabaseError
- completion_tokens
-
Completion tokens
- Type:
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
- get_charge_type_display(*, field=<django.db.models.CharField: charge_type>)
Shows the label of the
charge_type. Seeget_FOO_display()for more information.
- 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
- objects = <django.db.models.Manager object>
- prompt_tokens
-
Prompt tokens
- Type:
Type
- resource_locator
-
Resource locator. The TimestampedModel.resource_locator of the resource that this charge is associated with.
- Type:
Type
- save(*args, **kwargs)[source]
Save the Charge, and send
new_charge_createdwhen it is new.- Parameters:
args – Passed to Django’s
save().kwargs – Passed to Django’s
save().
- total_cost
-
Total cost
- Type:
Type
- total_tokens
-
Total tokens
- Type:
Type
- 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
- class smarter.apps.account.models.charge.ChargeTypes(*values)[source]
Bases:
SmarterEnumAbstractCharge types enumeration.
This enumeration defines the different types of charges that can be associated with user profiles. Each charge type corresponds to a specific billing event, such as prompt completion, plugin usage, or tool usage.
The charge types are:
PROMPT_COMPLETION: a prompt completion charge.PLUGIN: a plugin charge.TOOL: a tool charge.COMPUTE: an hour of infrastructure, e.g. the nodes of an LLMHostCompute. It has no tokens.
- COMPUTE = 'compute'
- PLUGIN = 'plugin'
- PROMPT_COMPLETION = 'completion'
- TOOL = 'tool'
- smarter.apps.account.models.charge.get_actuals(resource_locator, start=None, end=None)[source]
What a resource has spent from start up to end, from its charges whether aggregated or not.
AggregatedCharges have the precision of an hour, so an hour is counted if it begins at or after start, and before end. Charges of a previous month are rolled up into days.