@toonparse/core
v1.0.0
Published
ToonParse - Convert JSON, YAML, CSV to TOON format and reduce LLM token costs by 60%
Maintainers
Readme
@toonparse/core
Reduce LLM token costs by 60% with TOON format
Convert JSON, YAML, CSV to TOON (Token-Oriented Object Notation) format and save up to 60% on LLM tokens. Optimized for ChatGPT, Claude, GPT-4, and other LLMs.
Why ToonParse?
- 💸 Save 30-60% on tokens compared to JSON
- 🎯 73.9% LLM accuracy vs JSON's 69.7%
- 🚀 Zero config - works out of the box
- 📦 Lightweight - minimal dependencies
- 🔄 Lossless - perfect round-trip conversion
Installation
npm install @toonparse/corepnpm add @toonparse/coreyarn add @toonparse/coreQuick Start
import { toToon, fromToon } from '@toonparse/core';
// Convert to TOON
const data = {
users: [
{ id: 1, name: 'Alice', role: 'admin' },
{ id: 2, name: 'Bob', role: 'user' }
]
};
const toon = toToon(data);
console.log(toon);
// Output:
// users[2]{id,name,role}:
// 1,Alice,admin
// 2,Bob,user
// Convert back to JSON
const original = fromToon(toon);
console.log(original);
// { users: [...] }API Reference
toToon(data: unknown): string
Convert JavaScript object/array to TOON format string.
Parameters:
data- Any JSON-serializable data
Returns: TOON format string
Example:
const toon = toToon({ name: 'Alice', age: 30 });
// name: Alice
// age: 30fromToon(toon: string): unknown
Parse TOON format string back to JavaScript object.
Parameters:
toon- TOON format string
Returns: Parsed JavaScript object
Example:
const data = fromToon('name: Alice\nage: 30');
// { name: 'Alice', age: 30 }compareFormats(data: unknown): ComparisonResult
Compare token usage between JSON and TOON formats.
Returns:
{
jsonTokens: number; // Token count for JSON
toonTokens: number; // Token count for TOON
savingsTokens: number; // Tokens saved
savingsPercent: number; // Percentage saved
}Example:
import { compareFormats } from '@toonparse/core';
const comparison = compareFormats(myData);
console.log(`Save ${comparison.savingsPercent}% tokens with TOON!`);countTokens(text: string): number
Calculate approximate token count for any text (uses ~4 chars/token).
Example:
import { countTokens } from '@toonparse/core';
const tokens = countTokens('Hello world');
// ~3 tokenscheckToonEligibility(data: unknown): EligibilityResult
Check if your data is suitable for TOON format.
Returns:
{
score: number; // 0-100 suitability score
reason: string; // Explanation
recommended: boolean; // Should you use TOON?
}Example:
import { checkToonEligibility } from '@toonparse/core';
const check = checkToonEligibility(myData);
if (check.recommended) {
console.log(`Score: ${check.score}/100 - ${check.reason}`);
}Use Cases
Optimizing ChatGPT Prompts
import { toToon } from '@toonparse/core';
const products = [
{ id: 'p1', name: 'Widget', price: 19.99 },
{ id: 'p2', name: 'Gadget', price: 29.99 },
// ... 100 more products
];
// Before: ~5000 tokens (JSON)
const jsonPrompt = `Analyze these products:\n${JSON.stringify(products)}`;
// After: ~2000 tokens (TOON) - 60% savings!
const toonPrompt = `Analyze these products:\n${toToon(products)}`;Cost Savings Calculator
import { compareFormats } from '@toonparse/core';
const comparison = compareFormats(largeDataset);
const monthlyCalls = 100000;
const costPerToken = 0.00003; // GPT-4 pricing
const jsonCost = comparison.jsonTokens * monthlyCalls * costPerToken;
const toonCost = comparison.toonTokens * monthlyCalls * costPerToken;
const savings = jsonCost - toonCost;
console.log(`Monthly savings: $${savings.toFixed(2)}`);Smart Format Selection
import { checkToonEligibility, toToon } from '@toonparse/core';
function optimizeForLLM(data: unknown): string {
const eligibility = checkToonEligibility(data);
if (eligibility.recommended) {
console.log(`Using TOON (${eligibility.score}/100): ${eligibility.reason}`);
return toToon(data);
} else {
console.log(`Using JSON: ${eligibility.reason}`);
return JSON.stringify(data);
}
}Performance Benchmarks
Tested across 4 LLMs on 209 questions:
| Format | Accuracy | Tokens | Efficiency Score | |--------|----------|--------|------------------| | TOON | 73.9% | 2,744 | 26.9 🏆 | | JSON Compact | 70.7% | 3,081 | 22.9 | | YAML | 69.0% | 3,719 | 18.6 | | JSON | 69.7% | 4,545 | 15.3 |
Result: TOON achieves 73.9% accuracy while using 39.6% fewer tokens than JSON.
When to Use TOON
✅ Use TOON for:
- LLM prompts (ChatGPT, Claude, GPT-4)
- Uniform arrays of objects (database results, API responses)
- Cost-sensitive applications
- When token counts matter
❌ Keep JSON for:
- Deeply nested structures
- Non-uniform data
- Standard web APIs
- Configuration files
Free Online Tools
Don't want to code? Try our free converters:
TypeScript Support
Full TypeScript support included with type definitions:
import { toToon, fromToon, type ConversionResult } from '@toonparse/core';
// Full type safety
const toon: string = toToon(data);
const parsed: unknown = fromToon(toon);Advanced Usage
Custom Token Counting
import { countTokens, toToon } from '@toonparse/core';
const data = { /* large dataset */ };
const json = JSON.stringify(data);
const toon = toToon(data);
console.log(`JSON: ${countTokens(json)} tokens`);
console.log(`TOON: ${countTokens(toon)} tokens`);Integration with LangChain
import { toToon } from '@toonparse/core';
import { ChatOpenAI } from 'langchain/chat_models/openai';
const chat = new ChatOpenAI();
const data = [ /* your data */ ];
// Use TOON to reduce costs
const response = await chat.call([
{ role: 'user', content: `Analyze:\n${toToon(data)}` }
]);Contributing
Contributions welcome! Please visit:
License
MIT © ToonParse Team
Links
Built by ToonParse - The #1 TOON format toolkit
