Back to articles
AI Trends

Stop Hardcoding Your Token Prices to the Code

January 23, 20265 min readBy Mikko Tikkanen
Stop Hardcoding Your Token Prices to the Code

Every AI-related project I've built inevitably ends up having the same problem buried somewhere in the codebase: a file called pricing.ts or costs.config.js with numbers like this:

const PRICES = {
  'gpt-4o': { input: 2.5, output: 10 },
  'claude-sonnet-4': { input: 3, output: 15 },
}

These are the provider prices of tokens per model, used to track the actual running cost of AI usage. Given time, these numbers are guaranteed to be wrong. At minimum, the prices change over time and costs start to drift. At worst, models change and suddenly you have no cost data at all. All usage just became zero cost—most excellent!

Not only is this file guaranteed to be out of date, it is also incredibly annoying to create and to keep up to date. Hunting down the exact model from each pricing page and remembering to do that regularly is not what I signed up for.

Building the solution

For quite some time I've been squinting at this problem. The solution would need to be an npm package that provides programmatic access to automatically updating pricing data. I should not need to modify or update anything when I switch models.

So, something like this:

import { CostClient } from 'token-costs';

const client = new CostClient();

const cost = await client.calculateCost('anthropic', 'claude-sonnet-4', {
  inputTokens: 1500,
  outputTokens: 800,
});
console.log(cost.totalCost);  // actual accrued cost

No more hardcoding. No more stale data. The prices would update automatically at 00:01 UTC every day. You just give it the model name with token counts and out comes cost in dollars. If Anthropic drops their prices tomorrow, your cost calculations adjust without you doing anything. Or you could even switch models every minute and still get accurate cost data.

However, how would you actually build this? I eventually realized GitHub Actions plus public JSON files would do the trick. However, the pain was so sporadic that it always sank to the bottom of the todo list. But then the world shifted.

In late 2025, Anthropic released Opus 4.5 to be used with Claude Code, and custom software turned into a commodity overnight. After just a couple of days of back-and-forth, I had something built that seems to work. The result is split into two parts: daily pricing data crawlers and an npm module for cost calculations.

The crawlers scrape the pricing pages of OpenAI, Anthropic, Google, and OpenRouter and pull the latest prices per model. Out of the crawlers comes a historical running archive of raw pricing data. The historical data is in turn distilled into smaller files that are easier to consume. The npm package then fetches these smaller JSON files upon startup.

And boom, we now can calculate real costs of tokens spent, without any manually updated pricing data!

The edge cases

Every pricing page has its own quirks and structures, so the crawlers are bespoke to each site. They'll inevitably break, but at least everything is open and out there now, so hopefully others can help as well when this happens.

Regardless of the best intentions, there will be a moment every day when the actual prices have not been updated just yet. For this there is a staleness flag built in. If you end up requesting cost data just when it is updated the package tells you. For most use cases this doesn't matter. Prices rarely change daily. But if you need strict accuracy, you can check the flag and decide how to handle it.

For cases where startup times are critical, you can even build your own external caching solution. Plug in Redis or any cache, and the package uses it instead of refetching on every invocation.

const client = new CostClient({
  externalCache: {
    get: (key) => redis.get(key),
    set: (key, value) => redis.set(key, value, { ex: 86400 }),
  },
});

You can also run entirely offline with custom pricing data if you have internal models or negotiated rates. Or when absolute beast mode loading times are needed.

A proposal for providers

Building scrapers is messy and fragile. So I have a question: why are we even scraping pricing pages in 2026? The moment a marketing guy presses publish on any of the pricing page redesign, things break. It can be fixed and the problems are not that big, but it should not be so.

What if providers just published a /llm_prices.json file at their domain root? Like robots.txt but for pricing. One standard format, one source of truth for prices, no scraping needed—a simple JSON file, model IDs as keys, prices per million tokens.

Probably won't happen. But it should. (More details in the documentation)

Try it

The package is on npm: npm install token-costs

Documentation and API endpoints: mikkotikkanen.github.io/token-costs

If you're building anything that needs to track LLM costs—internal tooling, customer dashboards, or just understanding which queries end up costing you the most—this is what I built it for.

MT

Mikko Tikkanen

Fixer of the North