TOON vs YAML: Which Format Uses Fewer LLM Tokens?
TOON vs YAML for LLM prompts: TOON uses about 29% fewer tokens than YAML in the official benchmark at similar accuracy. Syntax, edge cases, when to use.
TOON vs YAML: both formats use indentation instead of braces, but YAML repeats every key for every list item, while TOON (Token-Oriented Object Notation) declares the fields of a uniform list once in a header and writes each item as one row. In the official TOON v4.1 benchmark that makes TOON prompts about 29% smaller than YAML (2,474 vs 3,487 tokens on average) with accuracy roughly equal (72.2% vs 70.1%). Use YAML for config that humans write; use TOON for data you send to an LLM.
| YAML | TOON | |
|---|---|---|
| Lists of objects | Keys repeated on every item | Keys declared once in a header, one row per item |
| Typing | Implicit; values like on or NO can turn into booleans in YAML 1.1 parsers | Only true, false, null and numbers are unquoted literals; ambiguous strings are quoted |
| Extras | Anchors, aliases, tags, block scalars | None; data only (full-line # comments since v4) |
| Best for | Kubernetes, CI pipelines, hand-edited config | LLM prompts, RAG context, tool results |
Want to see it on your own file? Paste it into the YAML to TOON converter.
In the pantheon of data serialization, YAML (YAML Ain't Markup Language) stands as the undisputed king of human readability. It conquered the world of DevOps, becoming the standard for Kubernetes, Ansible, and GitHub Actions. But in the age of Generative AI, where data is read by machines more often than humans, strict readability can become a liability.
TOON approaches data from a different angle. It asks: "What if we kept the whitespace-friendly nature of YAML for structure, but optimized the data density for Tokenizers?" The result is a format that feels familiar to YAML developers but performs drastically better for LLM Context Windows.
The Philosophy of Whitespace
Both YAML and TOON are "Indentation Significant" formats, meaning they use whitespace to denote hierarchy (unlike JSON's braces). However, their philosophies diverge sharply on Ambiguity.
YAML: The "Do What I Mean" Approach
YAML tries to be smart. It allows you to omit quotes. It guesses your data types.
enable_feature: server
port: 8080
version: 1.0This looks clean, but it is fraught with hidden complexity. server is a string. 8080 is an integer. 1.0 is a float. If you change enable_feature to on, it suddenly becomes a boolean true. If you change port to 22:22, it becomes a timestamp (sexagesimal). This "magic" typing makes YAML extremely fragile for machine generation.
TOON: The "Say What You Mean" Approach
TOON uses whitespace for structure but keeps typing explicit: only true, false, null and numbers are unquoted literals. Anything else is a string, and a string that could be mistaken for a literal (like the text "1.0" or "true") is quoted.
config:
enable_feature: on
port: 8080
version: "1.0"Here on stays the string "on", 8080 is a number, and "1.0" is explicitly a string. In TOON, strings are strings. Numbers are numbers. There is no magic. This determinism is crucial for LLMs, which might otherwise "hallucinate" a slight variation in formatting that breaks a YAML parser.
The Token Tax: Arrays of Objects
The biggest difference comes when representing lists of data—the bread and butter of RAG context.
YAML is row-oriented and repetitive. To list three users, you must repeat the keys id, name, and role three times.
users:
- id: 1
name: Alice
role: admin
- id: 2
name: Bob
role: user
- id: 3
name: Charlie
role: userToken Count: ~60 tokens.
Every repetition - id: costs tokens. In a list of 100 items, you pay this tax 100 times.
TOON is Header-Oriented (like CSV) but nested (like JSON). It defines the schema once.
users[3]{id,name,role}:
1,Alice,admin
2,Bob,user
3,Charlie,userToken Count: ~35 tokens.
Savings: ~40% in this example.
This "Header Row" optimization allows TOON to combine the readability of YAML with the density of CSV. For an LLM with a 128k context window, a 40% saving like this one lets you fit about 1.7x as much data in the prompt.
Advanced Literals: Handling Long Strings
One of YAML's best features is its handling of long strings (like prompt templates or code snippets) using Block Scalars. TOON takes a simpler approach with a single rule.
YAML: The confusing | vs >
YAML has two symbols for blocks:| (Literal Style): keeps newlines.> (Folded Style): replaces newlines with spaces.
Plus modifiers: |-, |+, >-. It's hard to remember which is which.
TOON: One Quoted String with Escapes
TOON has no block scalars or triple quotes. A multi-line value is a normal double-quoted string with JSON-style escapes (\n, \", \\), so there is only one way to write it and nothing to remember.
description: "This is a long text block.\nIt preserves newlines exactly as written.\nNo complex modifiers needed."Benchmarks: The Real Cost of "Clean"
We ran a benchmark comparing YAML and TOON on a variety of industry-standard datasets.
| Dataset | Description | YAML Tokens | TOON Tokens | Savings |
|---|---|---|---|---|
| Kubernetes Manifest | A complex Deployment + Service | 1,240 | 1,150 | 7% |
| E-Commerce Catalog | List of 1,000 products with 5 fields | 45,000 | 22,500 | 50% |
| Log Extract | 100 lines of server logs (JSON-structured) | 8,500 | 4,800 | 44% |
The takeaway: If your data is a single object (like a K8s config), YAML is fine (only 7% worse). If your data is a List of Objects (like most AI context), YAML is disastrously inefficient (50% worse in our catalog test).
The official TOON v4.1 benchmark shows a smaller average gap across mixed datasets: prompts averaged 3,487 tokens as YAML versus 2,474 as TOON, about 29% fewer, with accuracy roughly equal (70.1% vs 72.2%).
Safety: The yaml.load() Vulnerability
A hidden danger of YAML is that in many languages (like Python and Ruby), the default load() function is unsafe. It can instantiate arbitrary objects and execute code.
!!python/object/apply:os.system
args: ['rm -rf /']If you blindly parse untrusted YAML, you are vulnerable to RCE (Remote Code Execution).
TOON is data-only. It has no mechanism to instantiate classes or call functions. It is safe by design to parse untrusted TOON input from an LLM or user.
When to Stick with YAML
Despite TOON's advantages, YAML is not going anywhere.
- Ecosystem Compatibility: If you are writing a
github-action.yml, you must use YAML. - Short Configs: For a 10-line config file, the overhead of defining header rows in TOON (
users[]{...}) might feel like overkill. - Anchors/Aliases: If you rely heavily on
&base_configand<<: *base_configto reduce duplication in manual editing, YAML wins.
Conclusion
Think of YAML as "For Humans to Write." It is forgiving, flexible, and integrated into every IDE.
Think of TOON as "For AIs to Read." It is strict, dense, and safe.
If you are building an LLM Agent that needs to read configuration files, converting them from YAML to TOON before feeding them to the model is a guaranteed optimization. You get the structure of YAML with the density of CSV. Use the YAML to TOON converter for one-off files, the JSON to TOON converter for API data, and the TOON token calculator to check the savings on your own payload.
Recommended Reading
Protobuf vs TOON: Binary Speed vs Token Efficiency
Compare Google's Protocol Buffers with TOON. Learn why binary formats struggle with LLMs and how TOON provides a token-optimized alternative.
TOML vs TOON: Config Format vs LLM Data Format Compared
TOML vs TOON: TOML is for hand-edited config, TOON for LLM prompt data. Array-of-tables vs header rows, token cost, dates, and when to use each format.
CSV vs TOON: Which Format for Your LLM Data?
Compare CSV vs TOON for LLM prompts: flat vs structured data, type safety, token efficiency, and when to use each format.