Skip to content

Pricing

Token prices, for providers that report tokens but not cost (Codex). No prices are built in: they change, and subscription plans don’t bill per token anyway, so the caller supplies the rates it wants to budget by.

Since v0.1.0

USD per million tokens.

Signature

export interface ModelPrice {
readonly input: number
/** Defaults to `input` when omitted. */
readonly cachedInput?: number
readonly output: number
/**
* Defaults to `output` when omitted; only used if the provider reports reasoning
* separately from output.
*/
readonly reasoning?: number
}

Source

Since v0.1.0

Keyed by model id; "*" is the fallback.

Signature

type PriceTable = Readonly<Record<string, ModelPrice>>

Source

Since v0.1.0

Cost in USD. inputTokens includes cached tokens (as both providers report it), so cached tokens are billed at the cached rate and the rest at input.

Example

import { Pricing } from "@jpowersdev/effect-agent"
import assert from "node:assert"
// USD per million tokens
const price: Pricing.ModelPrice = {
input: 3,
cachedInput: 0.25,
output: 15,
}
const cost = Pricing.costOf(price, {
inputTokens: 1_000_000, // half of them cached
cachedInputTokens: 500_000,
outputTokens: 100_000,
reasoningTokens: null,
})
assert.strictEqual(cost, 3.125)

Signature

declare const costOf: (price: ModelPrice, usage: TokenUsage) => number

Source

Since v0.1.0

The price for model, falling back to the "*" entry.

Signature

declare const priceFor: (
table: PriceTable,
model: string | null,
) => ModelPrice | undefined

Source

Since v0.1.0