DEV Community

Cover image for Same 1,000 spreadsheet rows, 2.62 X the tokens in pretty JSON
AICostbudget.com
AICostbudget.com

Posted on Edited on Originally published at aicostbudget.com AI-assisted

Same 1,000 spreadsheet rows, 2.62 X the tokens in pretty JSON

A file's size on disk tells you little about how many tokens its rows will take up in a prompt. In one AICostBudget test, the same 1,000 order rows took 35,290 tokens as CSV and 92,289 as pretty-printed JSON. The cells were the same. The difference came from how the rows were written as text.

The same rows in five formats

We generated 1,000 synthetic orders with nine columns, including an order ID, date, city, product, and optional note. We serialized the rows five ways and counted each string with o200k_base using tiktoken==0.14.0.

Format Input tokens Compared with CSV
TSV 34,655 0.98×
CSV 35,290 1.00×
Markdown table 40,316 1.14×
Minified JSON 62,162 1.76×
Pretty-printed JSON 92,289 2.62×

In the JSON array of objects, every row repeats the field names. Removing indentation saves tokens, but the keys and punctuation are still there. A Markdown table writes the header once and adds separators around the cells. How much this matters depends on what's in the cells. When we repeated the test with support tickets containing longer text, pretty JSON used 1.52× the CSV token count.

What to check in your pipeline

Measure the exact text that your application sends for the rows. If you have an .xlsx file, export the relevant cells in the format your application will use. For this tokenizer, you can count a UTF-8 text export with the same package version as the benchmark.

from pathlib import Path

import tiktoken

payload = Path("orders.csv").read_bytes().decode("utf-8")
encoding = tiktoken.get_encoding("o200k_base")
print(len(encoding.encode(payload, disallowed_special=())))
Enter fullscreen mode Exit fullscreen mode

For formats your application can handle, try a representative task and check the answer quality as well as the token count. This test measured only the serialized rows. Instructions, the rest of the request, and the model's output add their own tokens. Exact counts vary with the data, serializer, and tokenizer.

The full CSV and Excel benchmark has the breakdown for order exports, metrics sheets, and support tickets.

AI was used to draft this summary of AICostBudget's benchmark.

Top comments (0)