Charges

Smarter.apps.dashboard.views.api.charges

API view for the dashboard’s Token Usage chart: the tokens that a user’s LLM requests used, over a recent window of time, one point per hour, day or month. The series is built by smarter.apps.account.token_usage, which the REST api’s GET /api/v1/accounts/usage/ shares.

  • ChargesView: the authenticated POST endpoint, {"periodicity": "24_hours"}.

class smarter.apps.dashboard.views.views.api.charges.ChargesView(**kwargs)[source]

Bases: SmarterAuthenticatedWebView

API view for the dashboard’s Token Usage chart: the user’s token usage over a window of time.

A POST with {"periodicity": "<window>"} returns a TokenUsageSeries of the charges to the user’s UserProfile. An unknown periodicity is a 400.

property formatted_class_name: str

Returns the class name in a formatted string along with the name of this view.

post(request, *args, **kwargs)[source]

Handle POST requests to return the user’s token usage over a window of time.

Parameters:

request (HttpRequest) – The incoming HTTP POST request, whose JSON body may name a periodicity.

Returns:

The window’s points, or a 400 with the error if the periodicity is unknown.

Return type:

JsonResponse

smarter.apps.dashboard.views.views.api.charges.requested_periodicity(request)[source]

The periodicity in the request’s JSON body, {"periodicity": "24_hours"}, or the default.

Parameters:

request (HttpRequest) – The incoming HTTP POST request.

Returns:

The requested periodicity, or TokenUsageWindow.DEFAULT if the body has none.

Return type:

str

Token usage: the tokens that a user’s LLM requests used, over a recent window of time, one point per hour, day or month.

Each LLM request creates a Charge for each resource that it is charged to, the user’s UserProfile among them. Celery Beat rolls charges up into AggregatedCharges every hour, and deletes them, so each point is summed from both, by get_actuals(), as the budgets are. The charges of the current hour are therefore counted before they’re aggregated.

The web console’s dashboard charts them (smarter.apps.dashboard.views.views.api.charges.ChargesView), and the REST api returns them at GET /api/v1/accounts/usage/ (smarter.apps.account.api.v1.views.usage.TokenUsageView).

smarter.apps.account.token_usage.TOKEN_USAGE_API_MODELS = (<class 'smarter.apps.account.token_usage.TokenUsageSeries'>,)

The models whose JSON Schemas the Smarter api’s OpenAPI document includes.

(<class 'smarter.apps.account.token_usage.TokenUsageSeries'>, )
class smarter.apps.account.token_usage.TokenUsagePoint(*, period_start, records, prompt_tokens, completion_tokens, total_tokens, total_cost)[source]

Bases: BaseModel

The tokens and cost of one period of the Token Usage chart.

model_config = {'extra': 'forbid'}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class smarter.apps.account.token_usage.TokenUsageSeries(*, periodicity, period, points)[source]

Bases: BaseModel

The Token Usage chart’s points for one window, oldest first.

model_config = {'extra': 'forbid'}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class smarter.apps.account.token_usage.TokenUsageWindow[source]

Bases: object

The windows of time that the Token Usage chart offers, by their periodicity key.

Each is a number of points of one period, ending with the current one: e.g. 24_hours is the current hour and the 23 before it. Charges are aggregated by the hour, so an hour is the smallest period.

classmethod get(periodicity)[source]

Return the period of a window’s points, and how many there are.

Parameters:

periodicity (str) – The window’s key, e.g. "24_hours".

Returns:

The period, "hour", "day" or "month", and the number of points.

Return type:

tuple[Literal['hour', 'day', 'month'], int]

Raises:

ValueError – If the periodicity is not one of the windows.

smarter.apps.account.token_usage.build_token_usage_series(resource_locator, periodicity, now=None)[source]

Return the tokens that a resource used in each period of a window, oldest first.

A period without charges is a point of zeros, so that the chart’s time axis has no gaps.

Parameters:
  • resource_locator (str) – The record locator of the resource whose charges are counted, e.g. a UserProfile’s.

  • periodicity (str) – The window’s key, e.g. "24_hours". See TokenUsageWindow.

  • now (datetime | None) – The end of the window. Defaults to now.

Returns:

The window’s points.

Return type:

TokenUsageSeries

Raises:

ValueError – If the periodicity is not one of the windows.

smarter.apps.account.token_usage.token_usage_series(resource_locator, periodicity)[source]

The JSON of build_token_usage_series() for a window that ends now, cached for five minutes.

Parameters:
  • resource_locator (str) – The record locator of the resource whose charges are counted.

  • periodicity (str) – The window’s key. See TokenUsageWindow.

Returns:

The JSON of a TokenUsageSeries.

Return type:

dict

Raises:

ValueError – If the periodicity is not one of the windows. Errors aren’t cached.

The REST api’s token usage, GET /api/v1/accounts/usage/: the tokens that the user’s LLM requests used.

The series is the one that the web console’s dashboard charts, built by smarter.apps.account.token_usage.

class smarter.apps.account.api.v1.views.usage.TokenUsageView(*args, **kwargs)[source]

Bases: SmarterAuthenticatedAPIView

The tokens that the user’s LLM requests used, over a window of time.

?periodicity=24_hours names the window, one of TokenUsageWindow.WINDOWS. The response is a TokenUsageSeries of the charges to the user’s UserProfile, cached for five minutes. An unknown periodicity is a 400.

get(request, *args, **kwargs)[source]

Return the user’s token usage over the window of the query’s periodicity.

Parameters:

request (Request) – The request.

Return type:

Response

Returns:

The window’s points, or 400 if the periodicity is unknown.