Token-Efficient Formatting: Markdown vs. XML vs. JSON

Token-Efficient Formatting: Markdown vs. XML vs. JSON

Master the structural semantics of tokens. Learn which format is the most compact for data injection and how to avoid the 'Syntax Tax'.

Token-Efficient Formatting: Markdown vs. XML vs. JSON

In AI engineering, the "Shape" of your data determines the "Size" of your bill. How you format blocks of text, lists of items, and structured data can result in variations of 20-30% in token usage for the exact same information.

In this lesson, we compare the three main formatting standards—Markdown, XML, and JSON—from a Token Density perspective. We’ll learn which works best for specific tasks and how to avoid "Syntax Bloat."


1. The Token Density Table

FormatToken Cost (Sample)Readability (for AI)Best Use Case
MarkdownLowHighLong text, lists, headers
XMLMediumHighMulti-document context injection
JSONHighHighStructured data, API responses
YAMLLowestMediumConfiguration and state

2. JSON: The "Brace Tax"

JSON is the industry standard for APIs, but it is not token-efficient for LLMs.

  • Every {, }, ", and : is a token (or part of one).
  • Quotes around keys are mandatory.
  • Commas are mandatory.

JSON (55 Tokens):

{
  "customer": "John Doe",
  "status": "active",
  "purchases": [
    {"item": "book", "price": 10},
    {"item": "pen", "price": 2}
  ]
}

YAML Alternative (35 Tokens):

customer: John Doe
status: active
purchases:
  - item: book
    price: 10
  - item: pen
    price: 2

3. Markdown: The Native Language of LLMs

Most LLMs (especially GPT-4 and Llama) were trained on the open internet (Wikipedia, Reddit, GitHub). These sources are heavily formatted in Markdown.

Markdown is inherently token-efficient because it uses minimal characters for structure.

  • Use # instead of <title>
  • Use - instead of <li>

Efficiency Tip: If you are feeding a list of "Features" to a model, don't use JSON. Use a simple Markdown unordered list.


4. XML: Specialized Context Wrapping

Anthropic (makers of Claude) specifically recommend using XML tags (<context></context>) for their models.

Why? XML provides unambiguous "Bookends" for data. If you have a 10,000-token document, wrapping it in <doc> and </doc> costs only 4 tokens, but it allows the attention mechanism to isolate that document perfectly from your instructions.

graph LR
    System[Instructions] --> XML_START[<doc id='1'>]
    XML_START --> DATA[The Big Document...]
    DATA --> XML_END[</doc>]
    XML_END --> Query[User Question]

5. Avoiding the "Whitespace Trap"

In your backend code, ensure you are not "Prettifying" your JSON/XML before sending it to the LLM.

Bad (Pretty):

{
    "id": 1,
    "msg": "Hello"
}

Good (Minified): {"id":1,"msg":"Hello"}

The literal space characters and newlines in "Pretty" JSON are counted as tokens. In a high-volume app, "Minification" can save you $100/month just by deleting white space.


6. Implementation: The Format Chooser (Python)

Python Code: Comparing Formats

import tiktoken
import json
import yaml

enc = tiktoken.get_encoding("cl100k_base")

data = {"title": "The Hobbit", "author": "Tolkien", "year": 1937}

json_tokens = len(enc.encode(json.dumps(data)))
yaml_tokens = len(enc.encode(yaml.dump(data)))
text_tokens = len(enc.encode(f"Title: {data['title']}, Author: {data['author']}, Year: {data['year']}"))

print(f"JSON: {json_tokens} | YAML: {yaml_tokens} | Plain Text: {text_tokens}")

7. Summary and Key Takeaways

  1. JSON is for machines, YAML is for tokens: If you don't need strict JSON parsing on the response, use YAML for input context.
  2. Markdown by Default: Use it for all unstructured text and simple lists.
  3. XML for Context: Use tags for multi-document RAG (especially for Claude).
  4. Minify Every Prompt: Strip extra newlines and spaces before calling the API.

In the next lesson, Preamble and Postamble Suppression, we learn how to stop models from talking about themselves.


Exercise: The Token Weigh-in

  1. Take a list of 10 usernames and their primary emails.
  2. Format them in three ways:
    • CSV
    • JSON
    • Markdown Table
  3. Weigh the tokens.
  4. If you had to process 1,000,000 users, which format would save your company the most money?
  • (Hint: Usually a compact CSV or Markdown list is the winner).

Congratulations on completing Module 4 Lesson 4! You are now a structural specialist.

Subscribe to our newsletter

Get the latest posts delivered right to your inbox.

Subscribe on LinkedIn