Skip to main content

Overview

LLM API costs are directly tied to token count. These optimization tools help you reduce token usage while maintaining accuracy, enabling cost-effective AI applications at scale.

TOON Format

63.9% average token reduction for structured data

Headroom

47-92% token savings through intelligent compression

Why Optimize?

Impact on API Costs

Based on GPT-4 pricing ($0.03/1K input tokens):

Toonify Token Optimization

Reduce token usage by 30-73% using TOON (Token-Oriented Object Notation) format

What is TOON?

TOON is a compact serialization format designed specifically for LLM token efficiency. It achieves CSV-like compression while maintaining structure and readability.

Key Benefits

63.9% Average Reduction

Verified across 50 real-world datasets

73.4% for Tabular Data

Optimal for structured, uniform data

Human Readable

Still easy to understand and debug

<1ms Overhead

Negligible conversion time

Format Comparison

Token Savings: 85 → 39 tokens (54.1% reduction)Cost Impact: 2.55/1Krequests2.55/1K requests → 1.17/1K requests

Implementation

1

Install Toonify

2

Convert Data to TOON

3

Send to LLM

4

Decode if Needed

Real-World Example

Results:
  • JSON: 487 bytes, ~165 tokens
  • TOON: 186 bytes, ~68 tokens
  • Reduction: 58.8% tokens, 61.8% bytes

Best Use Cases

Optimal for:
  • Product catalogs
  • CSV exports
  • Database query results
  • API response data
  • Survey results
  • Analytics data
Token savings: 60-73%
Good for:
  • Configuration files
  • Uniform object arrays
  • API payloads
  • Log data
Token savings: 50-65%
Avoid TOON for:
  • Highly nested data (greater than 3 levels)
  • Irregular/heterogeneous structures
  • Small payloads (less than 100 bytes)
  • Binary data
  • When JSON compatibility is critical

Interactive Demo

Run with: streamlit run toonify_app.py

Performance Benchmarks


Headroom Context Optimization

Reduce token usage by 47-92% through intelligent context compression for AI agents

What is Headroom?

Headroom is a context optimization layer that compresses tool outputs and conversation history while preserving accuracy. Unlike simple truncation, it uses statistical analysis to keep what matters.

Key Benefits

47-92% Token Reduction

Verified across production workloads

Zero Code Changes

Transparent proxy integration

Reversible Compression

LLM can retrieve original data via CCR

Provider Caching

Optimizes for OpenAI/Anthropic caching

Core Features

Statistical Compression

Keeps:
  • First N items (context)
  • Last N items (recency)
  • Anomalies (statistical outliers)
  • Query-relevant matches
Removes:
  • Repetitive boilerplate
  • Redundant middle sections
  • Low-information content

Installation & Setup

1

Install Headroom

2

Choose Integration Method

Real-World Performance

These are actual results from production API calls, not estimates.

Needle in Haystack Test

Setup:
  • 100 production log entries
  • 1 critical FATAL error at position 67
  • Question: “What caused the outage? Error code? Fix?”
Baseline (no compression):
With Headroom:
What Headroom kept:
  • Position 67: FATAL error (the needle)
  • Position 1-2: Context (timeline start)
  • Position 99-100: Most recent state
  • Position 45: Anomaly (connection spike)
What Headroom removed:
  • 96 INFO/DEBUG entries
  • Repetitive health checks
  • Standard operational logs
Result: Same accuracy, 87.6% fewer tokens

Configuration

HeadroomChatModel
class
LangChain integration with compression

Best Use Cases

Optimal for:
  • Multi-tool workflows
  • Code search agents
  • Database query agents
  • API integration agents
  • Log analysis agents
Average savings: 75-90%
Ideal for:
  • Code search results (100+ files)
  • Database query results (1000+ rows)
  • API responses (large JSON)
  • Log files (10K+ lines)
  • Documentation searches
Average savings: 80-92%
Useful for:
  • Long chat sessions
  • Multi-turn debugging
  • Context-heavy conversations
  • Memory-intensive agents
Average savings: 50-70%

Safety Guarantees

Never Removes Human Content

User and assistant messages are always preserved in full

Never Breaks Tool Pairing

Tool calls and responses stay together

Parse Failures = No-op

Malformed content passes through unchanged

Reversible Compression

LLM can retrieve original data via CCR

Best Practices

Cost Calculator

Resources

Toonify GitHub

TOON format library and examples

Headroom GitHub

Context optimization framework

Example Apps

Complete optimization demos

OpenAI Tokenizer

Test token counting