
TOON (Token‑Oriented Object Notation) is a lightweight, human‑readable serialization format designed to pass structured data into Large Language Models (LLMs) while dramatically reducing token usage compared to JSON.
Instead of repeating field names and punctuation, TOON uses a compact tabular style that’s both efficient and easy to scan.
JSON vs TOON: A Realistic Example
Consider this JSON representation of product orders with shipping details:
{
"orders": [
{
"order_id": 1001,
"product": "Laptop",
"quantity": 2,
"price": 1200.50,
"shipping": {
"address": "123 Main St",
"city": "Melbourne",
"status": "shipped"
}
},
{
"order_id": 1002,
"product": "Headphones",
"quantity": 1,
"price": 199.99,
"shipping": {
"address": "456 High St",
"city": "Sydney",
"status": "processing"
}
}
]
}
Estimated tokens: ~145
The same data expressed in TOON:
orders[2]{order_id,product,quantity,price,shipping}:
1001,Laptop,2,1200.50
shipping{address,city,status}:
123 Main St,Melbourne,shipped
1002,Headphones,1,199.99
shipping{address,city,status}:
456 High St,Sydney,processing
Estimated tokens: ~85
Reduction: ~41% fewer tokens
Cost Impact
Based on GPT‑4 pricing at $30 per million tokens, the savings are clear:
- JSON → 145 tokens ≈ $0.00435
- TOON → 85 tokens ≈ $0.00255
- Savings per request: ~41%
Even with small datasets, TOON delivers meaningful reductions in token usage. At scale, these savings compound into lower API costs and greater context capacity for your LLM applications.
Using TOON in Go
If you’re a Go developer, you can start using TOON right away with the GoTOON package:
$ go get github.com/alpkeskin/gotoon
Encoding to TOON Format
Encoding to TOON format lets you transform structured Go data into a compact, LLM‑friendly representation.
package main
import (
"fmt"
"github.com/alpkeskin/gotoon"
)
func main() {
data := map[string]interface{}{
"orders": []map[string]interface{}{
{"order_id": 1001, "product": "Laptop", "quantity": 2, "price": 1200.50},
{"order_id": 1002, "product": "Headphones", "quantity": 1, "price": 199.99},
},
}
encoded, _ := gotoon.Encode(data)
fmt.Println(encoded)
}
When you run the above code, you get the output as below:
orders[2]{order_id,product,quantity,price}:
1001,Laptop,2,1200.50
1002,Headphones,1,199.99
Decoding Back to Go Structs
Decoding back to Go structs lets you seamlessly reconstruct typed data from compact TOON format.
package main
import (
"fmt"
"github.com/alpkeskin/gotoon"
)
type Order struct {
OrderID int `toon:"order_id"`
Product string `toon:"product"`
Quantity int `toon:"quantity"`
Price float64 `toon:"price"`
}
func main() {
toonData := `
orders[2]{order_id,product,quantity,price}:
1001,Laptop,2,1200.50
1002,Headphones,1,199.99
`
var result struct {
Orders []Order `toon:"orders"`
}
err := gotoon.Decode([]byte(toonData), &result)
if err != nil {
panic(err)
}
fmt.Println(result.Orders)
}
When you run the above code, you get the output as below:
[{1001 Laptop 2 1200.5} {1002 Headphones 1 199.99}]
Customization Options
TOON’s Go encoder also offers handy customization features, letting you adjust output formatting to suit your needs:
WithIndent(4)→ Control indentation Specify spaces for nested structures, useful for YAML‑style readability.WithDelimiter("|")→ Change row separators Replace default comma separators with custom delimiters (e.g.,|or;), ideal for CSV‑like pipelines.WithLengthMarker()→ Explicitly mark array lengths Always include array length markers (e.g.,[2]), improving LLM parsing accuracy.
TOON Use Cases
TOON excels in use cases where structured data must be compact, readable, and optimized for LLMs, including:
- Prompt engineering to deliver cleaner, more efficient inputs
- RAG pipelines that rely on structured context for retrieval
- Analytics dashboards feeding tabular data into models
- E‑commerce and log data where arrays of objects are frequent
Final Thoughts
TOON isn’t just another serialization format—it’s a game‑changer for LLM developers. By cutting token counts and enhancing readability, it empowers you to achieve more with fewer tokens. Whether you’re building structured input pipelines or fine‑tuning prompts, TOON is a powerful addition to your toolkit. And with GoTOON, bringing this efficiency into your Go projects is simple and seamless.

Leave a comment