Back
41/65
Lesson 41
5 min
TRU: JSON to TOON
Encode structured data as TOON so it costs fewer tokens in LLM requests
Ready to level up?
Mark complete to track your learning progress.
Encode structured data as TOON so it costs fewer tokens in LLM requests
Mark complete to track your learning progress.
Hand agents structured data as TOON instead of JSON so the same records cost fewer tokens.
TRU (toon_rust) is a fast Rust implementation of TOON, Token-Optimized Object Notation: a data format that carries the same information as JSON with far fewer of the quotes, braces, brackets and commas that LLM tokenizers charge you for. The toon binary encodes JSON to TOON and decodes TOON back to JSON.
It is a serialization format, not a source-code minifier. Feed it API responses, search results, config files, session exports and other structured data. Uniform arrays of objects benefit most: TOON writes them as one header row plus one CSV-like line per record, which is where the 40-60% token savings come from.
Tabular data shrinks 40-60%
Encode and decode, spec-first
--stats shows before/after
Native binary, streaming decode
Pick a JSON payload, encode it, and watch the token estimate drop. Outputs and counts are real toon --stats results.
{"users": [{ "id": 1, "name": "Alice", "role": "admin", "active": true },{ "id": 2, "name": "Bob", "role": "user", "active": false },{ "id": 3, "name": "Carol", "role": "user", "active": true },{ "id": 4, "name": "Dave", "role": "viewer", "active": true }]}
Point toon at a file. A .json input is encoded, a .toon input is decoded, and stdin is encoded unless you pass --decode.
# Encode JSON to TOON (stdout)toon users.json# Decode TOON back to JSONtoon users.toon# See the token estimate for the conversiontoon --stats users.json# Token estimates: ~55 (JSON) → ~24 (TOON)# Saved ~31 tokens (-56.4%)# Pipe from a command that emits JSONgh issue list --json number,title,state | toon --encode
--stats before you commit to TOON for a given payload. Flat records and tables save the most; deeply nested objects with unique keys save the least.TOON keeps JSON's data model and drops its punctuation. Objects become indented key-value lines, arrays declare their length up front, and arrays of uniform objects become a header row followed by one row per record.
# JSON (55 tokens){"users": [{ "id": 1, "name": "Alice", "role": "admin", "active": true },{ "id": 2, "name": "Bob", "role": "user", "active": false },{ "id": 3, "name": "Carol", "role": "user", "active": true },{ "id": 4, "name": "Dave", "role": "viewer", "active": true }]}# TOON (24 tokens)users[4]{id,name,role,active}:1,Alice,admin,true2,Bob,user,false3,Carol,user,true4,Dave,viewer,true
Anything in the flywheel that emits JSON can hand it to toon before it lands in an agent's context.
Encode cass search --json results before quoting them to an agent
UBS can emit its findings as TOON directly (it uses toon under the hood)
acfs doctor --toon prints the health report in TOON for agents
Pipe issue lists, PR metadata and CI payloads through toon in scripts