Tokenomics MCP
This server provides MCP tools for token counting and API cost estimation across OpenAI and Anthropic models, designed for Claude Code CLI.
Count tokens (
count_tokens): Exact or approximate token count for text using tiktoken for OpenAI models; for Claude models, uses Anthropic's API ifANTHROPIC_API_KEYis set, otherwise falls back to an approximation and states the method used.Estimate cost (
estimate_cost): Estimates API cost for input text, optionally including expected output tokens.Compare models (
compare_models_cost): Side-by-side token count and cost comparison across multiple models.List supported models (
list_supported_models): Shows all models with pricing data.Pricing: Manually maintained static pricing table with a last-verified date, so rates may become stale.
Output: Tools return descriptive strings summarizing token count, cost, and method.
Integration: MCP server for Claude Code CLI; install with
claude mcp add -s user.
Provides exact token counting and API cost estimation for OpenAI models such as gpt-4o, gpt-4.1, gpt-5, and o3.
Click on "Deploy Server".
Wait a few minutes for the server to deploy. Once ready, it will show a "Started" state.
In the chat, type
@followed by the MCP server name and your instructions, e.g., "@Tokenomics MCP How many tokens is this prompt for gpt-4o?"
That's it! The server will respond to your query, and you can continue using it as needed.
Here is a step-by-step guide with screenshots.
Tokenomics MCP
An MCP server for counting LLM prompt tokens and estimating API costs across OpenAI and Anthropic models — right inside your chat client, no browser-based token counter needed.
Tools
Tool | What it does |
| Exact/approximate token count for a piece of text |
| $ cost estimate for input + optional expected output |
| Side-by-side cost table across several models |
| See every model this server has pricing data for |
Related MCP server: nikhilnt
How token counting works
OpenAI models (
gpt-4o,gpt-4.1,gpt-5,o3, etc.): exact, via tiktoken.Claude models: exact via Anthropic's
count_tokensAPI ifANTHROPIC_API_KEYis set; otherwise falls back to atiktoken-based approximation, and says so explicitly in the output.
Pricing data lives in src/tokenomics_mcp/pricing.py as a plain dict —
PRICING_LAST_VERIFIED marks the date it was checked. LLM pricing changes
often; update that dict directly when it does.
Project layout
tokenomics-mcp/
├── src/tokenomics_mcp/
│ ├── server.py # MCP tool wiring (thin layer)
│ ├── pricing.py # pricing table + token-counting logic (unit-tested)
│ └── __init__.py
├── tests/
│ └── test_pricing.py # pure-logic tests, no network/API calls needed
├── Dockerfile # multi-stage build, non-root runtime user
├── docker-compose.yml
├── .github/workflows/
│ ├── ci.yml # lint + test on every PR/push to main
│ └── docker-publish.yml # build + push image to GHCR on version tags
├── pyproject.toml
└── .env.exampleLocal development
python -m venv .venv && source .venv/bin/activate
pip install -e ".[dev]"
cp .env.example .env # optional: add ANTHROPIC_API_KEY for exact Claude counts
ruff check . # lint
pytest -v # test
python -m tokenomics_mcp.server # run the server standalone (stdio)Running with Docker
docker build -t tokenomics-mcp .
docker run -i --rm --env-file .env tokenomics-mcpMCP servers communicate over stdio, not a network port — that's why the
Dockerfile has no EXPOSE and the run command uses -i (keep stdin open)
rather than -p (publish a port). docker-compose.yml wraps the same
invocation if you prefer docker compose run tokenomics-mcp.
Connect it to Claude Code
This server is only verified working as an MCP server for the Claude Code
CLI, registered with the claude mcp command — not by hand-editing a
config file.
pip install -e . # or: pip install -e ".[dev]"
claude mcp add tokenomics -s user -- "/absolute/path/to/tokenomics-mcp"
# Windows: claude mcp add tokenomics -s user -- "C:\path\to\repo\.venv\Scripts\tokenomics-mcp.exe"Use -s user (global scope), not the default local/project scope.
Local-scope entries are stored keyed by the literal, unnormalized path
string of the project directory in ~/.claude.json. Different entry points
into Claude Code (a plain shell vs. an IDE extension) can normalize the same
directory to different strings — C:/Projects/tokenomics vs.
C:\Projects\tokenomics vs. c:/Projects/tokenomics all key separately on
Windows — so a server added under one key silently doesn't exist under
another, with no error. User scope isn't keyed by path at all, so it avoids
this entirely and works from any project.
After adding it, restart your Claude Code session (/mcp only reflects the
server list a session loaded at startup) and confirm with /mcp — you
should see tokenomics listed as connected with 4 tools. Then try:
"How many tokens is this prompt for gpt-4o?" or "Compare the cost of this
prompt across gpt-4o, gpt-5, and claude-sonnet-5."
Claude Desktop is not supported
Claude Desktop reads its own separate config file
(claude_desktop_config.json), unrelated to Claude Code's ~/.claude.json.
Registering the server there has not been made to work reliably — edits to
that file were observed to silently revert — so treat Claude Desktop as
unsupported for this server until that's investigated further.
CI/CD
ci.ymlruns on every PR and push tomain: installs the package, lints withruff, runs thepytestsuite. All logic inpricing.pyis unit-tested with stubbed tokenizers, so tests run fast with no network calls or API keys required.docker-publish.ymlruns when you push a version tag (git tag v0.1.0 && git push origin v0.1.0): builds the Docker image and pushes it to GitHub Container Registry (ghcr.io/<your-username>/tokenomics-mcp), tagged both with the version andlatest. No registry account setup needed — it authenticates with theGITHUB_TOKENGitHub Actions already provides.
Releasing a new version
Bump
versioninpyproject.tomland__version__in__init__.py.Commit, merge to
main.Tag and push:
git tag v0.2.0 && git push origin v0.2.0.Watch the Publish Docker image workflow run in the Actions tab — once green, the image is live at
ghcr.io/<your-username>/tokenomics-mcp:v0.2.0.
Limitations
Claude Code CLI only. See "Connect it to Claude Code" above — Claude Desktop is not currently supported.
Must be registered at user scope (
claude mcp add -s user), not local scope, due to the path-key normalization issue described above. Project scope (a checked-in.mcp.json) has not been tested with this server.New sessions required after registering or changing the server. A running Claude Code session doesn't pick up MCP config changes made outside it — start a fresh session and check
/mcpto confirm the tools are live.Claude token counts are exact only with
ANTHROPIC_API_KEYset. Without it (or if the API call fails for any reason),count_tokensandestimate_costfall back to acl100k_basetiktoken approximation for Claude models. The response'sMethod:line always says which path was used — check it if you need guaranteed-exact counts.Pricing is a static, hand-maintained table, not a live feed. Rates can drift from what a provider actually charges;
PRICING_LAST_VERIFIEDinpricing.pyshows how stale it might be, and the tool output repeats that date so you know to double check for anything cost-sensitive.No network calls beyond the optional Claude token-count API. Anything it can't compute locally (OpenAI counts, Claude counts without an API key) is an approximation by design, not a bug.
Notes
The pricing table needs periodic manual updates; there's no live pricing feed to scrape reliably, so this is intentionally a plain, editable dict rather than something auto-fetched.
Available Tools
4 toolscompare_models_costA
Compare token count and estimated cost for the same text across several models.
Args: text: The input text/prompt models: List of model names to compare. Defaults to a mix of current Anthropic and OpenAI models if not specified. expected_output_tokens: Rough guess at response length, applied to all models
Inputs: text (required), models, expected_output_tokens. Returns: result
count_tokensA
Count how many tokens a piece of text would use for a given model.
Args: text: Your text/prompt to tokenize model: Model name, e.g. "claude-sonnet-5", "gpt-5". Call list_supported_models to see all options.
Inputs: text (required), model. Returns: result
estimate_costA
Estimate the API cost of sending this text as input, plus optional expected output.
Args: text: The input text/prompt model: Model name, e.g. "claude-sonnet-5", "gpt-5" expected_output_tokens: Rough guess at response length (default 0 = input only)
Inputs: text (required), model, expected_output_tokens. Returns: result
list_supported_modelsA
List all models this server has pricing data for, with their current rates.
Inputs: no named parameters. Returns: result
Tool Schema Changelog
Recent tool additions, removals, and schema changes observed during successful MCP inspections.
Loading tool schema changelog…
TDQS
Scored across 4 tools
Each tool has a clearly distinct purpose: counting tokens, estimating cost, comparing costs across models, and listing supported models. There is no overlap or ambiguity between them.
All tool names follow a consistent verb_noun pattern: count_tokens, estimate_cost, compare_models_cost, list_supported_models. The naming convention is uniform and predictable.
Four tools is well-scoped for this server's purpose. Each tool addresses a core aspect of tokenomics (counting, costing, comparing, and model discovery) without unnecessary redundancy.
The tool set covers the full lifecycle of token cost analysis: list available models, count tokens, estimate cost, and compare across models. There are no obvious missing operations for the declared purpose.
Maintenance
Related MCP Connectors
Exact Claude API cost calc with real cache economics, plus a tiktoken-misuse scanner.
Use AI models for chat, image, and video generation from Claude Code and other MCP hosts.
Hosted MCP server for LLM cost estimation, model comparison, and budget-aware routing.
MCP server for OpenAI API (chat completions, image generation, embeddings) via AceDataCloud
Related MCP Servers
- AlicenseBqualityDmaintenanceToken usage tracker for OpenAI and Claude APIs with MCP (Model Context Protocol) support.6140 npm5MIT
- AlicenseBqualityDmaintenanceTrack LLM token costs across Claude, GPT and Gemini. MCP server + CLI with optimization hints and $ savings estimates.81MIT
- AlicenseNot gradedqualityDmaintenanceLive LLM pricing as an MCP server. Ask Claude or any MCP client 'how much does this prompt cost?' and get real numbers from a hand-checked pricing table for every major LLM.MIT
- AlicenseNot gradedqualityDmaintenanceEnables AI cost calculation, comparison, and optimization across major providers like Anthropic, OpenAI, Google, Meta, and Mistral. Supports cost estimation, budget-aware model finding, and token estimation through a simple API and MCP integration.MIT