JSON Token Counter for OpenAI, Claude & Gemini

Paste JSON to estimate how many tokens it costs in GPT, Claude and Gemini prompts. You also see how much of a context window it fills and which changes would save the most tokens: minifying, dropping nulls, shorter keys or a CSV-style table. Everything runs in your browser and nothing is uploaded.

Ways to save tokens

How your JSON splits into tokens

Each coloured piece is one pre-token from a GPT-style splitter, shown for the first 2,000 characters. Most pieces are one token. Pieces underlined in purple are long or unusual and probably cost two or more. Whitespace is shown as · (space), → (tab) and ↵ (line break).

These are estimates. NeatJSON does not ship the real tokenizer vocabularies. It uses a heuristic calibrated against OpenAI's o200k_base tokenizer, which is usually within about 10% for JSON and English text. Claude and Gemini figures are rougher ratios. For exact counts, use OpenAI's tiktoken, Anthropic's token counting endpoint or Gemini's countTokens (code below).

How to count JSON tokens

  1. Paste or open your JSON. It can be an API response, a tool result, a few-shot example or a whole dataset. If it isn't strictly valid (trailing commas, single quotes, comments, a truncated end), the counter repairs it with the NeatJSON parser and tells you what it fixed. If it can't be parsed at all, the raw text is still counted.
  2. Read the table. You get characters, UTF-8 bytes, lines and estimated tokens for four tokenizer families, both as pasted and minified. The Savings column shows what minifying alone would save.
  3. Pick a context window. Choose 8K, 32K, 128K, 200K or 1M, or type any size. Check your model's documentation for its actual limit. The bars show how much of the window the JSON takes up, before and after minifying.
  4. Try the savings ideas. Each card shows the token estimate for a cheaper version of the same data. Apply replaces the input so you can keep stacking changes, Undo steps back, and Copy puts the result on your clipboard.

Why JSON is expensive in tokens

LLM tokenizers are trained mostly on prose and code, so ordinary English comes out at roughly four characters per token. JSON is noisier. Here is how OpenAI's o200k_base tokenizer splits the start of a pretty-printed record:

[\n | ␣ | ␣{\n | ␣␣␣ | ␣" | id | ": | ␣ | 0 | ,\n | ␣␣␣ | ␣" | uuid | ": | ␣" | 651 | 327 | 0 | e | - | 269 | e | ...

Token savings techniques, measured

We measured each technique on the same 20-record customer array: ids, UUIDs, names, emails, timestamps, a nested address, tags and some null notes. The figures are real o200k_base counts from tiktoken, with this page's estimate alongside.

FormatCharacterso200k tokens (tiktoken)This page's estimateSaving vs pretty
Pretty-printed, 2-space indent8,8513,1243,142–
Pretty-printed, 4-space indent10,6113,1243,1420%
Pretty-printed, tab indent7,9713,4233,34210% more
Minified6,4302,1332,20832%
Minified, null fields dropped6,3262,1012,16833%
Minified, repeated keys aliased (with legend)5,5192,1492,22631%
Columnar JSON: {"columns":[…],"rows":[[…]]}4,8361,7431,76244%
CSV (nested address flattened)4,1291,6441,56647%

What the numbers say:

Exact token counts in code

The estimates here are for quick checks while you shape a prompt. To get exact counts for billing or hard limits, use each provider's own tokenizer or API.

OpenAI models: tiktoken (Python)

import tiktoken                       # pip install tiktoken

text = open("payload.json", encoding="utf-8").read()
enc = tiktoken.get_encoding("o200k_base")   # GPT-4o-era models; "cl100k_base" for GPT-4 / GPT-3.5
print(len(enc.encode(text)))

# or let tiktoken pick the encoding for a model name it knows:
# enc = tiktoken.encoding_for_model("gpt-4o")

Chat requests add a few tokens per message for role and formatting, so a full request costs a little more than the raw JSON.

Anthropic Claude: the token counting endpoint

import anthropic                      # pip install anthropic

client = anthropic.Anthropic()        # reads ANTHROPIC_API_KEY
text = open("payload.json", encoding="utf-8").read()
resp = client.messages.count_tokens(
    model="claude-opus-5-5",          # use the model you will actually call
    messages=[{"role": "user", "content": text}],
)
print(resp.input_tokens)

The endpoint is POST /v1/messages/count_tokens, and it accepts the same system prompt, tools and messages as a real request. Anthropic's tokenizer is not public, and counts vary between Claude model generations. Don't use tiktoken for Claude, because it undercounts.

Google Gemini: countTokens

from google import genai              # pip install google-genai

client = genai.Client()               # reads GEMINI_API_KEY
text = open("payload.json", encoding="utf-8").read()
resp = client.models.count_tokens(model="YOUR_GEMINI_MODEL", contents=text)
print(resp.total_tokens)

Context windows and output room

A context window is the total number of tokens a model can handle in one request. That covers the system prompt, tool definitions, conversation history, your JSON and the model's reply. If the JSON alone fills 90% of the window, there is little room left for the answer. Model limits change often and vary by tier and provider, so this page doesn't hard-code them. Pick the nearest common size or type the exact figure from your provider's documentation.

If the JSON is too big, cutting tokens isn't the only fix. You can send only the fields the task needs, for example with a JSONPath filter in the JSON viewer. You can split a large array into batches. You can describe the shape with a schema or TypeScript types instead of pasting thousands of sample rows. And when a model sends JSON back with a stray comma or a cut-off ending, the JSON validator pinpoints the error and the main formatter can repair it.

Frequently asked questions

How accurate is this JSON token counter?

It is an estimate. The page splits text the way GPT-style tokenizers do and then estimates the cost of each piece, without downloading the real vocabularies. Tested against OpenAI's o200k_base tokenizer on JSON, English and mixed samples, it is usually within about 10%, and closer on typical API JSON. Random strings such as base64 and hashes are the least accurate. For exact numbers use tiktoken, Anthropic's token counting endpoint or Gemini's countTokens.

Why are the Claude and Gemini numbers different from GPT?

Every model family uses its own tokenizer, so the same JSON produces a different token count in each. Anthropic and Google do not publish theirs as downloadable vocabularies, so this page applies rough ratios to the OpenAI-style estimate: about 1.2 times for Claude and 1.05 times for Gemini. Treat those two columns as ballpark figures and check with the provider's counting API before relying on them.

Does minifying JSON reduce tokens?

Yes, usually by a quarter to a third for pretty-printed JSON. In our 20-record test, minifying cut o200k tokens from 3,124 to 2,133. Changing 4-space indentation to 2 spaces saved nothing, because a run of spaces is a single token. Tab indentation cost more than spaces. Minified JSON is still valid JSON, and models read it without trouble.

Is CSV or JSON cheaper to send to an LLM?

For flat arrays of records, CSV or a columnar JSON layout is clearly cheaper, because the key names are written once instead of in every record. In our test, CSV used about 47% fewer tokens than pretty JSON and 23% fewer than minified JSON. CSV loses types, nulls and nesting, so for nested data a columnar JSON layout or plain minified JSON is often the better trade-off.

Should I shorten JSON keys to save tokens?

Only when the keys are long or unusual. Common English keys like name, email or createdAt are already one or two tokens, so a one-letter alias plus a legend saves little and makes the data harder for the model to interpret. Long snake_case keys repeated thousands of times are worth shortening, and the Shorten repeated keys card shows whether it pays off for your data.

How do I count tokens exactly?

For OpenAI models, use the tiktoken library with the o200k_base or cl100k_base encoding. For Claude, call Anthropic's token counting endpoint (messages.count_tokens in the SDK) with the model you plan to use. For Gemini, call countTokens (models.count_tokens in the Google Gen AI SDK). The code samples on this page show each one.

Is my JSON sent to OpenAI, Anthropic or Google?

No. Counting, minifying, the savings ideas and the token view all run in JavaScript in your browser. The page makes no API calls with your data, so it is safe for private payloads and works offline once loaded.