Metadata-Version: 2.4
Name: yaptax
Version: 0.6.0
Summary: Measure and cut the language tax in AI prompts: compare GPT, Claude and Gemini token costs per language, slim prompts, and route each language to the cheapest model.
Author: NeuroTitan Labs
License-Expression: MIT
Project-URL: Homepage, https://neurotitan.in/cost
Project-URL: Source, https://github.com/NeuroTitan-Hub/yaptax
Project-URL: Issues, https://github.com/NeuroTitan-Hub/yaptax/issues
Keywords: llm,tokens,tokenizer,multilingual,cost,claude,gemini,openai,router,prompt-compression
Classifier: Programming Language :: Python :: 3
Classifier: Environment :: Console
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: tiktoken>=0.7
Provides-Extra: hf
Requires-Dist: tokenizers>=0.19; extra == "hf"
Requires-Dist: huggingface_hub>=0.23; extra == "hf"
Provides-Extra: claude
Requires-Dist: anthropic>=1; extra == "claude"
Provides-Extra: mcp
Requires-Dist: mcp>=1.2; extra == "mcp"
Provides-Extra: router
Requires-Dist: fastapi>=0.110; extra == "router"
Requires-Dist: uvicorn>=0.29; extra == "router"
Requires-Dist: httpx>=0.27; extra == "router"
Requires-Dist: tomli>=2; python_version < "3.11" and extra == "router"
Provides-Extra: test
Requires-Dist: pytest>=7; extra == "test"
Requires-Dist: mcp>=1.2; extra == "test"
Requires-Dist: fastapi>=0.110; extra == "test"
Requires-Dist: uvicorn>=0.29; extra == "test"
Requires-Dist: httpx>=0.27; extra == "test"
Requires-Dist: tomli>=2; python_version < "3.11" and extra == "test"
Dynamic: license-file

# yaptax

*Every language at English price.* (Formerly `langtax`.)

**Measure the language tax in your AI prompts.** The same meaning takes more tokens outside English, and you pay per token. `yaptax` counts your own prompts per language, on the tokenizers your models use, and tells you how many of those tokens English would not need.

Everything runs on your machine. Your text is never uploaded to us.

```
pip install yaptax
yaptax measure --text "मेरा ऑर्डर कहाँ है? मुझे अपने पैसे वापस चाहिए।"   # try it on one sentence
yaptax measure prompts.jsonl                                       # then on your own prompts
```

**New in 0.6: use it inside the Claude app, no API key.** Most people use Claude through the app, not the API, so yaptax now lives there too. Ask Claude "how many tokens is this Hindi prompt?", "slim this prompt" or "which model is cheapest for Tamil?" and it runs yaptax for you. See [Inside the Claude app](#inside-the-claude-app-no-api-key).

**New in 0.5:**
- **Claude, ChatGPT and Gemini, side by side:** `yaptax measure prompts.jsonl --tokenizers o200k,claude,gemini` counts your own text on GPT-4o, Claude and Gemini (Claude and Gemini through their free token-counting APIs, with your keys).
- **`yaptax slim`:** cuts tokens without changing what prompts say. On `examples/app_log.jsonl` (a Hindi support bot whose code builds prompts with `json.dumps`), it cuts 881 tokens to 456 on o200k (48%), almost all from `\uXXXX` escapes.
- **The router talks to Claude natively** (`provider = "anthropic"`), to Gemini through Google's OpenAI-compatible endpoint, and can slim every request (`slim = true`). `yaptax providers --config openai,claude,gemini` writes a starter config.

```
Language                           Records  Label     o200k tokens  o200k extra  cl100k tokens  cl100k extra
---------------------------------  -------  --------  ------------  -----------  -------------  ------------
Hindi (hin_Deva)                         2  labelled            47          36%            142           79%
Spanish (spa_Latn)                       2  labelled            33          24%             38           34%
English (eng_Latn)                       2  labelled            31           0%             31            0%
Tamil (tam_Taml)                         1  labelled            28          50%            114           87%
...

o200k: 207 tokens. The same meaning in English would take about 148, so about 59 (29%) are language tax.
```

That is `examples/support.jsonl`, ten short customer-support messages.

## Commands

| Command | What it does |
|---|---|
| `yaptax measure FILE` | Tokens per language in a file, and how many are language tax |
| `yaptax mcp install` | Use yaptax inside Claude Desktop, no API key (`yaptax mcp config` for other MCP apps) |
| `yaptax skill` | A Claude Skill `.zip` to upload in claude.ai, so yaptax works in the Claude app on any device |
| `yaptax measure --text "..."` | The same for one sentence, no file needed (repeat `--text` for several). Also works with `check` and `slim` |
| `yaptax measure FILE --spend 2000` | Also shows the tax in money for a monthly spend |
| `yaptax measure FILE --lang hi` | Treat every record as one language |
| `yaptax measure FILE --tokenizers o200k,qwen3` | Choose tokenizers (see below) |
| `yaptax measure FILE --json` | Machine-readable output |
| `yaptax check FILE --spend 2000 --out check.md` | Short markdown Language Cost Check: tokens per language, language tax, fewest-token tokenizer per language, rough savings range. First tokenizer in `--tokenizers` is the model you use today |
| `yaptax measure FILE --tokenizers o200k,claude,gemini --sample 50` | Your text on GPT-4o, Claude and Gemini. Claude and Gemini are counted by their providers' token-counting APIs with your keys, 50 records per language by default |
| `yaptax measure FILE --tokenizers o200k,claude --router-ratios` | Also prints `token_ratios` for router.toml, so the router estimates Claude's tokens per language from your own text |
| `yaptax slim FILE --out slim.jsonl` | Cuts wasted tokens (escaped JSON, padding, invisible characters, extra spaces) without changing what prompts say, and flags habits that cost tokens on every request, such as a non-English system prompt |
| `yaptax providers` | Ready-made router entries for OpenAI, Claude, Gemini, DeepSeek, Mistral, OpenRouter and Ollama; `--config openai,claude,gemini --out router.toml` writes a starter config |
| `yaptax telemetry status`, `on` or `off` | Anonymous usage counts, off by default (see below) |
| `yaptax ratios Tamil` | The Index's ratios for one language, on all 8 tokenizers |
| `yaptax demo --open` | The router on a fake provider, with a live savings dashboard (needs `yaptax[router]`) |
| `yaptax router --config router.toml` | The router in front of your real models, with your own keys |
| `yaptax quality --config router.toml prompts.jsonl --judge MODEL --rules rules.toml` | Do the cheaper models answer as well, per language? Writes `allow` rules for the router (needs `yaptax[router]`) |
| `yaptax quality --demo` | The quality report on the fake provider: no keys, nothing paid |

**Input:** `.jsonl`, `.json`, `.csv`, `.tsv`, or plain text with one prompt per line (`-` reads stdin).
- Text is taken from a `text`, `prompt`, `content`, `input`, `message`, `query` or `question` field, or from chat `messages`. Use `--text-field` for another name.
- Language is taken from a `lang`, `language` or `locale` field if there is one (`hi`, `hi-IN`, `hin_Deva` and `Hindi` all work). Use `--lang-field` for another name.
- Without a label, the language is guessed from the writing system. All Devanagari is scored as Hindi, for example. Latin-script text is only scored as English if it looks like English; otherwise it is counted but not scored. Label your data for exact results.

## Inside the Claude app (no API key)

**Claude Desktop** (Windows, macOS): yaptax runs on your computer as an MCP server, so your text stays on your machine.

```
pip install "yaptax[mcp]"
yaptax mcp install
```

Then quit Claude Desktop fully (also from the system tray or menu bar) and open it again. yaptax appears under the tools menu in the chat box. Ask, for example:

- "How many tokens does this cost, and how much is language tax? मेरा ऑर्डर कहाँ है?"
- "Slim this system prompt without changing its meaning: ..."
- "Measure C:\work\prompts.jsonl and tell me which languages cost the most."
- "Which tokenizer needs the fewest tokens for Bengali?"

`yaptax mcp install` adds one entry to Claude Desktop's config and keeps everything else (the old file is saved as `.bak`). The tools: `measure_text`, `measure_file`, `slim_prompt` and `language_ratios`.

**Claude Code:** `claude mcp add yaptax -- python -m yaptax mcp` (after `pip install "yaptax[mcp]"`). **Other MCP apps** (Cursor, VS Code, Windsurf, Gemini CLI, ...): `yaptax mcp config` prints the JSON to paste.

**claude.ai** (web, desktop and mobile, nothing to install on your computer): add yaptax as a Claude Skill.

```
pip install yaptax
yaptax skill            # writes yaptax-skill.zip
```

In Claude, open Settings > Capabilities, turn on code execution, then under Skills choose Upload skill and pick `yaptax-skill.zip`. Claude then runs yaptax in its own sandbox when you ask about tokens or slimming. The zip carries yaptax and the GPT tokenizer files; the sandbox only installs `tiktoken` if it is missing.

What you get in the app: exact counts on the GPT tokenizers (o200k for GPT-4o, GPT-4.1, GPT-5 and o3; cl100k for GPT-4), the language tax from the Index, lossless slimming, and Index ratios for 204 languages. Claude's and Gemini's own tokenizers are not public, so exact Claude or Gemini counts still need your key and the CLI (`--tokenizers o200k,claude,gemini`). Shorter prompts also use less of your Claude plan's usage limits.

## yaptax Router: cut the tax automatically

Measuring is step one. The router is step two: an OpenAI-compatible endpoint that sends each request to the cheapest model you allow for its language, and shows what that saved.

```
pip install "yaptax[router]"
yaptax demo --open
```

`yaptax demo` runs the router against a built-in fake provider, sends 34 multilingual support messages through it and opens a live savings dashboard. Every model gets the same demo price, so the saving you see comes only from tokens. No real model is called and nothing is paid. On the demo set, routing saves 53% against sending every language to Qwen3:

| Language | Routed to | Saved |
|---|---|---|
| Hindi | gpt-4o | 64% |
| Tamil | gpt-4o | 68% |
| Telugu | gpt-4o | 72% |
| Thai | deepseek-v3 | 31% |
| Arabic | mistral-nemo | 24% |
| Amharic, English | qwen3 (baseline) | 0% |

For real traffic, write a config with your models, your prices and your keys (see [`examples/router.toml`](https://github.com/NeuroTitan-Hub/yaptax/blob/main/examples/router.toml)), then:

```
yaptax router --config router.toml
```

```python
from openai import OpenAI
client = OpenAI(base_url="http://127.0.0.1:8787/v1", api_key="unused")
reply = client.chat.completions.create(model="yaptax-auto", messages=[{"role": "user", "content": "मेरा ऑर्डर कहाँ है?"}])
```

How it decides:
- **Language:** the `x-yaptax-lang` header if you send one; otherwise a guess from the user's text.
- **Cost:** for each allowed model, input tokens on its tokenizer times its input price, plus an expected answer length on its tokenizer times its output price. OpenAI's tokenizers are counted exactly; others are estimated from the Language Cost Index. It picks the cheapest. On a tie it stays on your baseline.
- **Pinning:** send a configured model name instead of `yaptax-auto` and the router uses that model, logging it like any other request.
- **Safety:** per-language `allow`/`deny` rules, written for you by `yaptax quality` (below). If the chosen model is down (5xx), it retries on your baseline.
- **Accounting:** the actual cost uses the provider's reported usage. The baseline cost is an estimate of the same request on your baseline, with the answer converted to its tokenizer using the Index ratios.
- **Privacy:** the log keeps language, models, token counts, costs and timing. No prompt or answer text, and keys stay in your environment.
- **Endpoints:** `POST /v1/chat/completions` (streaming supported), `GET /v1/models`, `GET /stats`, and the dashboard at `/`.

What it doesn't do yet: route by latency, or re-check quality on live traffic by itself (run `yaptax quality` again when models or prompts change).

### Quality check: cheaper, but as good?

Fewer tokens only helps if the answers stay good. `yaptax quality` sends the same prompts to your baseline and to each cheaper model the router may use, then checks every candidate, language by language:

```
pip install "yaptax[router]"
yaptax quality prompts.jsonl --config router.toml --judge gpt-4o --rules rules.toml --pairs pairs.jsonl
```

- **Answered:** a real reply, no error. Reasoning models get room to think (`--max-tokens`, default 2048), or turn thinking off the way your app does (`--reasoning-effort none`).
- **Right language:** the reply is in the prompt's writing system, and a Spanish or Hindi question did not get an English reply.
- **Not worse:** a judge model compares the two answers twice, with the order swapped, and only "better or tie" in both orders counts. Results come with 95% intervals, because 20 prompts is a first check, not proof.
- **Cost:** what each answer really cost, from the provider's own token counts and your prices. Candidates that are not cheaper are skipped, so no judge calls are spent on them.

It writes a markdown report and `[languages.xxx] allow = [...]` rules: paste them into `router.toml` and the router only uses models that passed for that language. `pairs.jsonl` holds every prompt with both answers, for you to read; it stays on your machine. Try the report first with `yaptax quality --demo`.

The judge is a model, not a person, and the language check cannot tell Hindi from Marathi. Read some pairs yourself before trusting a pass.

## Free Language Cost Check

Want this number for your own product? We run a free one-week Language Cost Check for teams that serve users outside English.

- **What you get:** tokens per language on the model you use today and three alternatives, how much of your bill is language tax, a rough savings range, and a 20-minute call.
- **What to send:** 20 to 500 real prompts, with names, phone numbers, emails and IDs removed first. Tell us which models you use and roughly what you spend a month.
- **What happens to them:** we measure them on our own machines and delete them after we send you the result. The numbers stay with you; no prompt text is kept.
- **How to ask:** open a GitHub issue titled "Language Cost Check" in this repository. **Do not paste prompts into the issue, it is public.** Tell us your languages and models. We will reply there with how to send the prompts privately.

You can also run the same measurement yourself with `yaptax measure`. Nothing leaves your machine.

## Tokenizers

| ID | Used by | Needs |
|---|---|---|
| `o200k` | GPT-4o, GPT-4.1, GPT-5, o3 | built in |
| `cl100k` | GPT-4, GPT-3.5 | built in |
| `gpt2` | GPT-2 | built in |
| `utf8-bytes` | byte-level models | built in |
| `qwen3`, `deepseek-v3`, `mistral-nemo`, `bloom` | those open models | `pip install 'yaptax[hf]'`; fetched from Hugging Face at the exact revisions the Index used |

| `claude`, `claude:<model id>` | Claude (default `claude-opus-5-5`) | `pip install 'yaptax[claude]'` and `ANTHROPIC_API_KEY`; counted by Anthropic's token-counting endpoint |
| `gemini`, `gemini:<model id>` | Gemini (default `gemini-2.5-flash`) | `GEMINI_API_KEY`; counted by Google's countTokens endpoint |

Anthropic and Google do not publish their tokenizers as files, so `claude` and `gemini` send the text being counted to that provider, with your key, and nowhere else. The framing tokens those endpoints add to each message are measured once and subtracted. They are not in the Language Cost Index, so their "extra" column shows n/a: compare their token counts with the other columns.

## Using Claude and Gemini through the router

```
pip install 'yaptax[router,claude]'
yaptax providers --config openai,claude,gemini --out router.toml   # then add prices
yaptax measure prompts.jsonl --tokenizers o200k,claude,gemini --router-ratios   # paste token_ratios into router.toml
yaptax router --config router.toml
```

Your app keeps calling `http://127.0.0.1:8787/v1/chat/completions` in the OpenAI format. For a model with `provider = "anthropic"` the router calls Claude's Messages API with the official SDK and converts the answer back, streaming and tool calls included. `prompt_cache = true` asks Claude to cache the repeated start of each request. Sampling settings such as `temperature` are dropped for Claude, because current Claude models reject them. Gemini uses Google's OpenAI-compatible endpoint, so it needs no conversion.

## Anonymous usage counts (off by default)

yaptax asks once, in an interactive terminal, whether you will share anonymous usage counts; the default answer is no, and it never asks in CI or scripts. When on, each command sends a random install id, the command name, the yaptax version, the OS family and the Python version. Never your text, file names, token counts or keys. It tells us whether people come back after the first try. `yaptax telemetry off`, `YAPTAX_TELEMETRY=0` or `DO_NOT_TRACK=1` turn it off; `src/yaptax/usage.py` is the whole implementation.

## How "extra tokens" is computed

For each language, yaptax divides your token count by that language's ratio to English in the [Language Cost Index](https://neurotitan.in/cost): the same 1,012 FLORES-200 sentences, measured in 204 languages on 8 tokenizers. The difference is the language tax. It is an average, so a single short text can differ.

**Checked:** on FLORES-200 devtest, `yaptax measure` gives exactly the token totals published with the Index for Hindi, Tamil and English on o200k, cl100k, Qwen3 and BLOOM.

**What it does not tell you:** fewer tokens is not the same as a cheaper or better model. Prices per token differ between models, and so does answer quality in each language. Measuring those on your own traffic is what our Language Cost Audit does: write to the address on [neurotitan.in](https://neurotitan.in).

## Develop

```
pip install -e ".[test]"
pytest
```

MIT licence. The bundled Index data comes from [Sentence-cost-in-Electron](https://github.com/NeuroTitan-Hub/Sentence-cost-in-Electron); FLORES-200 is by Meta AI (CC BY-SA 4.0).

By [NeuroTitan Labs](https://neurotitan.in).
