DEV Community

Cover image for πŸš€ TOON for Laravel β€” A Complete Tutorial to Make AI Prompts Cheaper & Faster
Sagar Sunil Bhedodkar
Sagar Sunil Bhedodkar

Posted on

πŸš€ TOON for Laravel β€” A Complete Tutorial to Make AI Prompts Cheaper & Faster

🧠 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},
    ...
  ]
}
Enter fullscreen mode Exit fullscreen mode

βœ… TOON (2.5 KB)

messages:
  items[2]{id,done}:
    1,false
    2,true
Enter fullscreen mode Exit fullscreen mode

πŸ’₯ 67% smaller β€” SAME DATA β€” lower cost, faster AI.


βš™οΈ Installation

composer require sbsaga/toon
Enter fullscreen mode Exit fullscreen mode

(Optional) Publish config

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

Created 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

πŸš€ Usage β€” Most Common Pattern

use Sbsaga\Toon\Facades\Toon;

$data = [...];
$toon = Toon::convert($data);
$stats = Toon::estimateTokens($toon);
Enter fullscreen mode Exit fullscreen mode

🧠 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'] ?? '');
Enter fullscreen mode Exit fullscreen mode

πŸ“Š 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),
  ];
});
Enter fullscreen mode Exit fullscreen mode

πŸ“¦ Artisan Commands

php artisan toon:convert storage/test.json
php artisan toon:convert storage/test.toon --decode --pretty
Enter fullscreen mode Exit fullscreen mode

🧠 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
Enter fullscreen mode Exit fullscreen mode
Convert this JSON into TOON format and keep it reversible:
{ ... }
Enter fullscreen mode Exit fullscreen mode

πŸ› οΈ 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)