AI cost control JavaScriptPython

llm-cost-shieldStop surprise LLM bills with per-user budgets, caching and cheap-model routing

llm-cost-shield is a free, open-source library for JavaScript and Python that caps what each user and each day can spend on OpenAI, Anthropic, Gemini or any LLM API. It reserves budget before every call, caches repeated answers per user, routes easy requests to cheaper models and reads your usage logs to show where the money leaks.

npm npm install llm-cost-shield
PyPI pip install llm-cost-shield

Free · MIT license · runs on your machine · no signup npm ↗PyPI ↗GitHub ↗

Why this matters now

One looping agent or one abusive user can run up a month's API budget overnight. Providers bill after the fact, so the only real limit is the one in your own code.

How llm-cost-shield works

An app with an AI feature has no brakes by default. One user stuck in a loop or one bot hammering your chat widget can spend a month’s budget overnight. And most apps pay full price to answer the same questions again and again.

llm-cost-shield wraps your model calls with budgets per day, per user and per feature, stops retry and agent loops, caches identical requests, and can route simple prompts to a cheaper model. It reads the real token usage that OpenAI, Anthropic, Gemini, Bedrock, OpenRouter and the Vercel AI SDK return, including cache reads and writes, thinking tokens and long-context rates.

The analyzer reads your usage logs and shows cost by model, feature and user, how much repeated requests cost you, what moving small requests to a cheaper model would save, how much goes to thinking tokens, whether prompt caching actually saves money, and which users or loops look abnormal. Fewer tokens also means less compute and energy.

Try it

npx llm-cost-shield analyze usage.jsonl --prices prices.json

Free and open source under the MIT license. Want this done for your store, or need help with the findings? Book a call.

Agent skill included

Let your AI coding agent run llm-cost-shield for you

The llm-cost-review skill works with Claude Code, Codex, Cursor, Gemini CLI and other agents that read SKILL.md files. It runs the tool, explains each finding in plain words and helps you fix it.

npx skills add MuhammadAhmadZia/llm-cost-shield --skill llm-cost-review

Using the Claude or ChatGPT apps instead? Download the skill ZIP from the GitHub releases page and upload it under Skills.

See all 30 agent skills →

llm-cost-shield compared with alternatives

OptionHow it differs
llm-spend-guard (npm)Token budgets for Node.js; llm-cost-shield adds USD accounting with thinking tokens, cache writes and batch prices, a Python version and a usage-log analyzer
LiteLLM budget managerBudgets inside the LiteLLM proxy; llm-cost-shield is a library with no proxy to run

Based on public documentation and our own test files. Each project has different goals; try them on your own data.

Questions about llm-cost-shield

Is there a Python version?

Yes: pip install llm-cost-shield. It has the same budgets, loop guard, cache and analyzer, works with sync and async SDK clients, and is tested against the JavaScript version so both give the same costs.

Does it include model prices?

No, because prices change often. You copy current prices from your provider's pricing page into a small JSON file.

Does it work on serverless?

Yes. Pass a store backed by Redis, Upstash, Vercel KV or Supabase so limits hold across instances. It also runs on edge runtimes.

Does it count thinking tokens?

Yes. Gemini reports thinking tokens separately from the answer and bills them as output, and Anthropic has two cache-write prices. llm-cost-shield reads both, so the cost it records matches the bill.

Can parallel requests get past the budget?

No. Each call reserves its worst-case cost before it runs, so 20 requests at the same moment cannot spend more than the limit allows.

UN Sustainable Development Goals

How each tool maps to SDG targets →

Want this set up and tuned for your store?

The tools are free. If you would rather have the checks run, the findings fixed and the results measured for you, that is what I do for clients.