π§ TOON for Laravel β Make LLMs Cheaper, Faster & Friendlier
β¨ Compress your prompts, not your ideas! β¨
AI costs money. Every token counts. Thatβs why I built TOON β a Laravel package that turns heavy JSON into light, readable, reversible notation for LLMs like ChatGPT, Gemini, Claude, and Mistral.
This tutorial is human-friendly, full of real examples, and easy to follow. Whether youβre a beginner or production-level developer β youβll walk away with something useful. π
π Why TOON Exists β The Real Problem
Sending big chunks of JSON to LLMs = 𧨠higher costs + slower responses. TOON solves this by:
- π Reducing token usage by 60β75%
- π Keeping data reversible (TOON β JSON)
- π Staying human-readable
- β‘ Fully integrated with Laravel (Facade + Commands)
π‘ βAI doesnβt need verbose data β it needs clean, structured context.β
π₯ TL;DR β Quick Summary
| Step | Action |
|---|---|
| 1οΈβ£ | composer require sbsaga/toon |
| 2οΈβ£ | Use Toon::convert() before sending API requests |
| 3οΈβ£ | Measure token savings via estimateTokens()
|
| 4οΈβ£ | Use in AI prompts & Artisan commands |
π§ͺ Before vs After (Real Example)
β JSON (7.7 KB)
{
"messages": [
{"id": 1, "done": false},
{"id": 2, "done": true},
...
]
}
β TOON (2.5 KB)
messages:
items[2]{id,done}:
1,false
2,true
π₯ 67% smaller β SAME DATA β lower cost, faster AI.
βοΈ Installation
composer require sbsaga/toon
(Optional) Publish config
php artisan vendor:publish --provider="Sbsaga\Toon\ToonServiceProvider" --tag=config
Created file: config/toon.php
return [
'enabled' => true,
'escape_style' => 'backslash',
'min_rows_to_tabular' => 2,
'max_preview_items' => 200,
];
π Usage β Most Common Pattern
use Sbsaga\Toon\Facades\Toon;
$data = [...];
$toon = Toon::convert($data);
$stats = Toon::estimateTokens($toon);
π§ This alone can show you EXACT cost savings before using AI APIs.
π€ Example β Sending to OpenAI
$prompt = "System: analyze this data\n" . $toon;
$response = Http::withToken(env('OPENAI_KEY'))->post(...);
$decoded = Toon::decode($response['choices'][0]['message']['content'] ?? '');
π Benchmark Route β Try It Yourself
Route::get('/toon-benchmark', function () {
$json = json_decode(file_get_contents(storage_path('app/sample.json')), true);
$jsonEncoded = json_encode($json, JSON_PRETTY_PRINT);
$toonEncoded = Toon::convert($json);
return [
'json_size' => strlen($jsonEncoded),
'toon_size' => strlen($toonEncoded),
'saving_percent' => 100 - (strlen($toonEncoded) / strlen($jsonEncoded) * 100),
];
});
π¦ Artisan Commands
php artisan toon:convert storage/test.json
php artisan toon:convert storage/test.toon --decode --pretty
π§ AI Prompt Templates (Copy & Use)
You are an AI assistant. If you return structured data, use TOON format:
user: Sagar
items[2]{role,message}:
user,Hello
assistant,Hi there
Convert this JSON into TOON format and keep it reversible:
{ ... }
π οΈ Best Practices
β Use estimateTokens() before API calls
β Keep labels meaningful β AI understands better
β Use TOON for logs, chat history, search results
β Add fallback to raw JSON when needed
π§ Ideal Use Cases
| Use Case | Benefit |
|---|---|
| π€ AI prompt engineering | Lower token usage |
| π Log analysis | More readable than JSON |
| β Debugging | Great for dev tools |
| π¦ Microservices | Send smaller payloads |
β€οΈ Contribute & Support
- β Star the repo β it helps a lot!
- π Report issues on GitHub
- π Suggestions welcome
GitHub: https://github.com/sbsaga/toon
Packagist: https://packagist.org/packages/sbsaga/toon
π― Final Thought
π§ βAI doesnβt need more data β it needs better data.β
Thatβs what TOON delivers. Try it, benchmark it β your AI will thank you. π
Top comments (0)