DEV Community

Atis Papai
Atis Papai

Posted on

Save 60% on LLM API Costs with TOON Format in NestJS

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

TOON (1,400 tokens - 60% savings):

users[100]{id,name,email,age}:
1,Alice,alice@example.com,30
Enter fullscreen mode Exit fullscreen mode

Annual savings (1M LLM calls): $10k-$50k

Setup (5 minutes)

Install the package:

npm install nestjs-toon
Enter fullscreen mode Exit fullscreen mode

Add to your app module:

// app.module.ts
import { ToonModule } from 'nestjs-toon';

@Module({
  imports: [ToonModule.forRoot({ global: true })],
})
export class AppModule {}
Enter fullscreen mode Exit fullscreen mode

Done! Your API now supports TOON via content negotiation.

Usage

Request JSON (default):

curl http://localhost:3000/users
Enter fullscreen mode Exit fullscreen mode

Request TOON (explicit):

curl -H "Accept: text/toon" http://localhost:3000/users
Enter fullscreen mode Exit fullscreen mode

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}` }],
});
Enter fullscreen mode Exit fullscreen mode

When to Use

Use TOON for:

  • Uniform arrays (user lists, orders, logs)

Stick with JSON for:

  • Deeply nested data
  • Binary uploads
  • Legacy systems

Links


What's your biggest LLM cost bottleneck? Would love to hear your experience!

Top comments (0)