Token Optimization for LLMs: Understanding Tokens and Choosing Efficient Data Formats

    As large language models shift the unit of cost from bytes to tokens, traditional data formats like JSON become surprisingly expensive. This article explains what tokens are, why JSON wastes context window space, how token-optimized formats built on YAML-style indentation and CSV-style tables can cut token usage by 30%-60%, and when developers should still stick with JSON. It also covers practical applications in AI Agent workflows and RAG systems, helping you manage your model's "attention budget" effectively.

    1. Introduction: From Physical Bytes to Semantic Units

    In traditional cloud computing and microservice architectures, software engineers are used to measuring data costs in terms of “bandwidth” and “storage.” Whether in GB or TB, data volume has always been an accumulation of physical bytes. However, with the rise of large language models, the economic model of software engineering has fundamentally changed. The core unit of measurement is no longer the byte—it is the token. This shift affects more than billing rules; it deeply influences how systems should be architected.

    LLM API pricing is typically split between input and output. Although context windows keep expanding—from 4k in early models to 128k, 200k, and even million-token contexts today—context space remains a scarce and expensive resource. More importantly, the attention mechanism at the core of LLMs usually has a computational complexity that grows quadratically with sequence length, i.e., O(N²). This means redundant input data not only increases direct costs but also significantly increases time-to-first-token and overall inference latency. When building AI applications, we are not just writing code—we are managing an “attention budget.” Every piece of redundant information sent to the model consumes its limited attention capacity and may crowd out truly critical information, causing the model to “forget” or hallucinate.

    2. What Is a Token? Understanding the LLM’s Computational Unit

    Simply put, a token is the smallest semantic unit that a large language model uses to process and understand natural language. It is neither a complete word nor a single letter, but a “subword” unit somewhere in between.

    LLMs cannot directly read raw text characters during training and inference; the text must be converted into numbers. This is typically done using algorithms such as Byte Pair Encoding (BPE). These algorithms split text into tokens based on word frequency. For example, the English word “hamburger” might be split into three tokens: “ham”, “bur”, and “ger”. For Chinese, a single character may correspond to one or more tokens, depending on the tokenizer’s vocabulary.

    Therefore, the token count does not map directly to character or word counts. Information density has become the new optimization metric—conveying as much accurate semantic information as possible using as few tokens as possible.

    3. The Problem with Traditional Formats: Why JSON Is Bulky

    JSON was the undisputed king of the Web 2.0 era. With its clear hierarchical structure and good readability, it became the standard for front-end and back-end data exchange. However, its redundant syntax has become increasingly cumbersome in the LLM era, where tokens are billed and context windows are limited.

    In JSON, a large number of quotes, braces, commas, and repeated key names all consume tokens. For example:

    1. {
    2. "name": "Zhang San",
    3. "age": 28,
    4. "city": "Beijing"
    5. }

    Even this simple JSON record contains many punctuation marks and structural characters. When an AI Agent passes complex context or large tool call results, JSON’s structural redundancy inflates quickly, wasting a precious token budget and lengthening inference time.

    4. Exploring New Token-Oriented Data Formats

    To address this challenge, the industry has begun exploring new data format designs, such as token-oriented object notation. The core idea behind these formats is simple: express the maximum amount of data structure with the minimum token overhead.

    These new formats combine YAML’s indentation-based structure with CSV’s tabular layout to achieve very high information density. YAML uses indentation to express hierarchy, eliminating most braces and quotes, while CSV has a natural advantage for tabular data, avoiding repeated key names on every row.

    For example, the JSON above can be optimized into a YAML-like indented structure:

    1. name: Zhang San
    2. age: 28
    3. city: Beijing

    If you are processing batch user data, it can be converted into a CSV layout:

    1. name,age,city
    2. Zhang San,28,Beijing
    3. Li Si,30,Shanghai

    With this hybrid strategy, token-oriented formats can achieve token compression rates of 30%-60%. For AI applications that process large contexts, this translates into significant direct cost savings and faster inference.

    5. In-Depth Comparison: Trade-offs Between the New Format and JSON/YAML/CSV

    Introducing any new technology involves trade-offs. Token-optimized data formats deliver compression gains, but they also carry potential risks of semantic ambiguity.

    1. Token compression: By stripping away JSON’s redundant symbols and combining CSV’s compact tabular layout, the new format significantly reduces token counts when transmitting long lists or nested objects. This directly lowers API call costs and shortens time-to-first-token.
    2. Model comprehension: Modern LLMs were exposed to massive amounts of JSON, YAML, and CSV data during pretraining, so they understand these formats extremely well. When facing a non-standard hybrid format, however, the model may require more prompt guidance, and in rare complex nesting scenarios, parsing errors can occur.
    3. Semantic ambiguity risk: YAML relies on indentation, which is error-prone in text streams that mix spaces and tabs; CSV requires careful escaping when fields contain commas. Compared with JSON’s strict syntax, the new format demands more preprocessing from developers to ensure robustness.

    In practice, developers should not blindly abandon JSON. Instead, they should choose based on the characteristics of their data. For shallow structures with high field repetition, compact CSV-like formats offer huge gains; for deeply nested, complex objects that require strict type validation, JSON remains the safer choice.

    6. Strategic Use Cases: AI Agents and RAG Systems

    Optimizing data formats is strategically important in AI Agent and Retrieval-Augmented Generation (RAG) systems.

    In multi-turn AI Agent interactions, the system must continuously pack conversation history, tool call results, and environment state into the context. If JSON is used to wrap this large state machine data, structural symbols will exhaust the context window quickly. Adopting token-optimized compact formats allows an Agent to retain longer conversational memory, execute more complex task chains, and reduce the latency of each state synchronization.

    In RAG systems, knowledge chunks retrieved from vector databases usually come with metadata. Packing this metadata tightly alongside the text content lets you inject more high-quality knowledge chunks into a limited context window, improving both the accuracy and breadth of the model’s answers.

    7. Conclusion and Outlook: Managing the Attention Budget in the AI Era

    So, should we abandon JSON in the AI era? The answer is not a simple yes or no. The key is a shift in mindset: in the AI era, data is no longer just instructions for programs to execute—it is text for large models to “read.”

    Understanding what tokens really are, and recognizing the computational cost of the attention mechanism, is essential knowledge for modern AI engineers. Whether you adopt token-oriented object notation or simply trim fields and compress your existing JSON, you need to treat the “token budget” as a core consideration in system design. By streamlining data structures and increasing information density, you can find the right balance between cost control and inference performance, and build AI applications that are truly efficient and scalable.