The Cost of Curly Braces: How laravel-toon Reclaims the Context Window
Stop wasting 40% of your AI budget on JSON syntax. This Laravel package compresses data into a "tabular YAML" that LLMs actually prefer to read.
- Token-Optimized Object Notation (TOON) reduces API costs by replacing repetitive JSON keys with a single header row.
- The laravel-toon package uses dot-notation to flatten complex Eloquent relationships into a two-dimensional format.
- Developers can pack up to three times as much data into an LLM context window compared to standard JSON serialization.
- The package provides a reversible bridge that converts Laravel collections to TOON and decodes AI responses back into PHP arrays.
The Repetition Tax
We are paying a bracket tax to artificial intelligence. Every time a Laravel application sends an array of records to an OpenAI or Anthropic endpoint, it wraps the data in JSON. That means repeating the string "first_name" and "created_at" for every single row.
This structural boilerplate consumes valuable context window space. You pay for the syntax, not just the signal. Large payloads hit token limits faster and cost significantly more to process.
When you're building MCP servers or sending data to LLMs, every token counts. JSON is verbose. Repeated keys, curly braces, quotes, colons. For a 50-record response, you're burning thousands of tokens on structure alone.
Enter the Tabular Object
The solution is a format called Token-Optimized Object Notation (TOON). It treats the LLM context window as highly constrained real estate. Instead of repeating keys, TOON extracts them into a single header row.
An array of user objects becomes a clean, comma-separated list living under a defined schema. It removes the quotes, the braces, and the redundant labels. The result is a payload that looks like a hybrid between YAML and a CSV file.
Language models are surprisingly adept at reading tabular data. By switching formats, developers can pack three times as many records into the exact same prompt without confusing the model.
Flattening the Hierarchy
Most token-saving formats fail when data becomes complex. A simple list of strings is easy to compress. A nested Eloquent model with relationships is much harder. Previous attempts at TOON parsers kept the nesting intact, which missed the biggest optimization opportunity.
The laravel-toon package solves this through flattening. It uses dot-notation to represent three-dimensional relationships in a two-dimensional table.
If a user record contains a nested profile object, the TOON header simply becomes user.profile.bio. The LLM understands the hierarchy perfectly, but the payload remains entirely flat. This architectural choice is where the most dramatic token savings occur.
I looked at existing TOON packages for Laravel, but none handled nested objects properly. They'd keep the nesting intact, missing the biggest optimization opportunity.
The Laravel Bridge
The package integrates seamlessly into the Laravel ecosystem. It ships with a Service Provider and a Facade that make conversion a one-line operation. You do not need to manually traverse collections or write custom serialization logic.
Developers simply pass an Eloquent collection or an associative array to the Facade. The package handles the recursive parsing, state management, and indentation required to build the TOON string.
use Knackline\Toon\Facades\Toon;
$users = User::with('profile')->limit(50)->get();
// Instead of $users->toJson()
$promptData = Toon::fromJson($users->toArray());
It is also fully reversible. When the AI responds with TOON formatting, the same Facade can decode it back into a standard PHP array. This creates a closed-loop system for AI agents interacting with your database.
The Token Weigh-in
The true value of this package becomes obvious when you measure the payloads. We compared a standard dataset of user records across three formats. JSON prioritizes machine parsing. YAML prioritizes human readability. TOON prioritizes token density.
| Format | Raw Text Example | Token Count (Est.) | Signal-to-Noise |
|---|---|---|---|
| JSON | [{"id": 1, "name": "Alice"}, {"id": 2, "name": "Bob"}] |
High | Low (Heavy syntax) |
| YAML | - id: 1 |
Medium | Medium (Repeated keys) |
| TOON | users[2]{id,name}: |
Low | High (Pure data) |
Optimization is not just about lowering your API bill. It is about expanding your application's cognitive capacity. By stripping away the brackets and repeated keys, you free up the context window for what actually matters: more data, deeper context, and better instructions.
Compress your prompts, not your ideas.
Sources: