{
  "markdown": "<!-- mcp-name: io.github.zhaoyue722/llm-usage-mcp -->\n\n<p align=\"center\">\n  <img src=\"https://raw.githubusercontent.com/zhaoyue722/llm-usage-mcp/main/docs/assets/watch-pom.png\" alt=\"llm-usage-mcp\" width=\"140\">\n</p>\n\n<h1 align=\"center\">llm-usage-mcp</h1>\n\n<p align=\"center\"><em>LLM Usage &amp; Cost Tracker — your local-first spend watchdog</em></p>\n\n<p align=\"center\">\n  <a href=\"https://github.com/zhaoyue722/llm-usage-mcp/actions/workflows/ci.yml\"><img src=\"https://github.com/zhaoyue722/llm-usage-mcp/actions/workflows/ci.yml/badge.svg\" alt=\"CI\"></a>\n  <a href=\"https://github.com/zhaoyue722/llm-usage-mcp/blob/main/LICENSE\"><img src=\"https://img.shields.io/badge/License-MIT-blue.svg\" alt=\"License: MIT\"></a>\n  <a href=\"https://www.python.org/downloads/\"><img src=\"https://img.shields.io/badge/python-3.13%2B-blue.svg\" alt=\"Python 3.13+\"></a>\n  <a href=\"https://glama.ai/mcp/servers/zhaoyue722/llm-usage-mcp\"><img src=\"https://glama.ai/mcp/servers/zhaoyue722/llm-usage-mcp/badges/score.svg\" alt=\"Glama score\"></a>\n</p>\n\n<p align=\"center\">English | <a href=\"https://github.com/zhaoyue722/llm-usage-mcp/blob/main/README.zh.md\">中文</a></p>\n\nStop treating your LLM API bills like a scary horror movie you only look at through your fingers at the end of the month. Know what your LLM calls actually cost — across every provider, in one place, on your own machine. Ask your coding agent (MCP) or type a command (CLI).\n\nIt's a cost **meter**, not a router: it tells you what you spent and which provider fits a workload — it never changes your calls. Pairs happily alongside a router or a model-leaderboard tool.\n\n![Claude Code answering \"how much did I spend?\" via llm-usage](https://raw.githubusercontent.com/zhaoyue722/llm-usage-mcp/main/docs/assets/agent-spend.png)\n\nOr straight from the terminal — your week's spend, broken down by provider, and a cross-provider cost comparison before you commit to a model:\n\n![llm-usage CLI: weekly spend by provider and a cross-provider cost comparison](https://raw.githubusercontent.com/zhaoyue722/llm-usage-mcp/main/docs/assets/demo.gif)\n\n## Why you'd want this\n\nYou're calling LLMs from a handful of providers — Claude, GPT, plus Chinese models like Qwen and DeepSeek. Each one bills in its own dashboard, in its own currency, with its own rules for what a \"cached token\" costs. So the simplest possible question — *how much am I spending, and on what?* — turns into four browser logins, looking up exchange rates for RMB to USD, and trying to decipher what a \"cached context token discount\" actually means in midnight math. Most people just cross their fingers and let the bill be a surprise at the end of the month.\n\n`llm-usage-mcp` captures every call you make into one local store, costs it correctly per provider at the moment it happens, and hands the answer back **two ways**:\n\n- **Ask your coding agent.** It's an MCP server, so Claude Code, Cursor, or any MCP client can answer *\"how much did I spend on Claude this week?\"* or *\"which provider is cheapest for a 10k-in / 2k-out call?\"* in plain English.\n- **Or type a command.** It's also a CLI — `llm-usage spend`, `llm-usage compare`, `llm-usage recommend` — for when you'd rather not round-trip through an agent.\n\nAnd it stays out of your way:\n\n- **Local-first.** No SaaS, no signup, no telemetry. Just a SQLite file at `~/.llm-usage/usage.db`. Privacy is a feature, not a setting.\n- **Multi-provider, Chinese models included.** Anthropic, OpenAI, DeepSeek, Qwen — streaming and non-streaming for all four. DeepSeek and Qwen run the same capture path as Anthropic and OpenAI, not a bolted-on afterthought. More providers (Gemini, Bedrock, Moonshot, …) are [on the way](#supported-providers).\n\n## Quickstart\n\nTwo minutes from `git clone` to your first captured call. This part is about **capture** — getting calls recorded. [Reading the data back](#querying-your-spend) comes next.\n\n### 1. Install\n\nInstall from PyPI with [uv](https://docs.astral.sh/uv/) (or `pipx`) — this puts the three console scripts on your `PATH`:\n\n```bash\nuv tool install llm-usage-mcp   # or: pipx install llm-usage-mcp\n```\n\nPrefer to hack on it? Clone and sync from source instead:\n\n```bash\ngit clone https://github.com/zhaoyue722/llm-usage-mcp.git\ncd llm-usage-mcp\nuv sync\n```\n\nEither way you get three console scripts:\n- `llm-usage` — the multi-command CLI. See [From the command line (CLI)](#from-the-command-line-cli) below.\n- `llm-usage-mcp` — the stdio MCP server.\n- `llm-usage-proxy` — a back-compat alias; identical to `llm-usage proxy`.\n\n> The Quickstart below uses `uv run …` (the from-source workflow). If you installed from PyPI, the scripts are already on your `PATH` — drop the `uv run` prefix, and register the MCP server with `claude mcp add llm-usage -- llm-usage-mcp`.\n\n### 2. Set at least one API key\n\nYou only need a key for the provider(s) you actually use; the proxy starts regardless and per-route requests return `503 configuration_error` for any provider whose key is missing.\n\n```bash\nexport ANTHROPIC_API_KEY=sk-ant-...\n# and/or:\nexport OPENAI_API_KEY=sk-...\nexport DEEPSEEK_API_KEY=sk-...\nexport DASHSCOPE_API_KEY=sk-...   # Qwen\n```\n\nFull env-var reference: [`docs/configuration.md`](https://github.com/zhaoyue722/llm-usage-mcp/blob/main/docs/configuration.md) (or copy [`.env.example`](https://github.com/zhaoyue722/llm-usage-mcp/blob/main/.env.example) to `.env` and fill in).\n\n### 3. Run the capture proxy\n\n```bash\nuv run llm-usage-proxy\n```\n\nIt binds **loopback-only** (`127.0.0.1:5525`) — never reachable from the network. The proxy holds your API keys server-side; clients never need them.\n\n### 4. Point your coding agent at the proxy\n\nThe proxy exposes one route per provider. Set the matching `*_BASE_URL` env var on the client side:\n\n| Provider | Client env var | Value |\n|---|---|---|\n| Anthropic | `ANTHROPIC_BASE_URL` | `http://127.0.0.1:5525` |\n| OpenAI | `OPENAI_BASE_URL` | `http://127.0.0.1:5525/openai/v1` |\n| DeepSeek | `DEEPSEEK_BASE_URL` (or any OpenAI-SDK base-url override) | `http://127.0.0.1:5525/deepseek/v1` |\n| Qwen | DashScope OpenAI-compatible base | `http://127.0.0.1:5525/qwen/v1` |\n\nExample — launch Claude Code with calls routed through the proxy:\n\n```bash\nANTHROPIC_BASE_URL=http://127.0.0.1:5525 claude\n```\n\n### 5. Confirm it's capturing\n\nMake a call through your agent (or any client pointed at the proxy), then check it landed:\n\n```bash\nuv run llm-usage spend\n```\n\nEvery call lands in `~/.llm-usage/usage.db` with tokens, cost, latency, and a `request_id` for idempotency — and shows up in that headline. That's the whole loop: capture on one side, answers on the other.\n\n## Querying your spend\n\nOnce calls are being captured, you read them back two ways. Same data, same numbers — pick whichever fits the moment.\n\n### Ask your coding agent (MCP)\n\nRegister the MCP server with Claude Code:\n\n```bash\nclaude mcp add llm-usage -- uv --directory $(pwd) run llm-usage-mcp\n```\n\nThen just ask, in plain English, inside that session:\n\n> How much did I spend on Anthropic today? Which provider is cheapest for a 10k-input / 2k-output call?\n\nClaude picks the right tool and reads the numbers back. Seven tools are exposed over stdio; full param/return shapes are in [`docs/spec.md`](https://github.com/zhaoyue722/llm-usage-mcp/blob/main/docs/spec.md).\n\n| Tool | Purpose |\n|---|---|\n| `query_spend` | Totals + per-group rollups over a time window (group by provider / model / project / tag / day). |\n| `usage_summary` | Headline summary for `today` / `week` / `month` / `year` — totals, top-N providers + models, largest call. |\n| `compare_providers` | Given a hypothetical workload (tokens in / out), rank every priced model by cost. |\n| `recommend_provider` | Pick the cheapest priced model that fits a stated budget. |\n| `get_pricing` | Inspect the vendored pricing snapshot. |\n| `list_providers` | List providers + their models + OpenAI-compatibility flag. |\n| `record_usage` | Manual write path — log a call when the capture proxy isn't in the picture. |\n\n`query_spend` and `usage_summary` default to `include_failed=false` so partial-stream rows don't pollute totals; opt-in via the param.\n\n### From the command line (CLI)\n\nThe same questions, as a CLI — eight subcommands under one `llm-usage` console, for when typing is faster than asking your agent.\n\n> The examples below assume `llm-usage` is on your `PATH` — either `source .venv/bin/activate` or `uv tool install .`. Otherwise, prefix each command with `uv run` (e.g. `uv run llm-usage spend`).\n\n```text\n$ llm-usage\n Local-first LLM spend capture + query, exposed over MCP.\n\n Commands\n   proxy      Run the local LLM capture proxy on 127.0.0.1.\n   compare    Project the cost of a hypothetical workload across every priced model.\n   models     Browse the local pricing catalog.\n   recommend  Recommend the cheapest priced model for a workload + budget.\n   spend      Show recorded spend over a calendar period.\n   status     Snapshot of the local install: DB, proxy, providers, pricing.\n   providers  List configured providers with key state, wire-format, model count.\n   about      Show version, author, license, and the project homepage.\n```\n\n| Command | The question it answers |\n|---|---|\n| [`compare`](#compare) | Given a workload, who's cheapest? |\n| [`models`](#models) | What do they actually charge per million tokens? |\n| [`recommend`](#recommend) | I've got $0.04 left — which model won't bankrupt me? |\n| [`spend`](#spend) | How much did I just spend? |\n| [`status`](#status) | Is everything actually working? |\n| [`providers`](#providers) | What's configured locally? |\n| [`about`](#about) | What is this, and where do I report a bug? |\n| `proxy` | Run the capture proxy (same as `llm-usage-proxy`). |\n\nConventions that hold across every command:\n\n- `--json` emits the same Pydantic shape the matching MCP tool returns. Pipe straight into `jq`.\n- `--color {auto,always,never}` honors `NO_COLOR` and TTY detection. The palette is a warm, low-contrast dark theme — easy on the eyes at 11pm.\n- Filter flags (`--provider`, `--model`) are case-insensitive on providers, case-sensitive on models, and repeatable where they act as whitelists.\n- `--version` / `-V` prints the version and exits. `--install-completion {bash|zsh|fish|powershell}` installs a tab-completion script — one shell restart later, every flag is `<Tab>`-able.\n\n#### `compare`\n\nRank every priced model by projected cost for an `n`-input / `m`-output call. Cheapest first, percent against the cheapest. Default view family-deduplicates rows that share both a model family root *and* an identical price — so `gpt-5-mini` and `gpt-5-mini-2025-08-07` collapse to one row with `×2`. Pass `--all` to see every catalog row.\n\n```bash\n# How does an 8k-in / 2k-out call price out today?\n$ llm-usage compare --in 8000 --out 2000\n\n# Just OpenAI's models:\n$ llm-usage compare --in 8000 --out 2000 --model gpt-5-mini --model gpt-5-nano\n\n# Same projection, JSON for a script:\n$ llm-usage compare --in 8000 --out 2000 --json | jq '.ranked[0]'\n```\n\n![llm-usage compare ranking models by projected cost](https://raw.githubusercontent.com/zhaoyue722/llm-usage-mcp/main/docs/assets/cli-compare.png)\n\n#### `models`\n\nCatalog browser. Sibling of `compare`, but answers \"what does this model charge?\" rather than \"what would my workload cost?\". Rates per million tokens, sorted alphabetically by provider by default; switch with `--sort input` or `--sort output` to find the cheapest in either axis. Cache rates are hidden until you ask (`--cache`) because most models don't have them and empty columns waste width.\n\n```bash\n# Full catalog, deduped.\n$ llm-usage models\n\n# OpenAI's nano models only, with cache rates:\n$ llm-usage models --provider openai --match nano --cache\n\n# Cheapest input rate first — quick \"what's the floor right now?\":\n$ llm-usage models --sort input\n```\n\n#### `recommend`\n\nPicks one. Filters by `--provider`, `--model`, and `--budget`, then returns the cheapest match plus two runner-ups. The reasoning string explains what it assumed and what got chosen, so you can sanity-check rather than trust blindly.\n\n```bash\n# Cheapest priced model, full stop.\n$ llm-usage recommend\n\n# Anything Anthropic that fits under one cent for a 1k/1k call:\n$ llm-usage recommend --provider anthropic --budget 0.01\n\n# Of these three specific candidates, which wins?\n$ llm-usage recommend --model gpt-5-mini --model claude-sonnet-4-6 --model qwen-max\n```\n\nv1 ranks by cost only. `--task` is optional and surfaces in the reasoning text; it doesn't drive selection (the tool isn't an LLM and can't interpret free text).\n\n#### `spend`\n\nRead the SQLite. The default view is a `usage_summary` headline — total dollars, top-3 providers, top-3 models, largest single call. Pass `--group-by` to switch into rollup mode.\n\n```bash\n# Headline for this week.\n$ llm-usage spend\n\n# This month grouped by model, JSON for a dashboard:\n$ llm-usage spend --period month --group-by model --json | jq\n\n# Spend on a specific project tag, day-by-day:\n$ llm-usage spend --group-by day --project my-side-thing\n```\n\nPeriod boundaries are calendar UTC: `today` = since 00:00 UTC, `week` = since Monday, `month` = since the 1st, `year` = since January 1st. Failed / partial-stream rows are excluded by default; opt in with `--include-failed`.\n\n![llm-usage spend headline — totals, top providers, largest call](https://raw.githubusercontent.com/zhaoyue722/llm-usage-mcp/main/docs/assets/cli-spend.png)\n\n#### `status`\n\nOne screen, four sections: Database, Capture proxy, Providers, Pricing. The \"is everything actually working?\" command. Read-only — running it on a fresh install before you've ever booted the proxy or MCP server prints `database not initialized` rather than silently creating the file.\n\n```bash\n$ llm-usage status\n\n# Skip the network probe (offline, CI, slow link):\n$ llm-usage status --no-net\n\n# Machine-readable for a healthcheck script:\n$ llm-usage status --json\n```\n\n#### `providers`\n\nPer-provider configuration view. Wider than the `status` Providers block: adds the wire-format flag (`openai-compat: yes/no`) and an optional `--models` expansion that lists every priced model under each provider.\n\n```bash\n$ llm-usage providers\n$ llm-usage providers --models   # expand each provider with its model list\n```\n\n#### `about`\n\nThe front-door panel: version, author, license, and the project homepage. The human-facing companion to `--version` — fields are read from the installed package metadata, so they match what PyPI shows.\n\n```bash\n$ llm-usage about\n\n# Machine-readable, for a script or an issue template:\n$ llm-usage about --json\n```\n\n## Supported providers\n\n| Provider | Auth | Non-streaming | Streaming | Cache pricing |\n|---|---|---|---|---|\n| Anthropic | `x-api-key` | yes | yes | `cache_creation` + `cache_read` |\n| OpenAI | `Bearer` | yes | yes | nested `prompt_tokens_details.cached_tokens` |\n| DeepSeek | `Bearer` | yes | yes | `prompt_cache_hit_tokens` / `_miss_tokens` |\n| Qwen (DashScope) | `Bearer` | yes | yes | usually omitted on the OpenAI-compat endpoint |\n\n**More on the way.** Google Gemini, AWS Bedrock, Moonshot (Kimi), Zhipu GLM, MiniMax, and others are scoped in [`docs/post_v1_providers.md`](https://github.com/zhaoyue722/llm-usage-mcp/blob/main/docs/post_v1_providers.md).\n\n**Where prices come from.** Pricing is a vendored, trimmed snapshot of [LiteLLM's pricing JSON](https://github.com/BerriAI/litellm/blob/main/litellm/model_prices_and_context_window_backup.json), refreshed weekly by a GitHub Action ([`refresh-pricing.yml`](.github/workflows/refresh-pricing.yml)). Models LiteLLM doesn't carry yet are filled in locally via [`pricing_overrides.json`](src/llm_usage/core/pricing_data/pricing_overrides.json).\n\n## Configuration\n\nEverything is env vars (or a `.env` file at the repo root). Defaults are sane — nothing is required to start the proxy. Full reference: [`docs/configuration.md`](https://github.com/zhaoyue722/llm-usage-mcp/blob/main/docs/configuration.md). The three you're most likely to touch:\n\n| Variable | Default | Purpose |\n|---|---|---|\n| `LLM_USAGE_DB_URL` | `sqlite:///$HOME/.llm-usage/usage.db` | Where the local DB lives. |\n| `LLM_USAGE_PROXY_PORT` | `5525` | Capture proxy port (loopback only). |\n| `LLM_USAGE_<PROVIDER>_BASE_URL` | each provider's official endpoint | Point a provider at a reverse proxy / gateway — handy in network-restricted regions. |\n\n## Docker\n\nA minimal [`Dockerfile`](https://github.com/zhaoyue722/llm-usage-mcp/blob/main/Dockerfile) is included **only** for automated MCP registry validation (e.g. Glama), which verifies that the packaged server boots and responds to MCP introspection. The recommended way to run the server is still `uvx llm-usage-mcp` locally — this is a local-first tool, not a hosted service.\n\n## License\n\n[MIT](https://github.com/zhaoyue722/llm-usage-mcp/blob/main/LICENSE).\n",
  "bytes": 16826,
  "sha": "38e578df0d825efa1e87979140cabd5d4c6bb8d8c6d83208b05b763aed5f0de8",
  "repo_slug": "zhaoyue722/llm-usage-mcp",
  "fonte": "repo",
  "truncated": false,
  "api": "https://agentalog.com/api/listings/mcp_io_github_zhaoyue722_llm_usage_mcp_e530d741/readme"
}