Skip to Content

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, audioDurationMilliseconds, or costMicros.
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. An empty record list returns 0. This is an arithmetic average of returned records, not a weighted total and not a provider subscription quota. The percentage operation does not itself filter records to the current billing period; use the detailed allowance records for an interface that must explain periods or different units.

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. Exceeding the Modem allowance does not itself create a separate customer overage charge. Accepted upstream work can still incur provider charges or consume subscription quota. 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. Usable provider measurements replace estimates for the corresponding fields; missing fields can remain estimated. Estimates count against Modem allowances. They are not provider-reported usage or proof of an actual provider charge.

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

Estimated cost

costMicros measures millionths of a US dollar. Regular-product Chat Completions and Completions rules can use it when applicable model pricing exists. It is an estimate used for allowance enforcement, not an invoice or subscription credit balance. See Limits and estimates.

API reference