DEV Community

Sagar Sunil Bhedodkar
Sagar Sunil Bhedodkar

Posted on

πŸš€ Token Estimation for AI Prompts in Laravel β€” TOON

Write cleaner prompts. Spend fewer tokens. Build smarter AI apps.


🧠 What Problem Does It Solve?

Every time your Laravel app sends data to an AI model β€” it costs tokens. More tokens = more cost + less context.
But most developers don’t know how heavy their prompt really is.

That’s why TOON includes a built-in Token Estimator β€” a lightweight tool to quickly measure your prompt before sending it to an LLM.


✨ What Is TOON?

TOON (Token-Optimized Object Notation) is a compact, human-readable data format built for Laravel. It converts PHP arrays / JSON into an AI-friendly format that reduces token usage and improves context clarity.

And now β€” it can also estimate the token weight of that data.


πŸ”’ Token Estimation β€” The Feature You Actually Need

TOON gives you a native, zero-dependency PHP method to estimate token usage:

use Sbsaga\Toon\Facades\Toon;

$prompt = Toon::convert($data);            // Converts JSON/PHP array to TOON format
$stats  = Toon::estimateTokens($prompt);   // Estimates token usage

dd($stats);
Enter fullscreen mode Exit fullscreen mode

Example Output:

{
  "words": 20,
  "chars": 182,
  "tokens_estimate": 19
}
Enter fullscreen mode Exit fullscreen mode

πŸ’‘ Why This Helps Developers

βœ” Spot heavy prompts instantly
βœ” Reduce AI costs before making API calls
βœ” Keep more context inside model limits
βœ” Works locally, in CI, or before production
βœ” No external tokenizer β€” fully PHP native


🧭 Practical Use Cases

Use Case How TOON Helps
Local development Measure prompt size while designing AI requests
CI check Fail build if prompt > 3,000 tokens
Debugging Compare JSON vs TOON format side-by-side
Optimization Remove unnecessary data & observe impact
LLM cost control Save real money over large requests

πŸ“Œ Important Note

  • This is an approximate estimation, not a billing-accurate tokenizer.
  • It is model-agnostic, fast, and perfect for comparison / debugging.

πŸ§ͺ Try It in 20 Seconds

composer require sbsaga/toon
Enter fullscreen mode Exit fullscreen mode

πŸ“Œ Ready-to-Use Tags (Paste into Medium / Dev.to)

laravel, php, ai, prompt-engineering, chatgpt, openai, ai-tooling, laravel-dev


🧠 β€œCompress your prompts, not your ideas.” β€” TOON helps you talk to AI efficiently.

Top comments (0)