npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@pydantic/genai-prices

v0.0.47

Published

Calculate prices for calling LLM inference APIs

Readme

@pydantic/genai-prices

JavaScript package and command-line tool for calculating LLM API prices.

Basic usage

calcPrice

The package exports a function for price calculation that, by default, uses the bundled price data.

import { calcPrice } from '@pydantic/genai-prices'

const usage = { input_tokens: 1000, output_tokens: 100 }

const result = calcPrice(usage, 'gpt-3.5-turbo', { providerId: 'openai' })

if (result) {
  console.log(
    `$${result.total_price} (input: $${result.input_price}, output: $${result.output_price})`,
    result.provider.name,
    result.model.name
  )
} else {
  console.log('No price found for this model/provider combination')
}

updatePrices

You can optionally use updatePrices to implement logic that can periodically update the data used by calcPrice. See the src/examples/browser directory for an example that implements a local storage-backed auto-update and src/examples/node-script.ts for an example of a file-based asynchronous auto-update implementation.

calcPrice is a synchronous function that uses the currently available data - either the bundled one, or the last data fetched from the updatePrices setup. To force calcPrice to await potential in-progress data updates that can happen in enableAutoUpdate, await the waitForUpdate() return value before calling calcPrice

import { calcPrice, updatePrices } from '@pydantic/genai-prices'

enableAutoUpdate(/** auto-update logic */)

// ...

// this guarantees that the latest data is used
await waitForUpdate()
const result = calcPrice(usage, 'gpt-5', { providerId: 'openai' })

console.log(
  `$${result.total_price} (input: $${result.input_price}, output: $${result.output_price})`,
  result.provider.name,
  result.model.name
)

Provider Matching

The library uses intelligent provider matching:

  1. Explicit provider: Use providerId parameter or provider:model format
  2. Model-based matching: Uses the provider's model_match logic (e.g., OpenAI matches models starting with "gpt-")
  3. Fallback: Tries to match based on model name patterns

Best practices:

  • Always specify providerId if you know it (e.g., openai, google, etc.) for best results
  • Use provider:model format in CLI for explicit provider selection
  • The async API with --auto-update provides the most up-to-date pricing

Error Handling

When a model or provider is not found, the library returns null. This makes it easier to handle cases where pricing information might not be available.

import { calcPrice } from '@pydantic/genai-prices'

const usage = { input_tokens: 1000, output_tokens: 100 }

// Returns null if model/provider not found
const result = calcPrice(usage, 'non-existent-model')
if (result === null) {
  console.log('No pricing information available for this model')
} else {
  console.log(`Total Price: $${result.total_price} (input: $${result.input_price}, output: $${result.output_price})`)
}

// Async version also returns null
const asyncResult = await calcPrice(usage, 'non-existent-model', { awaitAutoUpdate: true, providerId: 'unknown-provider' })
if (asyncResult === null) {
  console.log('No pricing information available for this model/provider combination')
} else {
  console.log(`Total Price: $${asyncResult.total_price} (input: $${asyncResult.input_price}, output: $${asyncResult.output_price})`)
}

Troubleshooting

Common Issues

  • No price found (returns null):
    • Make sure you specify the correct providerId (e.g., openai, google, anthropic)
    • Try using provider:model format in CLI
    • Use --auto-update flag to fetch latest data
    • Check that the model name is correct and supported by the provider

CLI

The easiest way to run the latest version of the package as a CLI tool is through npx:

npx @pydantic/genai-prices@latest

For example:

npx @pydantic/genai-prices@latest calc gpt-4 --input-tokens 1000 --output-tokens 500
npx @pydantic/genai-prices@latest list

You can also install it globally and then use the genai-prices command:

npm i -g @pydantic/genai-prices
# Basic usage
genai-prices gpt-5 --input-tokens 1000 --output-tokens 100

# Specify provider explicitly
genai-prices openai:gpt-5 --input-tokens 1000 --output-tokens 100

# List available providers and models
genai-prices list
genai-prices list openai