Skip to Content
Client SDKGet customer usage

Get customer usage

client.usage.retrieve() returns the usage limits and rate limits that apply to the signed-in customer.

Read usage allowances

const usage = await client.usage.retrieve() for (const limit of usage.usageLimits) { console.log({ metric: limit.allowanceMetric, used: limit.rawAmount, allowance: limit.allowanceValue, remaining: limit.remaining, usedPercentage: limit.usedPercentage, scopeKind: limit.scopeKind, scopeValue: limit.scopeValue }) }

Each usage-limit item contains:

FieldTypeDescription
idstringThe usage record ID.
rawAmountnumberThe amount used so far in the record’s billing period.
allowanceValuenumberThe allowance set on the product.
allowanceMetricstringThe unit being measured. It is one of numberOfRequest, numberOfTokens, numberOfCharacters, numberOfImages, audioFileSizeBytes, or audioDurationMilliseconds.
remainingnumberThe allowance minus the amount used. It can be below zero.
usedPercentagenumberThe amount used as a percentage of the allowance, rounded to one decimal place. It is 0 when the allowance is 0.
scopeKindstringHow the rule matches models. It is modelIdentifier or requestRegexPatterns.
scopeValuestringThe model identifier, or the regular expression that is matched against the model identifier.

Read rate-limit rules

The same response contains the customer’s rate-limit rules:

for (const limit of usage.rateLimits) { console.log({ allowance: limit.allowanceValue, metric: limit.allowanceMetric, frequency: limit.rateLimitFrequency, scopeKind: limit.scopeKind, scopeValue: limit.scopeValue }) }

rateLimitFrequency is minute, hour, or day. A rate-limit entry describes the rule only, not the usage in the current window.

Get the aggregate percentage

Use client.usage.retrievePercentage() when the interface needs one percentage:

const { percentage } = await client.usage.retrievePercentage() renderUsageBar(percentage)

The response is:

type CustomerUsagePercentage = { percentage: number }

The percentage is the average of the usedPercentage values of the customer’s usage limits. The request fails when the customer has no usage limit records.

How limits are enforced

Limits on provider-reported usage, such as tokens, are soft caps. When recorded usage reaches the allowance, new requests are refused. Requests already running finish and can go over. No overage is billed. See Gateway errors for the refusal.

Tokens come from the provider’s usage report. When a response has no token count, the gateway estimates it from the text and labels it as an estimate. A provider count always replaces an estimate. Estimates count against allowances, not against provider usage or pricing.

The dashboard’s Customer requests view labels each request’s tokens as Reported provider usage, Estimated usage, or Mixed provider and estimated usage.