π§ 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
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
Config File: config/toon.php
return [
'enabled' => true,
'escape_style' => 'backslash',
'min_rows_to_tabular' => 2,
'max_preview_items' => 200,
];
π§© 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);
Output:
user: Sagar
message: Hello\, how are you?
tasks:
items[2]{done,id}:
false,1
true,2
π Convert TOON β JSON
$toon = <<<TOON
user: Sagar
tasks:
items[2]{id,done}:
1,false
2,true
TOON;
print_r(Toon::decode($toon));
π 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)
βββββββββββββββββ
π§ͺ 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,
]);
});
π§° 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
π 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)