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.

6 min read • View on GitHub • More from Knackline

A giant heavy stone block carved with a single JSON bracket crushing a tiny number 1, next to a lean vertical pillar holding up massive amounts of data with ease.
Standard JSON serialization forces LLMs to process heavy structural boilerplate before reaching the actual data.
Key Takeaways

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.

Mischa Sigtermans, Laravel News

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.

A split-pane interactive view. On the left

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.

A close-up of a magnifying glass over a piece of parchment. The glass shows a complex tree structure, but the shadow it casts on the paper is a perfectly straight, efficient line of text with dots connecting the nodes.
Complex nested JSON objects are flattened into dot-notation headers, preserving the relationship hierarchy without the nested syntax overhead.

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.

Mischa Sigtermans, Laravel News

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
  name: Alice
- id: 2
  name: Bob
Medium Medium (Repeated keys)
TOON users[2]{id,name}:
1,Alice
2,Bob
Low High (Pure data)
A visual representation of an 8k context window behaving like a container being filled with data blocks. Three interactive buttons labeled JSON

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.

Sagar Sunil Bhedodkar, DEV Community

Sources: