AI/TLDR

TOON

A compact, lossless encoding of JSON built for prompt tokens

Context Compression & Token BudgetsOpen source
Language
TypeScript
License
MIT

Overview

Token-Oriented Object Notation (TOON) is a compact, human-readable encoding of the JSON data model, designed for what you put into an LLM prompt. It borrows YAML's indentation for nested objects and CSV's tabular layout for uniform data: a header such as `forecast[3]{day,temp{min,max},condition,rainChance}:` declares the row count and the field list once, then each element is a single comma-separated row. The project frames it as a translation layer — keep using JSON in your code, encode to TOON at the boundary where the data becomes prompt text.

The format has four forms, picked automatically from the shape of the data: inline form for primitive arrays, tabular form for uniform object arrays, keyed tabular for maps of uniform objects (config maps, feature flags, records by ID), and list form as the fallback for mixed or non-uniform data. Round-trips are deterministic and lossless, and the declared lengths and field lists double as guardrails — truncated or malformed model output does not silently pass.

The repository's own benchmarks measure 244 retrieval questions across four models. On the mixed-structure track TOON scores 72.2% accuracy on 2,474 tokens against JSON's 71.4% on 4,308 — the same comprehension for 42.6% fewer tokens — and it ranks first on accuracy per 1,000 tokens. The README is equally clear about where TOON loses: deeply nested or non-uniform data, where compact JSON often wins outright; purely tabular data, where CSV is smaller; and latency-bound deployments, where compact JSON can be processed faster. The reference implementation is TypeScript on npm as `@toon-format/toon` with a separate CLI, MIT licensed, with a versioned spec and community ports in other languages.

What it does

  • Lossless, deterministic round-trip with the JSON data model — objects, arrays and primitives
  • Four automatic forms: inline, tabular, keyed tabular and list, with nested field groups folded into headers
  • Declared `[N]` row counts and `{fields}` lists that catch truncated or malformed model output
  • Streaming encode and decode (`encodeLines`, `decodeFromLines`, `decodeStream`) for large datasets
  • A CLI that converts either direction, reads stdin, and prints token savings with `--stats`
  • A published spec with a shared conformance test suite, plus editor support and community ports

Getting started

Try it on your own data with the CLI before installing anything, then encode at the point where data becomes prompt text.

See what it saves on your data

`--stats` prints the TOON alongside estimated JSON and TOON token counts.

bashbash
cat data.json | npx @toon-format/cli --stats

Install the library

The reference implementation is published on npm; the CLI is a separate package if you want it installed globally.

bashbash
npm install @toon-format/toon
npm install -g @toon-format/cli

Encode before prompting

Keep JSON in your program and convert at the boundary.

tsts
import { encode } from '@toon-format/toon'

const data = {
  users: [
    { id: 1, name: 'Ada', role: 'admin' },
    { id: 2, name: 'Bob', role: 'user' }
  ]
}

console.log(encode(data))
// users[2]{id,name,role}:
//   1,Ada,admin
//   2,Bob,user

Stream large payloads

`encodeLines` yields one line at a time instead of building the whole string in memory.

tsts
import { encodeLines } from '@toon-format/toon'

for (const line of encodeLines(largeData)) {
  process.stdout.write(`${line}\n`)
}

Commands and code are distilled from the project's own documentation — always check the official repo for the latest.

When to use it

  • Cut prompt cost when you feed a model tables of uniform records — orders, logs, rows from a database
  • Give a model structural guardrails: a declared row count and field list it can check its reading against
  • Shrink MCP or tool responses before they land in an agent's context window
  • Compare formats honestly — the repo's benchmarks show where JSON or CSV is still the better choice

How TOON compares

TOON alongside other open-source context compression & token budgets tools AI/TLDR tracks, ranked by GitHub stars.

ToolStarsWhat it does
RTK★ 80.9kA single-binary Rust CLI proxy that filters, groups, truncates and dedupes the output of 100+ dev commands before a coding agent reads it, cutting token use by 60–90%.
Headroom★ 72.9kLocal context-compression layer that shrinks tool outputs, logs, files and RAG chunks before they reach the model, usable as a library, a drop-in proxy or an MCP server.
TOON★ 25.4kA compact, lossless encoding of JSON built for prompt tokens
Context Mode★ 23.4kAn MCP server that keeps raw tool output out of the context window by sandboxing it, and indexes session events into SQLite FTS5 so an agent survives compaction.
pxpipe★ 7.4kA local proxy that renders bulky request context — system prompt, tool docs and older turns — as PNG pages the model reads with vision, cutting the input tokens a coding agent re-sends each turn.