Skip to content
Sagar Sunil Bhedodkar edited this page Nov 8, 2025 · 1 revision

🧠 TOON for Laravel Wiki

Welcome to the TOON for Laravel Wiki — your complete guide to Token-Optimized Object Notation, a compact, human-readable, and token-efficient format for AI prompts, LLM contexts, and structured data optimization in Laravel.


📌 Overview

TOON for Laravel (Token-Optimized Object Notation) is a Laravel-native AI data optimization library designed to transform large JSON or PHP arrays into a compact, readable, and token-efficient format.

It is ideal for developers working with ChatGPT, Gemini, Claude, Mistral, OpenAI, and other AI/LLM platforms, offering:

  • Token & Cost Reduction – Save up to 70% on API tokens.
  • Readable Structure – Easily human-readable YAML-like format.
  • Better AI Context Understanding – Structured data improves AI response quality.
  • Reversibility – Convert back to JSON seamlessly.

💬 “Compress your prompts, not your ideas.”


🚀 Key Features

Feature Description
🔁 Bidirectional Conversion Convert JSON ⇄ TOON effortlessly
🧩 Human-Readable & Compact YAML-like structure, perfect for developers
💰 Token-Efficient Save up to 70% tokens for AI prompts
⚙️ Laravel Integration Facades, Service Providers, and Artisan commands included
🔒 Deterministic Output Maintains key order for consistent results
📊 Built-in Analytics Measure token usage, byte size, and compression efficiency
🌍 AI & LLM Ready Optimized for ChatGPT, Gemini, Claude, Mistral, and more

🧪 Real-World Benchmark

Metric JSON TOON Reduction
Size (bytes) 7,718 2,538 67% smaller
Tokens (est.) 1,930 640 ~67% fewer tokens

Visual Comparison:

JSON (7.7 KB) ██████████████████████████████████████████████████████████████████████████

TOON (2.5 KB) █████████████████

💡 TOON reduces token load by 60–75%, improving AI efficiency, reducing cost, and maintaining high-quality responses.


⚙️ Installation

Install via Composer:

composer require sbsaga/toon

Laravel auto-discovers the service provider and facade.


⚙️ Configuration

Publish the config file:

php artisan vendor:publish --provider="Sbsaga\Toon\ToonServiceProvider" --tag=config

config/toon.php:

return [
    'enabled' => true,
    'escape_style' => 'backslash',
    'min_rows_to_tabular' => 2,
    'max_preview_items' => 200,
];

🧠 Usage Examples

JSON → TOON

use Sbsaga\Toon\Facades\Toon;

$data = [
    'user' => 'Sagar',
    'message' => 'Hello, how are you?',
    'tasks' => [
        ['id' => 1, 'done' => false],
        ['id' => 2, 'done' => true],
    ],
];

$converted = Toon::convert($data);
echo $converted;

Output:

user: Sagar
message: Hello\, how are you?
tasks:
  items[2]{done,id}:
    false,1
    true,2

TOON → JSON

$toon = <<<TOON
user: Sagar
tasks:
  items[2]{id,done}:
    1,false
    2,true
TOON;

$json = Toon::decode($toon);
print_r($json);

Estimate Tokens

$stats = Toon::estimateTokens($converted);
print_r($stats);

Sample Output:

{
  "words": 20,
  "chars": 182,
  "tokens_estimate": 19
}

🧪 Quick Benchmark Route

use Illuminate\Support\Facades\Route;
use Sbsaga\Toon\Facades\Toon;

Route::get('/toon-benchmark', function () {
    $json = json_decode(file_get_contents(storage_path('app/users.json')), true);
    $jsonEncoded = json_encode($json, JSON_PRETTY_PRINT);
    $toonEncoded = Toon::convert($json);

    return response()->json([
        'json_size_bytes' => strlen($jsonEncoded),
        'toon_size_bytes' => strlen($toonEncoded),
        'saving_percent' => round(100 - (strlen($toonEncoded) / strlen($jsonEncoded) * 100), 2),
        'json_content' => $jsonEncoded,
        'toon_content' => $toonEncoded,
    ]);
});

📊 Analytics & Visualization

Metric Description Example
json_size_bytes Original JSON size 7,718
toon_size_bytes Optimized TOON size 2,538
saving_percent Space saved 67%
tokens_estimate Estimated token count 640
compression_ratio Ratio (TOON/JSON) 0.33

Efficiency Graph:

JSON: ██████████████████████████████████████████████████████████ 100% TOON: ████████████ 33%


🧰 Artisan Commands

php artisan toon:convert storage/test.json
php artisan toon:convert storage/test.toon --decode --pretty
php artisan toon:convert storage/test.json --output=storage/result.toon

🧩 Integration Use Cases

Use Case Benefit
🤖 AI Prompt Engineering Compress structured data for LLMs
📉 Token Optimization Reduce token usage and API costs
🧠 Data Preprocessing Streamline complex inputs
🧾 Logging & Debugging Easier to read than JSON
🔍 Developer Tools Perfect for previews and dashboards

🧰 Compatibility

Laravel PHP Package Version
9.x – 12.x ≥ 8.1 v1.1.0+

🌐 SEO & AI Integration Keywords

laravel ai, chatgpt laravel, gemini laravel, laravel mistral, laravel openai, llm laravel, ai laravel package, prompt compression, token optimizer, compact notation, laravel data compressor, laravel ai integration, sbsaga toon, laravel-toon, toon php, openai cost optimizer, laravel ai efficiency, chatgpt laravel toolkit.


💡 Contribution

Contributions welcome!

  1. Fork the repo
  2. Create a feature branch
  3. Commit & push improvements
  4. Submit a Pull Request 🎉

📜 License

Licensed under the MIT License — free for commercial and personal use.


🧠 “Compress your prompts, not your ideas.” — TOON helps you talk to AI efficiently.