Token Optimization

Minimize token consumption and API billing through tiktoken counting, context pruning, payload compression, and whitespace minification.

TL;DR

  1. Count tokens with tiktoken before sending to predict costs.
  2. Trim old turns and repeated context to save context window space.
  3. Send compact formats like TSV and strip extra whitespace.

Local Token Counting

    Tiktoken Encoding

    Initialize BPE tokenizer matching target model architecture.

    import { get_encoding } from 'tiktoken';
    
    const enc = get_encoding('cl100k_base');
    const count = enc.encode('Hello world!').length;
    enc.free(); // Free WebAssembly memory
    Chat Message Estimator

    Account for role tokens and message framing overhead.

    function countMessageTokens(msg: ChatMessage): number {
      const enc = get_encoding('cl100k_base');
      const tokens = enc.encode(msg.content).length + 4;
      enc.free();
      return tokens;
    }
    Pre-Flight Budget Check

    Validate prompt size against hard account limits before dispatch.

    const total = countPrompt(prompt);
    if (total > 8000) {
      throw new Error(
        `Tokens exceed limit: ${total}`
      );
    }

Payload Compression

    JSON Minification

    Strip superfluous indentation whitespace before sending payload.

    const minified = JSON.stringify(data);
    // Saves ~25% tokens vs JSON.stringify(data, null, 2)
    Tabular TSV Conversion

    Represent row data as tab-separated values instead of JSON.

    function toTSV(rows: any[]): string {
      const head = Object.keys(rows[0]).join('\t');
      const body = rows
        .map(r => Object.values(r).join('\t'));
      return [head, ...body].join('\n');
    }
    Stopword Stripping

    Remove low-information conversational filler words from queries.

    function pruneFiller(text: string): string {
      const pat = /\b(please|kindly|could you)\b/gi;
      return text.replace(pat, '');
    }

Context Window Pruning

    Sliding Turn Window

    Retain only the most recent N conversational exchanges.

    function getRecentHistory(msgs: ChatMessage[], n = 6) {
      const system = msgs.filter(m => m.role === 'system');
      const recents = msgs.slice(-n);
      return [...system, ...recents];
    }
    Token Budget Trimmer

    Trim history dynamically to fit strict token ceiling.

    function trimBudget(
      msgs: ChatMessage[],
      max = 2000
    ) {
      let cur = 0;
      return msgs.reverse().filter(m => {
        cur += countTokens(m.content);
        return cur <= max;
      }).reverse();
    }
    Context Summarization

    Condense long dialog histories into compact running summaries.

    const prompt = `Summarize: ${oldChat}`;
    const summary = await miniModel
      .complete(prompt);

Prompt Efficiency Hygiene

    Concise System Prompts

    Refactor redundant wordy instructions into compact bullet rules.

    // Bad: 'Please act as an expert...'
    // Good: 'Role: TS Engineer. Output: Code.'
    Single-Letter Delimiters

    Replace verbose XML tags with compact delimiters when tokens matter.

    const prompt = `[Q]\n${userQuery}\n[A]\n`;
    // Saves tokens over <user_input_question_data>
    Token Billing Audit

    Record token metrics to calculate daily cost attribution.

    function auditSpend(tokens: number, ratePerM: number) {
      const cost = (tokens / 1000000) * ratePerM;
      metrics.gauge('ai.cost.daily', cost);
    }

Tips

  1. Convert verbose JSON arrays to compact TSV rows when sending tabular data, which can cut token usage by around 40%.
  2. Filter repeated reminders out of the messages array so you do not pay for the same instruction on every turn.

Warnings

  1. Do not rely on naive word counts or character estimates because punctuation, code, and whitespace consume disproportionate numbers of BPE-tokens.
  2. Avoid pruning so hard that you strip critical business rules from the system prompt, which triggers costly hallucination and retries.

In Practice

FAQ