HTML to Markdown 4 min read

HTML vs Markdown for LLMs: Measure Tokens and Content Loss

Compare HTML and Markdown token counts with a pinned tokenizer and an equivalent-content fixture. Record lost structure instead of assuming better RAG accuracy.

(Updated: ) • 687 words

Compare HTML and Markdown for an LLM by measuring both tokens and retained information. Markdown can remove markup, but a shorter input is not automatically a better input. Keep the task, tokenizer and expected content fixed, and list what the conversion or representation discards.

Use the same information on both sides

A fair format experiment starts with the same report, not a full HTML page on one side and a summary on the other. The fixture below has a title, a source link, a table value and a code example. Its Markdown was authored as an equivalent text fixture. It does not claim to be an automatic converter or a representative sample of every website.

Count with an explicit tokenizer

Install tiktoken. The tokenizer project provides named encodings; choose the encoding for the model you actually use and record the library version. The cl100k_base fixture is reproducible, but its count should not be described as the count for every current model.

from importlib.metadata import version
import tiktoken

HTML = """<article class="report" data-test="example"><h2>Batch report</h2>
<p>Source: <a href="https://example.test/report">approved fixture</a>.</p>
<table><tr><th>Item</th><th>Count</th></tr><tr><td>Accepted</td><td>2</td></tr></table>
<pre><code>print(2)</code></pre></article>"""
MARKDOWN = """## Batch report
Source: [approved fixture](https://example.test/report).

| Item | Count |
| --- | --- |
| Accepted | 2 |

```python
print(2)
```
"""

def compare_tokens(encoding_name="cl100k_base"):
    encoding = tiktoken.get_encoding(encoding_name)
    expected = ["Batch report", "https://example.test/report", "Accepted", "print(2)"]
    return {"tokenizer_version":version("tiktoken"), "encoding":encoding_name,
        "html_tokens":len(encoding.encode(HTML)), "markdown_tokens":len(encoding.encode(MARKDOWN)),
        "missing_expected_items":[item for item in expected if item not in MARKDOWN],
        "known_losses":["class attribute", "data-test attribute", "HTML element types"],
        "scope":"manually authored equivalent text fixture; not a web converter or accuracy benchmark"}

Report the losses beside the counts

The local fixture was run with tiktoken 0.14.0 and cl100k_base: HTML measured 90 tokens and the authored Markdown measured 41. All four expected text items were present. These are observations for the exact strings shown in the example, not a promised saving on other pages, tokenizers or models.

The expected-item check looks for the title, URL, table label and code string. It is an acceptance check for this fixture, not a proof that the source meaning was fully preserved. It deliberately lists the CSS class, data-test attribute and HTML element types as lost. A task that needs those fields should retain an HTML or structured representation.

Input feature Check after conversion
Table Headers remain associated with the correct values
Code Line breaks and literal content remain intact
Link Destination URL survives, not just anchor text
Page-specific metadata Required attributes or schema are retained separately
Client-rendered text The intended content is present before comparing tokens

Measure retrieval or answer quality in a separate labeled experiment. A lower count can make a context fit within a budget, but it does not establish a percentage improvement in RAG accuracy. Count source extraction, discarded useful context and answer failures alongside the token total.

Apply the experiment to real extractions

For SearchCans Reader, compare an approved source reference with data.markdown. Use mode 1 only when rendering is needed; proxy 0 is direct, and standard Reader extraction uses two credits before documented proxy additions. Do not confuse those API credits with LLM input tokens.

Use the URL-to-Markdown implementation for extraction, the ingestion acceptance checks for content validity, and the broader token-budget method for a whole pipeline. This page owns the paired-format experiment and its loss report.

Tags:

HTML to Markdown LLM Optimization RAG Token Economy AI Agents SearchCans Reader API GEO
SearchCans Team

SearchCans Team

SERP API & Reader API Experts

The SearchCans engineering team builds high-performance search APIs serving developers worldwide. We share practical tutorials, best practices, and insights on SERP data, web scraping, RAG pipelines, and AI integration.

Ready to build with SearchCans?

Test SERP API and Reader API with 100 free credits. No credit card required.