Build / AI /

Token Optimization

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

TL;DR

  1. Count exact request tokens locally before API dispatch using tiktoken.
  2. Prune redundant conversational turns to conserve available context-window capacity.
  3. Compress structured payloads and eliminate unnecessary whitespace-indentation across data requests.

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 payloads to compact delimited tsv-format rows when transmitting tabular datasets to slash token usage by 40%.
  2. Filter repetitive system reminders out of multi-turn dialogue-history arrays to prevent redundant token billing on every exchange.

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 over-pruning context to the point of stripping critical business rules because under-prompting triggers expensive hallucination recovery cycles.

In Practice

FAQ