# llm-pricing-calc

[![npm version](https://img.shields.io/npm/v/llm-pricing-calc.svg?style=flat-square)](https://www.npmjs.com/package/llm-pricing-calc)
[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg?style=flat-square)](LICENSE)
[![Official Website](https://img.shields.io/badge/Official_Website-llmabacus.com-10b981?style=flat-square)](https://llmabacus.com)
[![Live Matrix](https://img.shields.io/badge/Live_Pricing_Matrix-Interactive_App-6366f1?style=flat-square)](https://llmabacus.com)

A zero-dependency, ultra-lightweight Node.js / TypeScript utility to calculate token costs, look up official model prices, and compare API expenses across 50+ mainstream Large Language Models (including **DeepSeek V3/V4, OpenAI GPT-4o, Anthropic Claude 3.5 Sonnet, Google Gemini 1.5/2.0, Alibaba Qwen**, and more).

> **Live Interactive Version & Regional Analysis**:  
> For real-time updated pricing tables, global region price differences, and interactive charts, visit the official web tool: [**LLM Abacus (算盘)**](https://llmabacus.com).

---

## Features

- ⚡ **Zero Dependencies**: Pure JavaScript with zero runtime bloat.
- 💰 **Dual Currency Support**: Instant calculations in both **USD ($)** and **CNY (¥)**.
- 🎯 **Prompt Cache Discount Support**: Accurately computes cache-hit rates (such as DeepSeek and Claude prompt caching discounts).
- 🔄 **Cost Comparison**: Rank and compare costs across 50+ models for any given prompt/generation workload.
- 📦 **Dual ESM & CommonJS**: Works out-of-the-box with TypeScript, Node.js, Next.js, Vite, and Cloudflare Workers.

---

## Installation

```bash
npm install llm-pricing-calc
```
or via pnpm / yarn:
```bash
pnpm add llm-pricing-calc
```

---

## Quick Start

### 1. Calculate Exact Cost for a Request

```javascript
import { calculateCost } from "llm-pricing-calc";

// Calculate cost for DeepSeek with Prompt Caching
const result = calculateCost({
  modelId: "deepseek-v4-flash",
  inputTokens: 100_000,         // Uncached input
  cachedInputTokens: 50_000,    // Cache-hit tokens (discounted)
  outputTokens: 20_000,         // Generated tokens
  currency: "USD",              // 'USD' or 'CNY'
});

console.log(result.formatted.total); 
// Output: $0.041000

console.log(result.cost);
// { input: 0.001, cachedInput: 0.00025, output: 0.04, total: 0.04125 }
```

### 2. Compare Multiple Models for the Same Workload

```javascript
import { compareCosts } from "llm-pricing-calc";

// Find which model is most cost-effective for a 500k context task
const comparison = compareCosts({
  inputTokens: 500_000,
  outputTokens: 10_000,
  currency: "USD",
  modelIds: ["gpt-4o", "claude-3-5-sonnet", "deepseek-v4-pro", "qwen3-5-flash"]
});

comparison.forEach((item, index) => {
  console.log(`${index + 1}. ${item.modelName}: ${item.formatted.total}`);
});
```

### 3. Query Model Metadata & Pricing Rates

```javascript
import { getModel, getModels } from "llm-pricing-calc";

// Query specific model
const model = getModel("gpt-4o");
console.log(model);
/*
{
  id: "gpt-4o",
  name: "GPT-4o",
  vendor: "OpenAI",
  input_usd: 2.5,
  output_usd: 10.0,
  cached_input_usd: 1.25,
  context_window: 128000
}
*/

// Filter models by vendor
const alibabaModels = getModels({ vendor: "alibaba" });
```

---

## Supported Vendors

- **DeepSeek** (DeepSeek V3, V4 Flash, V4 Pro)
- **OpenAI** (GPT-4o, GPT-4o mini, o1, o3-mini)
- **Anthropic** (Claude 3.5 Sonnet, Claude 3.5 Haiku, Claude 3 Opus)
- **Google** (Gemini 1.5 Pro, Flash, Gemini 2.0 Flash)
- **Alibaba Cloud** (Qwen 2.5, Qwen 3.5 series)
- **ByteDance** (Doubao series)
- **MiniMax, Baidu, Tencent, Zhipu AI**, and more.

For continuous daily updates and full vendor benchmarks, refer to [**LLM Abacus**](https://llmabacus.com).

---

## Data Attribution & License

- Core library licensed under [MIT](LICENSE).
- Daily verified pricing dataset powered by [LLM Abacus (llmabacus.com)](https://llmabacus.com), licensed under [CC-BY-4.0](https://creativecommons.org/licenses/by/4.0/).
