Complete guide to the TOON (Token-Oriented Object Notation) format.
TOON is a compact, human-readable serialization format designed to transmit structured data to Large Language Models (LLMs) with significantly reduced token usage. By eliminating redundant JSON syntax and optimizing data representation, TOON can reduce token usage by 30-60%.
JSON:
{
"user": {
"id": 123,
"name": "Ada"
}
}
TOON:
user id 123 name Ada
JSON:
{
"tags": ["reading", "gaming", "coding"]
}
TOON:
tags [reading gaming coding]
JSON:
{
"users": [
{"id": 1, "name": "Alice"},
{"id": 2, "name": "Bob"}
]
}
TOON:
users [
id 1 name Alice
id 2 name Bob
]
Add a structural index header for validation:
#toon+ {"index":"abc123","length":42}
user id 123 name Ada
Include metadata in the header:
#toon+ {"metadata":{"version":"1.0","encoded":"2025-01-01T00:00:00Z"}}
user id 123 name Ada
Add field-level comments:
#toon+ {"comments":{"id":"User identifier","name":"User name"}}
user id 123 name Ada
Strip all whitespace for pure model use:
import { encodeToon } from '@programsmagic/toon-format';
const toon = encodeToon(data, { minimize: true });
// Output: user id 123 name Ada tags [reading gaming]
import { encodeToon } from '@programsmagic/toon-format';
const data = {
user: {
id: 123,
name: 'Ada',
tags: ['reading', 'gaming'],
active: true
}
};
const toon = encodeToon(data);
console.log(toon);
import { parseToon } from '@programsmagic/toon-format';
const toon = `user id 123 name Ada tags [reading gaming] active true`;
const result = parseToon(toon);
console.log(result.data);
TOON typically provides:
MIT