Engineering Guides5 min readUpdated: 2026-11-24

Offline LLM BPE Token Counting & Prompt Cost Optimization Guide

Count exact o200k_base and cl100k_base BPE tokens offline, visualize token boundary segments, prune null JSON fields, and cut prompt API costs locally.

Quick Answer

Use an offline BPE token counter to budget LLM prompt cost before making paid API calls. DataTo lazy-loads gpt-tokenizer inside local Web Workers for exact o200k_base vs cl100k_base tokenizer counts (plus calibrated Claude and Gemini estimates), lets you visualize BPE token segments locally, and pairs with JSON Token Optimizer to strip null fields from JSON and slash LLM tokens without leaving valid JSON.

How o200k_base vs. cl100k_base BPE tokenization works offline

Byte-Pair Encoding (BPE) merges frequent character sequences into subword IDs, which is why punctuation, repeated JSON field keys, and leading spaces affect prompt bills far differently than plain word counts. Comparing o200k_base vs cl100k_base tokenizers shows how GPT-4o / o3's 200k vocabulary compresses JSON syntax and multilingual text tighter than GPT-4 / 3.5's 100k vocabulary.

In DataTo LLM Token Counter, gpt-tokenizer computes exact token counts locally for o200k_base and cl100k_base with zero network calls, while reporting transparent, clearly labeled estimates for Claude 4 / Sonnet (~1.07x o200k) and Gemini 2.5 / 3 (~1.03x o200k) alongside context-window usage percentages and estimated input pricing per 1M tokens.

Model ProfileEncoding / MethodContext WindowEst. Input Rate / 1M
GPT-4o / o3o200k_base (exact BPE)128,000 tokens$2.50
GPT-4 / 3.5cl100k_base (exact BPE)128,000 tokens$10.00
Claude 4 / Sonnetclaude-bpe (est. ~1.07x o200k)200,000 tokens$3.00
Gemini 2.5 / 3sentencepiece (est. ~1.03x o200k)1,000,000 tokens$1.25

Count exact BPE tokens offline →

3-stage prompt reduction: Null Pruning, TOON, and Token Segments

Start with JSON Token Optimizer when downstream tools still require valid RFC 8259 JSON: enabling stripNulls, stripEmptyArrays, and minify deletes bloated null keys, empty [] arrays, and indentation whitespace cleanly.

For read-only RAG records or prompt tables, convert JSON, CSV, or YAML into TOON to fold repeated object keys into a single header, then enable compareToon and showTokenSegments in LLM Token Counter to inspect decoded BPE token boundaries (up to 5,000 segments) and exact JSON-vs-TOON savings side by side.

// Before (formatted with null/empty clutter)
{
  "requestId": "req_901",
  "debugTrace": null,
  "warnings": [],
  "items": [{ "sku": "PRO-01", "qty": 2, "notes": null, "tags": [] }]
}

// After JSON Token Optimizer (valid minified JSON)
{"requestId":"req_901","items":[{"sku":"PRO-01","qty":2}]}

Prune nulls & minify JSON locally →

Worker-isolated BPE tables with zero prompt exposure

Pasting unpublished system prompts, internal RAG contexts, or fine-tuning datasets into hosted web tokenizers exposes proprietary copy to third-party servers. DataTo isolates large BPE rank tables inside lazy Web Workers (keeping landing pages lightweight and running in Airplane Mode) and pairs directly with JSONL Validator to flag any dataset line exceeding maxTokensPerLine.

Convert JSON to TOON (-58% tokens) →

Tools used in this guide

LLM & TOON →

Related formats

Related Comparisons

Frequently asked questions

Are GPT-4o and GPT-4 token counts exact in DataTo LLM Token Counter?

Yes. DataTo uses gpt-tokenizer with official o200k_base and cl100k_base vocabularies locally in Web Workers, while Claude and Gemini values are explicitly labeled as calibrated estimates.

When should I use JSON Token Optimizer instead of JSON to TOON?

Use JSON Token Optimizer whenever downstream code or strict schemas still require valid JSON; use JSON to TOON when maximizing prompt token savings on uniform arrays fed directly to an LLM.

Does LLM Token Counter crash on special strings like <|endoftext|> in datasets?

No. DataTo configures disallowedSpecial: new Set() so literal special-token strings inside prompts or fine-tuning corpora count safely without throwing errors.

Authoritative References

More engineering guides