DEV Community

Cover image for Introducing TOON for Laravel β€” A Smarter, Token-Efficient Way to Talk to AI
Sagar Sunil Bhedodkar
Sagar Sunil Bhedodkar

Posted on

Introducing TOON for Laravel β€” A Smarter, Token-Efficient Way to Talk to AI

🧠 TOON for Laravel β€” Compress your prompts, not your ideas

πŸ“ License: MIT Β Β 

⚑ Laravel: 9+   

🐘 PHP: 8.1+


✨ Why TOON?

When building AI-powered Laravel apps, developers often struggle with massive JSON data, expensive token usage, and hard-to-read prompts. That’s where TOON comes in β€” a Laravel-native data compressor designed to make your AI payloads smaller, cleaner, and easier to understand. πŸ’‘

"Compress your prompts, not your ideas."

TOON transforms verbose JSON or PHP arrays into a compact, human-readable format that reduces token cost by up to 70%, while preserving every bit of data.


πŸš€ Quick Overview

Feature Description
πŸ” Bidirectional Conversion Convert JSON ⇄ TOON effortlessly
🧩 Readable & Compact YAML-like syntax with improved readability
πŸ’° Token Efficient Save up to 70% of AI token costs
βš™οΈ Laravel Native Seamlessly integrates with facades, commands & service providers
πŸ“Š Built-in Analytics Analyze compression & token estimates
🧠 AI Ready Works perfectly with ChatGPT, Gemini, Claude & OpenAI APIs

🧠 What Makes TOON Different?

Unlike typical compression tools, TOON focuses on AI efficiency rather than binary storage. It’s designed for prompt engineering, where readability still matters. You can send structured data to your LLMs while keeping payloads light.

  • βœ… Human-friendly like YAML, but optimized for tokenization.
  • βœ… Built for Laravel developers β€” not just another JSON formatter.
  • βœ… 100% reversible β€” decode TOON back into JSON with no loss.

πŸ’» Installation & Setup

composer require sbsaga/toon
Enter fullscreen mode Exit fullscreen mode

Once installed, TOON auto-registers its service provider and facade. Optionally, you can publish its config:

php artisan vendor:publish --provider="Sbsaga\\Toon\\ToonServiceProvider" --tag=config
Enter fullscreen mode Exit fullscreen mode

Config File: config/toon.php

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

🧩 Convert JSON β†’ TOON

use Sbsaga\Toon\Facades\Toon;

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

echo Toon::convert($data);
Enter fullscreen mode Exit fullscreen mode

Output:

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

πŸ”„ Convert TOON β†’ JSON

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

print_r(Toon::decode($toon));
Enter fullscreen mode Exit fullscreen mode

πŸ“Š Real-World Benchmark

Metric JSON TOON Reduction
Size (bytes) 7,718 2,538 πŸ”» 67.12% smaller
Tokens (est.) 1,930 640 πŸ’Έ ~66.8% fewer tokens

πŸ’‘ Less data = fewer tokens = lower API bills. Simple math, massive savings.

JSON (7.7 KB)
β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ

TOON (2.5 KB)
β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ
Enter fullscreen mode Exit fullscreen mode

πŸ§ͺ Quick Laravel 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);
    $toon = Toon::convert($json);

    return response()->json([
        'json_size' => strlen(json_encode($json)),
        'toon_size' => strlen($toon),
        'saving_percent' => round(100 - (strlen($toon) / strlen(json_encode($json)) * 100), 2),
        'toon_preview' => $toon,
    ]);
});
Enter fullscreen mode Exit fullscreen mode

🧰 Artisan Commands

php artisan toon:convert storage/data.json
php artisan toon:convert storage/data.toon --decode
php artisan toon:convert storage/data.json --output=storage/result.toon
Enter fullscreen mode Exit fullscreen mode

🌍 Real Use Cases

Use Case Benefit
πŸ€– AI Prompt Engineering Compress structured inputs for ChatGPT & Gemini
πŸ’¬ LLM Context Management Fit more data within token limits
🧾 Readable Logging Store structured data compactly
βš™οΈ Microservice Communication Reduce payload size across services

πŸ”‘ SEO & AI Integration Keywords

laravel, php, ai, chatgpt, openai, llm, gemini, mistral, anthropic, laravel-ai, laravel-openai, laravel-llm, php-ai, token-optimizer, prompt-engineering, api-optimization, backend-optimization, laravel-packages, php-development, web-development, json-compression, data-compression, efficient-coding, developer-tools, performance-optimization, api-integration, php-8.1


❀️ Final Thoughts

If you build AI tools with Laravel, TOON is a game-changer. It helps you:

  • πŸš€ Send more context without hitting token limits
  • 🧩 Keep your data clean, readable, and reversible
  • πŸ’Έ Save real money on AI API usage

🧠 β€œCompress your prompts, not your ideas.” β€” Let your code (and AI) breathe.

πŸ“¦ GitHub: https://github.com/sbsaga/toon

πŸ“¦ Packagist: https://packagist.org/packages/sbsaga/toon


Built with ❀️ for Laravel Developers who love clean, smart AI integrations.

Top comments (0)