TokenWise MCP Server
README.md
# TokenWise: Zero-Dependency LLM & AI Agent FinOps CLI + MCP Server
`tokenwise` is a lightweight, zero-dependency Python CLI and Model Context Protocol (MCP) server for developers and AI engineers to estimate, audit, and optimize LLM costs across major providers (OpenAI, Anthropic, Google) completely offline.
## Features
- **Zero Dependencies**: Runs anywhere with Python 3.9+ using only the Python standard library.
- **Offline-First**: Heuristic tokenization calibrated for modern models, no API keys or proxies required.
- **Multi-Provider Registry (2025/2026)**: Supports `gpt-4.1`, `gpt-4.1-mini`, `gpt-4.1-nano`, `o4-mini`, `gpt-4o`, `claude-opus-4`, `claude-sonnet-4`, `claude-3-7-sonnet`, `claude-3-5-haiku`, `gemini-2.5-pro`, `gemini-2.5-flash`, `gemini-2.0-flash`, and legacy models.
- **Codebase Scanner & CI Gate**: Detect expensive/legacy model defaults, missing `max_tokens` caps, and bloated prompts with `--markdown` PR comments and `--fail-on-high`.
- **Log Auditor & HTML Executive Report**: Audit `JSONL`, `JSON`, or `CSV` usage logs for spend, model-routing savings, `--max-cost` CI budget gates, and standalone `--html` reports.
- **MCP Server Mode (`tokenwise mcp`)**: Zero-dependency JSON-RPC 2.0 stdio server so AI coding agents (Claude Code, Cursor, Windsurf) can self-check prompt costs before calling frontier models.
## Installation
```bash
pip install tokenwise
```
For local/editable installation:
```bash
git clone https://github.com/<your-org>/tokenwise.git
cd tokenwise
pip install -e .
```
## Usage
### 1. Estimate Cost for a Prompt
```bash
tokenwise estimate --text "How do I optimize my LLM costs?" --output-tokens 1000 --monthly-requests 50000
tokenwise estimate --file prompt.txt --models gpt-4.1 gpt-4.1-mini claude-sonnet-4
```
### 2. Scan a Codebase for Cost Risks
```bash
tokenwise scan ./src
tokenwise scan ./src --markdown --fail-on-high
```
### 3. Audit Usage Logs & Export HTML Report
```bash
tokenwise audit usage.jsonl
tokenwise audit usage.jsonl --max-cost 25.00
tokenwise audit usage.jsonl --html finops-report.html
```
### 4. List Supported Models & Pricing
```bash
tokenwise models
tokenwise models --provider anthropic --json
```
### 5. Run as an MCP Server (Claude Code / Cursor / Windsurf)
Start the stdio server:
```bash
tokenwise mcp
```
Add to your MCP client configuration (e.g., `.mcp.json` or Cursor MCP settings):
```json
{
"mcpServers": {
"tokenwise": {
"command": "tokenwise",
"args": ["mcp"]
}
}
}
```
Exposed MCP tools: `estimate_prompt_cost`, `scan_codebase_costs`, `audit_usage_log`, `list_model_pricing`.
### 6. One-Command CI, MCP & Pre-Commit Setup (`tokenwise init`)
Scaffold `.mcp.json` and `.github/workflows/tokenwise.yml` in any repo:
```bash
tokenwise init
tokenwise init --all # Also generates .pre-commit-config.yaml
tokenwise init --mcp --force # Overwrite existing .mcp.json
```
Or reference `.pre-commit-hooks.yaml` directly in `.pre-commit-config.yaml` via the `tokenwise-scan` hook.
## TokenWise FinOps Team Kit ($19)
Want automated PR cost-risk comments, Slack/Discord daily spend alerts, and an executive LLM model-routing playbook?
Check out the **TokenWise FinOps Team Kit** (`pro_kit/`).
## Pricing Data
Prices are based on public API list rates and provided for estimation and FinOps auditing purposes.
## License
MIT
This server cannot be deployed
Maintenance
ActivityMaintained
ResponsivenessNo recent community issues