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

openrouter-cost-calculator

v1.6.4

Published

Lightweight AI cost and credit calculator for OpenRouter models

Readme

OpenRouter Cost Calculator

A lightweight utility to calculate costs for AI models (OpenRouter, OpenAI, etc.) and convert them into internal credits. Supports both Offline Calculation (using stored pricing) and Online Calculation (fetching real-time pricing from OpenRouter API).

Features

  • Dual Mode:
    • Offline: Zero-latency calculation using your own pricing data (e.g., from DB).
    • Online: Auto-fetch model pricing from OpenRouter API.
  • Flexible Billing: Configure margin multipliers and USD-to-Credit conversion rates.
  • Input-Focused: Calculates cost based primarily on input (prompt) tokens, as per modern pricing strategies.

Installation

npm install openrouter-cost-calculator

Usage

1. Offline Mode (Recommended for Speed)

Best for production environments where you store model pricing in your database. No API calls required.

import { OpenRouterCalculator } from "openrouter-cost-calculator";

// 1. Initialize
const calculator = new OpenRouterCalculator();

// 2. Configure Billing (Optional)
calculator.setBillingConfig({
  marginMultiplier: 2.0, // e.g. 2x margin
  usdPerCredit: 0.005,   // $0.005 = 1 Credit
});

// 3. Set Pricing (e.g. from your DB)
calculator.setPricing({
  prompt: "0.0000025",
  completion: "0.00001", // (Optional, ignored in calculation if not using output tokens)
  image: "0.004",
  request: "0"
});

// 4. Calculate
const result = calculator.calculateEstimate({
  prompt_tokens: 150
});

console.log(`USD Cost: $${result.usdCost}`);
console.log(`Credits: ${result.credit}`);

2. Online Mode (Auto-Fetch Pricing)

Useful for scripts or environments where you want to fetch the latest pricing from OpenRouter.

import { OpenRouterCalculator } from "openrouter-cost-calculator";

// 1. Initialize with API Key
const calculator = new OpenRouterCalculator("YOUR_OPENROUTER_API_KEY");

calculator.setBillingConfig({
  marginMultiplier: 1.2
});

// 2. Calculate directly (Fetches models internally)
// calculateCost(modelId, usage, imageCount?)
const result = await calculator.calculateCost(
  "openai/gpt-4",
  { prompt_tokens: 500 },
  0 // image count
);

if (result.modelDetails) {
  console.log(`Model: ${result.modelDetails.id}`);
  console.log(`Input Cost: $${result.usdCost}`);
} else {
  console.log("Model pricing not found");
}

Configuration

| Option | Description | Default | |--------|-------------|---------| | marginMultiplier | Multiplier for revenue calculation (e.g. 2.0 = 200% price) | 1.0 | | usdPerCredit | USD value of 1 internal credit unit | undefined | | minimumCredit | Minimum credits to charge if calculated < min | 0 |

License

MIT