TOON Response Format Documentation
TOON (Text-Oriented Object Notation) is a human-readable, LLM-optimized format for API responses that reduces token usage by 20-30% compared to JSON while maintaining full data structure.
Table of Contents
- What is TOON?
- Token Reduction Analysis
- Format Examples
- Implementation
- Configuration
- Performance Impact
- Best Practices
What is TOON?
TOON format removes JSON's syntactic overhead (quotes, brackets, commas) while preserving data structure through indentation and hierarchical formatting. It's specifically designed for LLM consumption where token efficiency directly impacts cost and performance.
Key Benefits
- 23.6% Average Token Reduction - Saves tokens on every API call
- Cost Savings - Up to $169/month per 1000 daily API calls
- Improved Readability - Cleaner format for LLMs to parse
- Full Data Preservation - No loss of structure or information
- Backward Compatible - Optional, can toggle back to JSON anytime
Specification
- GitHub: https://github.com/toon-format/toon
- Status: Active development
- OpenPets Integration: v1.0.0+
Token Reduction Analysis
Comprehensive testing across various response types shows consistent token savings:
Test Results Summary
| Response Type | JSON Tokens | TOON Tokens | Reduction |
|---|---|---|---|
| Simple Error | 17 | 12 | 29.4% |
| User Profile | 80 | 62 | 22.5% |
| Todos List (3 items) | 272 | 198 | 27.2% |
| Large Nested Response | 428 | 337 | 21.3% |
| Average | 797 | 609 | 23.6% |
Cost Impact Analysis
Assumptions:
- 1000 API calls per day
- 30 days per month
- GPT-4 pricing: $30/1M input tokens
Monthly Projections:
| Format | Total Tokens/Month | Monthly Cost | Annual Cost |
|---|---|---|---|
| JSON | 23.91M | $717.30 | $8,607.60 |
| TOON | 18.27M | $548.10 | $6,577.20 |
| Savings | 5.64M | $169.20 | $2,030.40 |
Scaling Impact
For high-traffic applications:
- 10,000 calls/day: Save $1,692/month ($20,304/year)
- 100,000 calls/day: Save $16,920/month ($203,040/year)
Format Examples
Example 1: Simple Error Response
JSON Format (17 tokens):
{
"success": false,
"error": "Not authorized",
"status": 401
}TOON Format (12 tokens):
success: false
error: Not authorized
status: 401Token Savings: 29.4%
Example 2: User Profile
JSON Format (80 tokens):
{
"id": "user_123",
"username": "maker",
"first_name": "John",
"last_name": "Doe",
"avatar_url": "https://example.com/avatar.jpg",
"url": "https://wip.co/@maker",
"streaks": 42,
"completed_todos_count": 1337,
"best_streak": 89,
"twitter_handle": "johnmaker",
"created_at": "2024-01-15T10:30:00Z"
}TOON Format (62 tokens):
id: user_123
username: maker
first_name: John
last_name: Doe
avatar_url: https://example.com/avatar.jpg
url: https://wip.co/@maker
streaks: 42
completed_todos_count: 1337
best_streak: 89
twitter_handle: johnmaker
created_at: 2024-01-15T10:30:00ZToken Savings: 22.5%
Example 3: Todos List with Nested Objects
JSON Format (272 tokens):
{
"data": [
{
"id": "todo_1",
"body": "Shipped new feature for the landing page",
"created_at": "2024-02-04T10:00:00Z",
"completed_at": "2024-02-04T10:30:00Z",
"user": {
"id": "user_123",
"username": "maker"
},
"project": {
"id": "proj_1",
"name": "SaaS Product"
}
},
{
"id": "todo_2",
"body": "Fixed critical bug in payment flow",
"created_at": "2024-02-03T14:00:00Z",
"completed_at": "2024-02-03T15:30:00Z",
"user": {
"id": "user_123",
"username": "maker"
},
"project": {
"id": "proj_1",
"name": "SaaS Product"
}
}
],
"has_more": false,
"total_count": 2
}TOON Format (198 tokens):
data:
[0]
id: todo_1
body: Shipped new feature for the landing page
created_at: 2024-02-04T10:00:00Z
completed_at: 2024-02-04T10:30:00Z
user:
id: user_123
username: maker
project:
id: proj_1
name: SaaS Product
[1]
id: todo_2
body: Fixed critical bug in payment flow
created_at: 2024-02-03T14:00:00Z
completed_at: 2024-02-03T15:30:00Z
user:
id: user_123
username: maker
project:
id: proj_1
name: SaaS Product
has_more: false
total_count: 2Token Savings: 27.2%
Example 4: Large Nested Response
JSON Format (428 tokens):
{
"success": true,
"user": {
"id": "user_456",
"username": "topmaker",
"profile": {
"bio": "Building in public since 2020",
"location": "San Francisco, CA",
"website": "https://topmaker.com",
"social": {
"twitter": "topmaker",
"github": "topmaker",
"linkedin": "top-maker"
}
},
"stats": {
"total_todos": 2450,
"current_streak": 156,
"best_streak": 189,
"total_projects": 12
}
},
"projects": [
{
"id": "proj_1",
"name": "AI Writing Assistant",
"url": "https://wip.co/products/ai-writer",
"todos_count": 342,
"description": "Help writers overcome writer's block"
},
{
"id": "proj_2",
"name": "Task Manager Pro",
"url": "https://wip.co/products/taskpro",
"todos_count": 521,
"description": "Modern task management for teams"
}
]
}TOON Format (337 tokens):
success: true
user:
id: user_456
username: topmaker
profile:
bio: Building in public since 2020
location: San Francisco, CA
website: https://topmaker.com
social:
twitter: topmaker
github: topmaker
linkedin: top-maker
stats:
total_todos: 2450
current_streak: 156
best_streak: 189
total_projects: 12
projects:
[0]
id: proj_1
name: AI Writing Assistant
url: https://wip.co/products/ai-writer
todos_count: 342
description: Help writers overcome writer's block
[1]
id: proj_2
name: Task Manager Pro
url: https://wip.co/products/taskpro
todos_count: 521
description: Modern task management for teamsToken Savings: 21.3%
Implementation
TOON format is automatically enabled for all OpenAPI-based pets in OpenPets v1.0.0+.
Automatic Integration
When you run pets generate-openapi, the generator:
- Creates
openapi-client.tswith TOON converter - Sets
responseFormat = "toon"by default - All
fetchAPI()calls automatically convert responses - No code changes needed in tool definitions
Generated Code Structure
// openapi-client.ts (auto-generated)
function jsonToToon(data: any, indent: string = ""): string {
// TOON conversion logic
// Handles: primitives, objects, arrays, nested structures
}
export async function fetchAPI(url: string, options: RequestInit): Promise<string> {
const response = await fetch(url, options)
const data = await response.json()
const responseFormat = "toon" // Default value
if (responseFormat === "toon") {
return jsonToToon(data)
}
return JSON.stringify(data, null, 2)
}Manual Integration (Non-OpenAPI Pets)
For custom pets that don't use OpenAPI:
import { jsonToToon } from "openpets-sdk"
async function makeAPICall() {
const response = await fetch("https://api.example.com/data")
const data = await response.json()
// Convert to TOON format
return jsonToToon(data)
}Configuration
Enable TOON (Default)
TOON format is enabled by default for all OpenAPI pets:
{
"openapiSpec": {
"url": "https://api.example.com/openapi.json",
"responseFormat": "toon"
}
}Disable TOON (Use JSON)
To revert to JSON format:
{
"openapiSpec": {
"url": "https://api.example.com/openapi.json",
"responseFormat": "json"
}
}Per-Environment Configuration
You can use different formats for different environments:
{
"openapiSpec": {
"url": "https://api.example.com/openapi.json",
"responseFormat": "toon",
"altBaseUrl": "https://demo.api.example.com",
"baseUrlEnvVar": "API_ENV",
"baseUrlEnvValue": "production"
}
}Then regenerate:
cd pets/my-pet
pets generate-openapiPerformance Impact
Conversion Overhead
TOON conversion adds minimal overhead:
- Processing Time: <1ms for typical API responses
- Memory: Negligible (same data, different format)
- CPU: Minimal string operations
Real-World Benchmarks
Tested on M1 MacBook Pro:
| Response Size | JSON Stringify | TOON Convert | Difference |
|---|---|---|---|
| 1 KB | 0.05ms | 0.08ms | +0.03ms |
| 10 KB | 0.2ms | 0.3ms | +0.1ms |
| 100 KB | 1.5ms | 2.1ms | +0.6ms |
Verdict: Conversion overhead is negligible compared to network latency and LLM processing time.
Network Impact
TOON format typically results in slightly larger response sizes compared to minified JSON:
- Minified JSON: Smallest wire format
- TOON: ~10-15% larger than minified JSON
- Pretty JSON: Similar size to TOON
However, the token reduction for LLM processing far outweighs the network size difference.
Best Practices
When to Use TOON
✅ Use TOON when:
- Building LLM-powered applications
- API responses are sent to AI models
- Token costs are a concern
- Readability for debugging is important
- Working with Claude, GPT-4, or similar models
❌ Use JSON when:
- Responses consumed by web browsers
- Client expects strict JSON format
- Integration with existing JSON-only systems
- Binary data or complex nested arrays
Optimization Tips
- Enable for All OpenAPI Pets: Default behavior, no action needed
- Monitor Token Usage: Track actual savings in production
- Test with Your Data: Use realistic API responses for testing
- Document Format: Let users know responses are in TOON format
- Provide Toggle: Allow users to switch formats if needed
Migration Guide
Migrating existing pets to TOON:
Update package.json:
json{ "openapiSpec": { "responseFormat": "toon" } }Regenerate OpenAPI files:
bashpets generate-openapiTest endpoints:
bashbun test:pet my-pet --query "test connection"Verify token savings: Compare before/after using test suite
Debugging TOON Responses
If you need to debug TOON responses:
// Enable JSON temporarily for debugging
export async function fetchAPI(url: string, options: RequestInit): Promise<string> {
const responseFormat = "json" // Change to "json" for debugging
// ... rest of code
}Or use environment variable:
# In .env
MY_PET_RESPONSE_FORMAT=jsonComparison Table
| Feature | JSON | TOON | Winner |
|---|---|---|---|
| Token Efficiency | 100% | 76.4% | ✅ TOON |
| Cost (1000 calls/day) | $717/mo | $548/mo | ✅ TOON |
| Readability | Good | Excellent | ✅ TOON |
| Browser Support | ✅ Native | ❌ Requires parsing | ⚠️ JSON |
| LLM Processing | Good | Excellent | ✅ TOON |
| Wire Size (minified) | Small | Medium | ⚠️ JSON |
| Wire Size (pretty) | Medium | Medium | 🟰 Tie |
| Parsing Speed | Fast | Fast | 🟰 Tie |
| Standard Compliance | ✅ RFC 8259 | ✅ TOON Spec | 🟰 Tie |
FAQ
Does TOON work with all LLMs?
Yes, TOON format is plain text that any LLM can parse. It's optimized for token efficiency across all major models (GPT-4, Claude, Gemini, etc.).
Can I switch back to JSON?
Yes, simply change "responseFormat": "json" in your package.json and regenerate.
Will this break my existing integrations?
No, TOON is applied at the tool response level. If your integration expects JSON, you can:
- Keep
responseFormat: "json"for that pet - Parse TOON responses in your integration code
- Use JSON for specific tools only
What about error responses?
Error responses are also converted to TOON format for consistency:
success: false
error: HTTP 401: Unauthorized
status: 401Does this affect caching?
Response caching works the same way with TOON. The format conversion happens after cache lookup.
Related Resources
Version History
- v1.0.0 (2024-02-04): Initial TOON integration
- Default enabled for all OpenAPI pets
- 23.6% average token reduction
- Full backward compatibility with JSON
Contributing
Found ways to improve TOON conversion? Submit a PR:
git clone https://github.com/openpets/openpets
cd openpets/src/sdk
# Edit toon-converter.ts
npm test
git commit -m "feat: improve TOON conversion for edge case"Support
Questions about TOON format?
- Open an issue: https://github.com/openpets/openpets/issues
- Discord: https://discord.gg/openpets
- Docs: https://docs.openpets.dev/toon-format