API pricing math for 1k+ AI models from the canonical tokenpricing database with multi-currency and cache-token pricing support.
Token pricing for AI models changes frequently across different providers and model types. This library now consumes the canonical database published from this repository, synchronized directly from upstream pricing sources every six hours.
Important: This library does not estimate token counts from strings or messages. tokenpricing focuses solely on providing accurate, current pricing data.
- Up-to-date AI model pricing from the tokenpricing canonical database
- Caching with 6-hour TTL for pricing data
- Multi-currency conversion via JSDelivr currency API with a 24-hour cached USD rates map
- TypeScript-first with full type definitions
- ESM and CJS dual-format package
- Zero dependencies beyond
fuse.js(for fuzzy matching)
pnpm add @atenareply/tokenpricingOr with npm:
npm install @atenareply/tokenpricingimport { getPricing, computeCost } from "@atenareply/tokenpricing";const pricing = await getPricing("openai/gpt-5.2");
console.log(`Input: $${pricing.inputPerMillion.toFixed(2)}/1M tokens`);
console.log(`Output: $${pricing.outputPerMillion.toFixed(2)}/1M tokens`);const pricing = await getPricing("openai/gpt-5.2", "EUR");
console.log(`Input: €${pricing.inputPerMillion.toFixed(2)}/1M tokens`);const cost = await computeCost("openai/gpt-5.2", 1000, 500, "EUR", { cacheReadTokens: 250, cacheCreationTokens: 100 });
console.log(`Total cost: €${cost.toFixed(6)}`);When you make a typo in a model ID or currency code, tokenpricing provides helpful "Did you mean?" suggestions:
await getPricing("openai/gpt4");
// Error: Model not found: openai/gpt4. Did you mean 'openai/gpt-4'?
await getPricing("openai/gpt-4", "ERU");
// Error: Unsupported currency: ERU. Did you mean 'EUR'?Get pricing info for a specific model.
modelId— Model identifier (e.g.,"openai/gpt-4")currency— Target currency code (default:"USD")- Returns
Promise<PricingInfo>
Compute total cost for a specific model given token counts.
modelId— Model identifierinputTokens— Number of input tokensoutputTokens— Number of output tokenscurrency— Target currency code (default:"USD")options.cacheReadTokens/options.cacheCreationTokens— optional cached-token usage- Returns
Promise<number>
interface PricingInfo {
inputPerMillion: number;
outputPerMillion: number;
cacheReadPerMillion?: number;
cacheCreationPerMillion?: number;
currency: string;
}Pricing data is sourced from the canonical tokenpricing dataset generated in this repository from OpenRouter and LiteLLM.
Caching uses a 6-hour TTL aligned to the canonical sync cadence. Currency conversion uses daily USD base rates from the JSDelivr currency API with a 24-hour cache.
pnpm installpnpm build # Build ESM + CJS bundles
pnpm test # Run tests
pnpm test:coverage # Run tests with coverage
pnpm lint # Lint and format check
pnpm lint:fix # Auto-fix lint/format issues
pnpm typecheck # TypeScript type check- Canonical database sync: Atena-IT/tokenpricing
See LICENSE file for details.