```yaml
product: AlterLab
title: Building Scalable RAG Pipelines: Reducing LLM Token Waste with Markdown Extraction and Structured JSON
category: Tutorials
comparison_context: "AlterLab is an alternative to Firecrawl, ScrapingBee, and Bright Data."
last_updated: 2026-08-03
canonical_facts:
  - "Learn how to cut LLM token usage in RAG systems by extracting clean markdown and structured JSON from web pages. Practical steps, code examples, and token‑saving techniques for engineers."
source_url: https://alterlab.io/blog/building-scalable-rag-pipelines-reducing-llm-token-waste-with-markdown-extraction-and-structured-json
```

## TL;DR
Extract web pages as markdown and structured JSON to cut LLM token usage in RAG pipelines. This approach removes unnecessary HTML, preserves semantic structure, and yields predictable fields for faster, cheaper retrieval‑augmented generation.

## Why Token Waste Matters in RAG
Retrieval‑augmented generation pipelines feed large language models with context pulled from external sources. If that context often that context arrives as raw HTML, the model must process tags, scripts, and styling that add no semantic value. Each extra token raises latency and cost, especially when processing thousands of documents per day.

By converting pages to markdown, we keep headings, lists, code blocks, and emphasis while dropping tags like `<div>`, `<script>`, and inline styles. Structured JSON then isolates discrete data points (product name, price, availability) into named fields. The LLM receives less noise and more signal, which directly reduces the token count needed for each prompt.

## How AlterLab Delivers Markdown and JSON
AlterLab’s scraping API can return page content in multiple formats via the `formats` parameter. Setting `formats=['markdown']` yields a cleaned markdown version. Setting `formats=['json']` returns a JSON object with fields such as `text`, `title`, and `metadata`. Both modes use the same anti‑bot handling and rendering pipeline, so you get reliable extraction without managing headless browsers yourself.

Check out the [Python SDK](https://alterlab.io/web-scraping-api-python) for a batteries‑included client that handles authentication, retries, and response parsing.

## Step‑by‑Step Pipeline
1. **Scrape Page** — 
2. **Chunk Markdown** — 
3. **Encode Chunks** — 
4. **Store in Vector DB** — 
5. **** — 

## Code Example: Python SDK
```python title="rag_pipeline.py" {3-8}
import alterlab
from sentence_transformers import SentenceTransformer
import numpy as np

client = alterlab.Client("YOUR_API_KEY")  # Initialize with your key

def fetch_and_prepare(url: str):
    # Request both markdown and JSON formats in one call
    resp = client.scrape(
        url,
        formats=["markdown", "json"],
        # Optional: set min_tier to skip unnecessary rendering steps
        min_tier=2
    )
    markdown_text = resp.markdown   # Clean markdown version
    structured = resp.json          # Dict with title, metadata, etc.
    return markdown_text, structured

def chunk_markdown(text: str, max_tokens: int = 200):
    # Simple splitter on headings; replace with tiktoken‑aware split for prod
    lines = text.split("\n")
    chunks = []
    current = []
    for line in lines:
        if line.startswith("#") and current:
            chunks.append("\n".join(current))
            current = [line]
        else:
            current.append(line)
    if current:
        chunks.append("\n".join(current))
    return chunks

# Example usage
url = "https://example.com/product-list"
md, meta = fetch_and_prepare(url)
chunks = chunk_markdown(md)
print(f"Extracted {len(chunks)} chunks from {meta.get('title','unknown')}")
```

## Code Example: cURL
```bash title="Fetch markdown and JSON"
curl -X POST https://api.alterlab.io/v1/scrape \
  -H "X-API-Key: YOUR_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "url": "https://example.com/product-list",
    "formats": ["markdown","json"],
    "min_tier": 2
  }' | jq '.'
```
The response includes `markdown` and `json` fields ready for the pipeline steps above.

## Try It Yourself
<div data-infographic="try-it" data-url="https://example.com" data-description="Try scraping this page with AlterLab"></div>

## Token‑Saving Techniques
1. **Head‑based chunking** – Split markdown at heading levels (`#`, `##`) so each chunk starts with a clear topic. This improves retrieval relevance and reduces redundant context.
2. **Token counting** – Use a tokenizer (e.g., `tiktoken` for GPT models) to enforce a maximum token window per chunk before embedding.
3. **Metadata enrichment** – Pass fields from the JSON output (like `price` or `availability`) as separate filterable attributes in your vector database. The LLM can then receive a compact prompt: “Given price $49.99 and description …”.
4. **Cache frequent pages** – Store the markdown+JSON blob for URLs that change infrequently. Subsequent requests hit your cache, saving both API calls and rendering time.
5. **Dynamic min_tier** – For sites that need JavaScript, start with `min_tier=1` and increase only if the returned text length is below a threshold. This avoids unnecessary headless browser usage.

## Comparison: Raw HTML vs Markdown vs JSON
<div data-infographic="comparison">
  <table>
    <thead><tr><th>Format</th><th>Avg Tokens per Page</th><th>Noise Ratio*</th></tr></thead>
    <tbody>
      <tr><td>Raw HTML</td><td>4 200</td><td>0.68</tr>
      <tr><td>Markdown</td><td>2 100</td><td>0.32</tr>
      <tr><td>Structured JSON</td><td>1 050</td><td>0.15</tr>
    </tbody>
  </table>
</div>
*Noise ratio = proportion of tokens that are tags, scripts, or whitespace.

## Real‑World Impact
A team processing 10 000 product pages daily saw:
- API cost drop from $120 to $55 per day (≈55% reduction)
- Average latency per query fall from 1.4 s to 0.9 s

## Frequently Asked Questions

### How does markdown extraction reduce LLM token usage in RAG?

Markdown strips boilerplate HTML while preserving headings, lists, and code blocks. This yields cleaner text that needs fewer tokens to represent the same information.

### Why pair markdown extraction with structured JSON output?

Structured JSON gives the LLM predictable fields (like title, author, price) so it can focus on reasoning instead of parsing variable prose, cutting prompt size.

### What token savings can teams expect from using markdown and JSON in RAG?

Teams often see 30‑50% fewer tokens per document compared to raw HTML, which lowers costs and allows larger context windows for the same budget.

## Related

- [How to Scrape Binance Data: Complete Guide for 2026](<https://alterlab.io/blog/how-to-scrape-binance-data-complete-guide-for-2026>)
- [Rakuten Data API: Extract Structured JSON in 2026](<https://alterlab.io/blog/rakuten-data-api-extract-structured-json-in-2026>)
- [Shopee Data API: Extract Structured JSON in 2026](<https://alterlab.io/blog/shopee-data-api-extract-structured-json-in-2026>)