/** * micros.ts — integer micro-dollar prices, tables and exact cost arithmetic. * * Not a public entry point: `package.json` `exports` does not expose this * file. Its public names are re-exported from `pricing.ts`. * * Every price here is a safe integer number of micro-dollars (1 = $0.000001) * per million tokens, and every cost is computed with BigInt and rounded up * once, so no floating-point error can enter a budget. */ import type { UsageTokens } from './pricing.js'; /** * Per-million-token prices for one model, in whole micro-dollars. A bucket's * price is optional only so that a model without that bucket can omit it; * pricing usage that has tokens in a bucket with no price throws (there is no * fallback multiplier). */ export interface MicrosRates { /** Uncached input, per million tokens. */ inputPerMTokMicros: number; /** Output, per million tokens. */ outputPerMTokMicros: number; /** Cache writes with the 5-minute TTL (also used when the provider does not split writes by TTL), per million tokens. */ cacheWritePerMTokMicros?: number; /** Cache writes with the 1-hour TTL, per million tokens. */ cacheWrite1hPerMTokMicros?: number; /** Cache reads, per million tokens. */ cacheReadPerMTokMicros?: number; } /** A replacement rate set used for the WHOLE request once its prompt passes `aboveInputTokens`. */ export interface LongContextRates extends MicrosRates { /** * The long-context prices apply when the prompt (input + cache reads + * cache writes) is strictly greater than this many tokens. A safe integer >= 1. */ aboveInputTokens: number; } /** A model's price: base rates, plus optional long-context rates. */ export interface MicrosPrice extends MicrosRates { longContext?: LongContextRates; } /** Input to `definePricingTable`. */ export interface PricingTableDefinition { /** When these prices were last checked: a UTC date `YYYY-MM-DD` or a strict ISO-8601 date-time with a zone. */ checkedAt: string; /** Where the prices came from, for example the provider's pricing page URL. Non-blank. */ source: string; /** Prices keyed `provider:model`. At least one entry. */ rates: Record; } /** A validated, deeply frozen table returned by `definePricingTable`. */ export interface DatedPricingTable { readonly checkedAt: string; readonly source: string; readonly rates: Readonly>>; } /** What `priceFor` returns: a price, or the fact that the table has none for that key. */ export type PriceLookup = { status: 'found'; key: string; price: Readonly; } | { status: 'unknown-model'; key?: string; }; /** * Validate one integer-micro price and return a fresh, deeply frozen copy. * Each field is read once. Throws TypeError for a wrong type or shape and * RangeError for a value that is not a non-negative safe integer. */ export declare function readMicrosPrice(fn: string, where: string, value: unknown): Readonly; /** Build the frozen rates record for a table whose entries have all been classified as integer-micro. */ export declare function buildMicrosRates(fn: string, entries: ReadonlyArray): Readonly>>; /** * Look up `provider:model` in an integer-micro table. * * @throws TypeError when `table` or its `rates` is not a plain object, or the * entry is not an integer-micro price; RangeError for an invalid rate. */ export declare function priceFor(table: DatedPricingTable, key: string): PriceLookup; /** * The cost of one call in whole micro-dollars, exact and rounded UP. * * Arithmetic is BigInt: each bucket's tokens times its price per million * tokens, summed, divided by 1,000,000 and rounded up ONCE on the sum. A cost * is therefore never below the true cost and at most one micro-dollar above * it; zero usage is 0, and any positive cost is at least 1. There is no * floating point anywhere in the path. * * Buckets: `inputTokens` at `inputPerMTokMicros`, `outputTokens` at * `outputPerMTokMicros`, `cacheReadTokens` at `cacheReadPerMTokMicros`, * `cacheCreation5mTokens` at `cacheWritePerMTokMicros`, `cacheCreation1hTokens` * at `cacheWrite1hPerMTokMicros`. A bucket with tokens and no configured price * throws; it is never priced at 0 and never borrowed from another bucket. * * Long context: when `price.longContext` exists and the prompt (input + cache * reads + cache writes) is strictly greater than `aboveInputTokens`, the long * rates replace the base rates for every bucket. A bucket the long rates do * not price throws; it does not fall back to the base tier. * * Each field of `usage` and `price` is read once. Usage must be known token * counts: pass the `usage` of a `usageFromAnthropic` or `usageFromOpenAI` reading * only after checking its `status` is `'known'`. * * @throws TypeError for a usage or price of the wrong type or shape (including * the string `'unknown'` or a whole reading object, an unknown field, a BigInt or a string count); * RangeError for a count or rate that is not a non-negative safe integer, a * bucket with tokens and no price, or a cost beyond the safe-integer range. */ export declare function estimateCostMicros(usage: UsageTokens, price: MicrosPrice): number;