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.
| Format | Tokens | Saved | Same content |
|---|---|---|---|
| JSON, indented | 3,021 | – | yes |
| YAML | 2,418 | 20 % | yes |
| JSON, minified | 1,972 | 35 % | yes |
| TOON | 1,316 | 56 % | yes |
| CSV | 1,212 | 60 % | 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
- CSV drops types. A model cannot tell the number 1 from the text “1”, or an empty value from a missing one.
- TOON is new. Independent tests found models read it less reliably than JSON on deeply nested data. Try it on your own task before you switch.
- Output is a separate matter. If your code parses the reply, ask for JSON in the answer whatever format you send.