TOON with Go: Encode and Decode Data for LLMs

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:

Estimated tokens: ~145

The same data expressed in TOON:

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:

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:

Decoding Back to Go Structs

Decoding back to Go structs lets you seamlessly reconstruct typed data from compact TOON format.

When you run the above code, you get the output as below:

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.

References

Leave a comment