llm-costprice
v1.0.0
Published
Calculate the cost of any LLM API call. Supports OpenAI, Anthropic, Google, Mistral, and more.
Maintainers
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-costpriceQuick 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.currencyestimateCost(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-15 → gpt-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 calladdModel()at runtime. See PRICING_UPDATE.md for contributor guidance. - Is this accurate? Prices are intended to match provider pricing pages. Each model has a
lastVerifieddate; 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
