@rivus/agent-kit/cost
createPricing, summarize, priceRecord, Pricing — anywhere, no imports
Functions
createPricing()
function createPricing(table: PricingTable, options?: PricingOptions): Pricing;The price lookup over table. Keys match regardless of case.
Table keys name one model each. A key prices a model whose id is the key, the key followed by a date (-20250929,
-2025-08-07 or @20250929), or either after one <provider>/ prefix: claude-sonnet-4-5 prices
claude-sonnet-4-5-20250929, claude-sonnet-4-5@20250929 and anthropic/claude-sonnet-4-5. Any other ending
names another model, so gpt-5 prices none of gpt-5.7, gpt-5-codex or gpt-5-mini, and o1 does not price
o1-mini. Among the keys an id matches, the id itself wins, then the id without its date, then both without
the prefix.
fallback keys are family aliases, used only when no table key matches: a model takes the longest fallback key its
id contains, the first given among equally long keys, so opus prices claude-opus-4-9 and gpt-5 prices
gpt-5.7.
overrides are the caller's own rules and, unlike table keys, match by substring: a model takes the longest
override key its id contains, the first given among equally long keys, and that override wins over the table and
the fallback, so { opus: … } reprices both claude-opus-4-8 and claude-3-opus. A partial override takes the
prices it lacks from the price the id has without overrides: over a table with gpt-4o and gpt-4o-2024-05-13,
{ "gpt-4o": { input: 1 } } keeps the dated model's own output price. A partial override of an id without a price
of its own gives no price. An override entry that says nothing about longContext keeps the model's own tier, and
one with a longContext replaces it. A model that nothing prices has no price. Lookups are cached by model id.
Parameters
table
PricingTable
options?
PricingOptions
Returns
priceRecord()
function priceRecord(record: UsageRecord, pricing: Pricing): PricedRecord;The record by model, priced: one part per model of its split, or the whole record as one part. summarize and
costOf charge records through it. A part's cost follows three rules in order: (1) an amount the part carries
(costUsd), such as one the agent logged, wins; (2) otherwise each token count is charged at the model's price;
(3) that amount is multiplied by the record's pricingMultiplier. An agent's own amount is what it billed, so the
multiplier does not apply to it. A count the part lacks costs nothing; a part without a model, or with a model that
the pricing does not know, has no cost: absent, never 0.
A split replaces the aggregate, never adds to it, and wins over it: when the agent logged an amount for the record
and for every model, the models' amounts are used. The record stays whole only when the agent logged an amount for
it that the split does not divide among every model, because charging the other models at table prices would
replace the agent's amount. An amount from a price table on a split record, as { ...record, ...costOf(record, pricing) } leaves it, does not keep the record whole: its models are charged again. A part of a split counts the
model calls its share records, not a portion of the record's.
The long-context tier prices one request. A request record is one; a turn or session record, and each part
of one, is an aggregate of several calls, whose totals cannot tell how large any single request's context was,
so it bills at the base rates and never at an average — the same limit ccusage's aggregation states for its
summed totals. An aggregate counts as one request only when its own, or a part's, modelCalls is 1.
Parameters
record
UsageRecord
pricing
Returns
summarize()
function summarize(
records: Iterable<UsageRecord>,
pricing: Pricing,
options?: SummarizeOptions
): UsageSummary;Totals of records in the window, as presence summarizes its sources: tokens, how many records contributed, and
the sum of the costs that priceRecord gives their parts, per group. A group's cost is absent when none of its
parts had a cost; parts without one add their tokens but no dollars. The total adds up the groups, as presence adds
up its sources, and counts each record once. A record split by model (usageByModel) adds its split, model by
model, and not its aggregate as well, so a turn of several models counts its tokens once and each model's tokens
land in that model's group.
Parameters
records
Iterable<UsageRecord>
pricing
options?
SummarizeOptions
Returns
UsageSummary
Interfaces
Cost
An amount in USD and where it comes from, under the names a UsageRecord uses, so { ...record, ...cost } is the
priced record; priceRecord gives a record split by model one per model.
Properties
costSource
readonly costSource: CostSource;costUsd
readonly costUsd: number;PricedPart
One model's share of a record split by model, or the whole record, with its cost when something prices it.
Properties
cost?
readonly cost?: Cost;Absent, never 0, when the agent logged no amount and the pricing does not know the model.
model?
readonly model?: string;The model of the share, or the record's model; absent when the record names none.
modelCalls?
readonly modelCalls?: number;The model calls the share, or the whole record, counts; absent when it does not say.
usage
readonly usage: Usage;PricedRecord
A usage record priced by model.
Properties
parts
readonly parts: readonly PricedPart[];One part per model when the record is split by model, else one part for the whole record.
Pricing
The price lookup over an injected table.
Methods
priceOf()
priceOf(model: string): Price | undefined;The price of model, or undefined when no key matches: an unknown model has no price, not a price of 0.
Parameters
model
string
Returns
Price | undefined