Skip to content

JSON, YAML, CSV or TOON: what a table costs in tokens

The same data, five ways to write it, counted.

The measurement

A table of 50 customers with six fields each, written as indented JSON, is 3,021 tokens on the OpenAI o200k tokenizer. Here is the same table in other formats.

FormatTokensSavedSame content
JSON, indented3,021–yes
YAML2,41820 %yes
JSON, minified1,97235 %yes
TOON1,31656 %yes
CSV1,21260 %types are lost

Where the saving comes from

Indented JSON spends tokens on three things a model does not need: spaces and line breaks, quotation marks and braces, and the field names repeated on every row. Minifying removes the first. YAML removes most of the second. CSV and TOON remove the third by naming the fields once in a header.

Nested data is different

The header trick only works for rows that share the same fields. For one nested order, 139 tokens as indented JSON, the picture changes: JSON, minified 75, YAML 90, TOON 83.

When not to take the saving

Convert and count your own data in the optimizer.