Skip to main content

Overview

TOON is designed specifically for LLM input. This guide covers strategies, prompting techniques, and validation approaches to get the most out of TOON format when working with Large Language Models.

Why TOON for LLMs?

TOON provides three key advantages for LLM input:

Token Efficiency

~40% fewer tokens than JSON while maintaining lossless representation.

LLM-Friendly Structure

Explicit length declarations [N] and field headers {fields} improve parsing accuracy.

Human Readability

YAML-like indentation and CSV-style tables are familiar and easy to verify.

Basic Integration

Simple Data Encoding

Showing Format Instead of Describing

LLMs understand TOON naturally when they see examples:
Don’t explain TOON syntax in detail. LLMs parse it naturally from examples. The structure is self-documenting.

Optimal Formatting for LLMs

Use Tab Delimiters

Tab delimiters provide the best token efficiency:
Token Comparison:

Enable Key Folding

Collapse nested structures when appropriate:
Only use key folding for LLM input. If you need to decode the output back to JSON, use expandPaths: 'safe' to ensure lossless round-trips.

Prompting Strategies

Code Block Formatting

Always wrap TOON data in code blocks:
Why this works:
  • Models recognize structured data in code blocks
  • Preserves whitespace and formatting
  • Reduces ambiguity in parsing

Explicit Length Declarations

TOON’s [N] syntax helps LLMs validate completeness:
LLMs can validate:
  • “Did I receive all 3 items?”
  • “Is the data complete or truncated?”

Field Headers for Structure

Field headers {id,name,role} act as schema hints:

Requesting TOON Output

When asking LLMs to generate TOON, provide header templates:

Specify Headers Explicitly

Example-Based Generation

When requesting TOON output, show the exact header format you expect. LLMs will match the structure more reliably.

Validation and Parsing

Validating LLM Output

Extracting TOON from Text

Handling Malformed Output

Real-World Integration Examples

RAG (Retrieval-Augmented Generation)

Multi-Turn Conversations

Structured Data Extraction

Batch Processing

Token Cost Optimization

Calculate Token Savings

Cost Estimation

Benchmark Results

From the README benchmarks:
  • Token Efficiency: TOON uses ~40% fewer tokens than JSON on mixed-structure datasets
  • Accuracy: 76.4% accuracy vs JSON’s 75.0% across 209 questions and 4 models
  • Efficiency Score: 27.7 accuracy%/1K tokens (best among all formats tested)
See full benchmark methodology and results in the README.

Best Practices

1

Use tab delimiters

Set delimiter: DELIMITERS.tab for ~5-10% additional token savings.
2

Wrap in code blocks

Always use ```toon code blocks when including TOON in prompts.
3

Show, don't describe

Provide examples instead of explaining TOON syntax.
4

Validate output

Use strict mode to catch malformed LLM responses early.
5

Monitor token usage

Track actual token counts and cost savings in production.

Token efficiency

Tab delimiters + key folding maximize token savings.

Structure validation

Length declarations [N] and field headers {fields} improve LLM accuracy.

Round-trip safety

Use expandPaths: 'safe' when decoding if encoded with keyFolding: 'safe'.

Format familiarity

CSV-like tables and YAML-like indentation are naturally understood.

When to Use TOON

✓ Uniform arrays

Large arrays of objects with consistent fields (tabular data).

✓ Token-sensitive apps

Applications where token costs matter (RAG, long contexts).

✓ Structured output

When requesting structured data from LLMs.

✗ Deeply nested

Highly nested structures may be better as compact JSON.

Next Steps

Encoding Guide

Learn all encoding options and transformations

CLI Usage

Use command-line tools for quick conversions

Streaming

Process large datasets efficiently