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

modelpricing-ai

v2026.2.8

Published

TypeScript client for ModelPricing.ai — estimate LLM usage costs

Readme

modelpricing-ai

TypeScript client for the ModelPricing.ai API — estimate LLM usage costs and track spending with a single call.

Zero runtime dependencies. Uses native fetch — works in Node.js 18+, Deno, Bun, Cloudflare Workers, and browsers.

Installation

npm install modelpricing-ai

Quick Start

import { ModelPricingClient } from 'modelpricing-ai'

const client = new ModelPricingClient({ apiKey: 'YOUR_API_KEY' })

const estimate = await client.estimate({
    model: 'gpt-4o-mini',
    tokensIn: 1000,
    tokensOut: 500,
    traceId: { requestId: 'abc-123' }
})

console.log(`Cost: $${estimate.total.toFixed(6)}`)

Response Structure

estimate() returns an EstimateResponse object:

estimate.total // number — total USD cost
estimate.model // string — canonical model name
estimate.traceId // object | null — your pass-through trace ID
estimate.trace // string — server-assigned trace identifier
estimate.breakdown.input.unit.branch.qty.rate.subtotal.output // EstimateBreakdownGroup // EstimateBreakdown //   string — e.g. "per-1M-input" //   string — pricing tier that matched //   number — number of input tokens //   number — per-unit rate //   number — input cost // EstimateBreakdown (same fields for output tokens)

Configuration

| Parameter | Default | Description | | ------------ | ------------------------------- | ------------------------------------------------------------------------ | | apiKey | required | Your ModelPricing.ai API key (also reads MODELPRICING_API_KEY env var) | | baseUrl | "https://api.modelpricing.ai" | API base URL (also reads MODELPRICING_BASE_URL env var) | | timeout | 30000 | Request timeout in milliseconds | | maxRetries | 3 | Maximum retry attempts for transient errors | | fetch | globalThis.fetch | Optional custom fetch function for testing or custom HTTP |

Parameters are resolved in order: constructor option > environment variable > default.

const client = new ModelPricingClient({
    apiKey: 'YOUR_API_KEY',
    baseUrl: 'https://api.modelpricing.ai',
    timeout: 30000,
    maxRetries: 3
})

Error Handling

The client raises typed exceptions for different failure modes:

| Exception | HTTP Status | When | | ----------------- | ----------- | ----------------------------- | | Unauthorized | 401 | Invalid or missing API key | | ValidationError | 422 | Invalid model name or metrics | | NotFound | 404 | Unknown endpoint | | ServerError | 5xx | Server-side failures |

All exceptions inherit from ModelPricingError and include a statusCode property.

import { Unauthorized, ValidationError, ServerError } from 'modelpricing-ai'

try {
    const estimate = await client.estimate({
        model: 'gpt-4o-mini',
        tokensIn: 1000,
        tokensOut: 500
    })
} catch (error) {
    if (error instanceof Unauthorized) {
        console.log('Check your API key')
    } else if (error instanceof ValidationError) {
        console.log(`Bad request: ${error.message}`)
    } else if (error instanceof ServerError) {
        console.log('Server error — will be retried automatically')
    }
}

Retry Behavior

The client automatically retries on transient errors with exponential backoff:

  • Retries: 5xx server errors and network errors (TypeError from fetch)
  • No retry: 4xx client errors (401, 404, 422)
  • Default: 3 retries with exponential backoff + jitter
// Increase retries for unreliable networks
const client = new ModelPricingClient({ apiKey: 'YOUR_API_KEY', maxRetries: 5 })

// Disable retries (no retry attempts)
const client = new ModelPricingClient({ apiKey: 'YOUR_API_KEY', maxRetries: 0 })

License

MIT