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 🙏

© 2026 – Pkg Stats / Ryan Hefner

llm-costprice

v1.0.0

Published

Calculate the cost of any LLM API call. Supports OpenAI, Anthropic, Google, Mistral, and more.

Readme

llm-costprice

Calculate the cost of any LLM API call in one line.

Zero dependencies. TypeScript. Supports OpenAI, Anthropic, Google, Mistral, Cohere, Meta (via API providers), and more.

Install

npm install llm-costprice

Quick Start

import { calculateCost } from "llm-costprice";

const result = calculateCost({
  model: "gpt-4o",
  inputTokens: 1500,
  outputTokens: 500,
});

console.log(result.totalCost);   // 0.01125
console.log(result.breakdown);    // { inputCost: 0.00375, outputCost: 0.0075, ... }

API

calculateCost(usage: LLMUsage): CostResult

Primary function. Takes a usage object (model + token/character/audio counts) and returns the cost in USD with a breakdown.

import { calculateCost } from "llm-costprice";

const result = calculateCost({
  model: "claude-sonnet-4-20250514",
  inputTokens: 1000,
  outputTokens: 500,
  cachedInputTokens: 200,  // optional, for prompt caching
});
// result.totalCost, result.breakdown, result.model, result.currency

estimateCost(usage: LLMUsage): CostResult

Alias for calculateCost. Use whichever name fits your codebase.

getCost(model: string, inputTokens: number, outputTokens: number): number

Returns only the total cost as a number.

import { getCost } from "llm-costprice";

const cost = getCost("gpt-4o", 1000, 500);

getModel(modelId: string): ModelPricing | undefined

Look up a model's pricing info. Returns undefined if not found (does not throw).

import { getModel } from "llm-costprice";

const model = getModel("gpt-4o");
console.log(model?.inputCostPer1MTokens);

listModels(provider?: string): ModelPricing[]

List all models, optionally filtered by provider.

import { listModels } from "llm-costprice";

const all = listModels();
const anthropic = listModels("anthropic");

listProviders(): string[]

Returns a sorted array of all provider names.

addModel(pricing: ModelPricing): void

Register a custom model at runtime. Overwrites if modelId already exists.

removeModel(modelId: string): boolean

Remove a model from the registry. Returns true if it existed.

formatCost(usd: number, options?: FormatOptions): string

Format a cost number into a human-readable string.

import { formatCost } from "llm-costprice";

formatCost(0.00375);                      // "$0.003750"
formatCost(0.00375, { decimals: 4 });     // "$0.0037"
formatCost(1.5, { symbol: false });       // "1.500000"

Supported Models

Pricing is maintained for all major providers. Use listModels() or listModels("openai") for the full list.

| Provider | Examples | |-----------|----------| | OpenAI | gpt-4o, gpt-4o-mini, gpt-4-turbo, gpt-3.5-turbo, o1, o3, o4-mini | | Anthropic | claude-opus-4, claude-sonnet-4, claude-haiku-4-5, claude-3-5-sonnet | | Google | gemini-2.5-pro, gemini-2.5-flash, gemini-2.0-flash, gemini-1.5-pro | | Mistral | mistral-large-latest, mistral-small-latest, codestral-latest | | Cohere | command-r-plus, command-r, command-light | | Together | llama-3.1-405b, llama-3.1-70b, llama-3.1-8b |

Model IDs are resolved case-insensitively. Many models support aliases (e.g. gpt-4o-2024-08-06) and date-suffix fallback (e.g. gpt-4o-2025-01-15gpt-4o).

Custom Models

Register your own pricing (e.g. fine-tuned or private models) at runtime:

import { addModel, calculateCost } from "llm-costprice";

addModel({
  provider: "custom",
  modelId: "my-fine-tune",
  inputCostPer1MTokens: 10,
  outputCostPer1MTokens: 30,
  lastVerified: "2026-01-15",
});

calculateCost({ model: "my-fine-tune", inputTokens: 1000, outputTokens: 500 });

Formatting

Use formatCost for display:

import { formatCost, calculateCost } from "llm-costprice";

const result = calculateCost({ model: "gpt-4o", inputTokens: 1000, outputTokens: 500 });
console.log(formatCost(result.totalCost));

Error Handling

  • UnknownModelError — The model ID is not in the registry. The message may include suggestions for similar model names.
  • InvalidUsageError — Invalid usage (e.g. negative tokens, cachedInputTokens > inputTokens, or no usage dimensions).
import { calculateCost, UnknownModelError, InvalidUsageError } from "llm-costprice";

try {
  calculateCost({ model: "gpt-4o", inputTokens: -1, outputTokens: 0 });
} catch (e) {
  if (e instanceof InvalidUsageError) {
    console.error("Invalid usage:", e.message);
  }
}

FAQ

  • How do I update pricing? Open a PR that updates src/data/models.ts, or call addModel() at runtime. See PRICING_UPDATE.md for contributor guidance.
  • Is this accurate? Prices are intended to match provider pricing pages. Each model has a lastVerified date; verify critical numbers yourself.
  • Browser support? Yes. No Node-specific APIs; works in browsers and Node 16+.

Contributing

Contributions are welcome. For price updates, see .github/PRICING_UPDATE.md.

License

MIT