Building LLM apps? Your API response size directly impacts costs. TOON format reduces tokens by 30-60% vs JSON.
The Problem
JSON (3,500 tokens for 100 users):
{"users": [{"id":1,"name":"Alice","email":"alice@example.com","age":30}, ...]}
TOON (1,400 tokens - 60% savings):
users[100]{id,name,email,age}:
1,Alice,alice@example.com,30
Annual savings (1M LLM calls): $10k-$50k
Setup (5 minutes)
Install the package:
npm install nestjs-toon
Add to your app module:
// app.module.ts
import { ToonModule } from 'nestjs-toon';
@Module({
imports: [ToonModule.forRoot({ global: true })],
})
export class AppModule {}
Done! Your API now supports TOON via content negotiation.
Usage
Request JSON (default):
curl http://localhost:3000/users
Request TOON (explicit):
curl -H "Accept: text/toon" http://localhost:3000/users
100% backward compatible - existing JSON clients work unchanged.
LLM Integration
// Fetch data in TOON format
const response = await fetch('http://localhost:3000/users', {
headers: { Accept: 'text/toon' },
});
const toonData = await response.text();
// Send to Claude - 60% fewer tokens
const message = await claude.messages.create({
model: "claude-3-5-sonnet-20241022",
messages: [{ role: "user", content: `Analyze:\n\n${toonData}` }],
});
When to Use
Use TOON for:
- Uniform arrays (user lists, orders, logs)
Stick with JSON for:
- Deeply nested data
- Binary uploads
- Legacy systems
Links
- npm: https://www.npmjs.com/package/nestjs-toon
- GitHub: https://github.com/papaiatis/nestjs-toon
- TOON spec: https://toonformat.dev/
What's your biggest LLM cost bottleneck? Would love to hear your experience!
Top comments (0)