Practical techniques for reducing token usage in AI applications — from prompt engineering and context pruning to semantic deduplication and budget-aware injection strategies.
Founder & Lead Engineer at Toffee
Every token sent to a large language model costs money, consumes context window space, and increases latency. GPT-4o charges $2.50 per million input tokens. A context transfer of 30,000 tokens costs $0.075. Across 10,000 daily active users averaging 3 transfers, the annual input cost exceeds $82,000.
Token optimization is also about quality. Research demonstrates that models lose track of information in the middle of long contexts — the "lost in the middle" effect. By reducing context size, you improve the model's ability to use the context you provide.
Modern LLMs use subword tokenization (Byte Pair Encoding or SentencePiece). Key facts:
| Format | Tokens (3 records) | Reduction |
|---|---|---|
| JSON | 42 | Baseline |
| YAML | 31 | 26% |
| TSV | 18 | 57% |
| Compressed prose | 14 | 67% |
// Before: 47 tokens
You are a helpful AI assistant. When the user asks you a question,
you should provide a detailed, accurate, and helpful response.
// After: 18 tokens
Role: Technical assistant. Provide precise, example-rich answers.
The compressed version conveys the same instructions in 62% fewer tokens.
Often 1-2 examples suffice. Going from 5 to 2 examples can save thousands of tokens. Use minimal examples that demonstrate the pattern, not comprehensive ones.
Requesting tables or bullet points instead of prose produces responses with the same information density in 60% fewer tokens.
Include only the N most recent messages. Simple but loses foundational context.
Pin the first 2-3 messages (project setup) and include the last N messages:
function selectMessages(messages: Message[], maxTokens: number): Message[] {
const pinned = messages.slice(0, 3);
const recent = messages.slice(-10);
let selected = [...pinned, ...recent];
let totalTokens = countTokens(selected);
while (totalTokens > maxTokens && selected.length > 3) {
selected.splice(3, 1);
totalTokens = countTokens(selected);
}
return selected;
}
Score each message by information value. Decisions and entity definitions score high. Conversational filler ("thanks!", "got it") scores zero. This achieves the best compression ratios by eliminating low-value content regardless of position.
When transferring context to a different model, adapt the injected context to fit the target's capacity:
const MODEL_CONSTRAINTS: Record<string, { maxInput: number; reserveOutput: number }> = {
'gpt-4o': { maxInput: 120000, reserveOutput: 8000 },
'claude-3.5-sonnet': { maxInput: 190000, reserveOutput: 10000 },
'gemini-1.5-pro': { maxInput: 990000, reserveOutput: 10000 },
};
function budgetAwareInjection(bundle: ToffeeBundle, targetModel: string): string {
const constraints = MODEL_CONSTRAINTS[targetModel];
const available = constraints.maxInput - constraints.reserveOutput - 500;
if (bundle.metrics.compressed_tokens <= available) return bundle.compressed_context;
return truncateByPriority(bundle, available);
}
The truncateByPriority function removes lowest-priority information first until the bundle fits. Core decisions, entities, and preferences are the last to be removed.
Reduce indentation, remove blank lines, collapse single-statement blocks:
// Before: 45 tokens
async function fetchUser(id: string): Promise<User> {
const response = await fetch('/api/users/' + id);
if (!response.ok) {
throw new Error('User not found');
}
const data = await response.json();
return data as User;
}
// After: 31 tokens (31% reduction)
async function fetchUser(id: string): Promise<User> {
const res = await fetch('/api/users/' + id);
if (!res.ok) throw new Error('User not found');
return await res.json() as User;
}
Preserve comments that explain why. Strip comments that describe what (when the code is self-explanatory).
Track these metrics:
The sweet spot is 60-70% compression. Below 60%, savings are insufficient. Above 75%, you risk losing important context. The optimal point varies by conversation type and target model.
A typical 30-minute conversation produces 15,000 to 35,000 tokens, depending on complexity. Software development sessions are on the higher end due to code blocks.
Research shows models are better at recalling information at the beginning and end of their context window, but less accurate with middle content. Shorter, well-organized context often produces better results.
Not always. Removing too much context leaves the model without enough information. The goal is removing redundant and low-value tokens while preserving high-value information.
A deep technical exploration of semantic compression — the process of reducing AI conversation transcripts by 60-80% while preserving entities, decisions, preferences, and structural relationships.
Learn how AI context transfer works, why language models lose memory between sessions, and how protocols like Toffee compress and port conversation context across ChatGPT, Claude, and Gemini without data loss.
How to architect systems that give AI applications persistent, cross-platform memory — covering local-first storage, sync engines, offline support, and the future of AI knowledge graphs.