How TOON Compression Works
One of Semfora's most impactful features is TOON compression—our custom format that reduces token usage by 73% compared to JSON. Let's explore how it works and why it matters.
The Token Problem
Large Language Models have limited context windows. Every token counts. When you're trying to give an AI assistant context about your codebase, you're constantly fighting this limit.
Traditional approaches send JSON:
{
"symbols": [{
"name": "validateUser",
"kind": "function",
"visibility": "public",
"parameters": [
{ "name": "email", "type": "string" },
{ "name": "password", "type": "string" }
],
"calls": [
{ "name": "hashPassword", "async": true }
]
}]
}
That's 847 tokens for a single function's metadata. Multiply by thousands of functions and you've blown your context window before even starting.
The TOON Approach
TOON (Text Object-Oriented Notation) takes a different approach:
symbol: validateUser
kind: fn
vis: pub
params: email:string, password:string
calls:
- hashPassword [await]
Same information, 127 tokens. That's an 85% reduction.
Design Principles
1. Human-Readable Compression
Unlike binary formats, TOON is still readable. AI models can parse it naturally without special tooling.
2. Domain-Specific Abbreviations
We use consistent abbreviations that are unambiguous in the code analysis domain:
fnfor functionvisfor visibilitypubfor publicparamsfor parameters
3. Inline Metadata
Instead of nested objects, we use inline annotations:
hashPassword [await]instead of{ "name": "hashPassword", "async": true }
4. Whitespace Efficiency
YAML-style indentation is more token-efficient than JSON's brackets and quotes.
Real-World Impact
In practice, this means:
- 3x more context: Feed your AI assistant information about 3x more code
- Faster responses: Less to process means faster completions
- Lower costs: Token usage directly impacts API costs
Implementation
The TOON encoder is part of semfora-engine and handles the conversion automatically:
// Semantic analysis produces structured data
let symbol = analyze_function(&ast)?;
// TOON encoding compresses for output
let toon = ToonEncoder::encode(&symbol);
Try It Yourself
Install Semfora Engine and see the compression in action:
semfora-engine analyze src/auth.ts --format toon
Compare with JSON output to see the difference:
semfora-engine analyze src/auth.ts --format json
The token savings add up quickly across your entire codebase.