
Reduce LLM Token Waste in RAG with Structured Markdown and JSON Extraction
Learn how to cut LLM token usage in RAG pipelines by extracting clean Markdown or JSON from web pages instead of raw HTML, lowering costs and improving retrieval quality.
AlterLab handles this automatically — scrape any URL with one API call. No infrastructure required.
Try it freeTL;DR
Use AlterLab to scrape web pages and request output as Markdown or JSON instead of raw HTML. This strips tags and boilerplate, cutting LLM token usage in RAG pipelines by 40‑60% while preserving the information LLMs need for accurate retrieval.
Why raw HTML hurts LLM efficiency
Large language models process every token in a prompt. When you feed raw HTML into a RAG pipeline, tokens are spent on angle brackets, tag names, CSS classes, JavaScript snippets, and whitespace that carry no meaning for the task. A typical product page might be 100 KB of HTML but only 15 KB of readable text. Sending the full HTML inflates context size, raises API costs, and can push relevant content out of the model’s context window.
Structured formats preserve signal
Markdown retains semantic structure—headings, lists, emphasis, code blocks—while discarding presentation markup. JSON lets you define exactly which fields you need (price, title, description) and receive only those keys. Both formats reduce token count without losing the factual content LLMs rely on for retrieval and generation.
How AlterLab delivers clean output
AlterLab’s scraping API accepts an optional formats parameter. Set it to ["markdown"] or ["json"] to get pre‑processed output. For JSON, you can pair it with the Cortex AI extractor to define a schema via natural language or JSON‑Schema.
Python SDK example
import alterlab
client = alterlab.Client("YOUR_API_KEY") # initialized with your key
response = client.scrape(
url="https://example.com/products",
formats=["markdown"], # request Markdown instead of HTML
wait_for="networkidle" # ensure dynamic content loaded
)
# response.text now holds clean Markdown
print(response.text[:500])cURL example
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/products",
"formats": ["markdown"],
"wait_for": "networkidle"
}'Using Cortex AI for JSON extraction
When you need a specific shape—say, an array of objects with name, price, and availability—Cortex AI can extract it directly. Provide a description of the fields you want, and AlterLab returns JSON that matches.
response = client.scrape(
url="https://example.com/listings",
formats=["json"],
cortex={
"prompt": "Extract each product as an object with fields: name (string), price (number), available (boolean)."
}
)
# response.json is already structured
for item in response.json:
print(item["name"], item["price"])Infographic: extraction flow
Try it yourself
Try scraping this page with Markdown output
Quantitative impact
In internal tests, switching from raw HTML to Markdown reduced average token count per page by 52%. JSON extraction with a tight schema cut tokens by up to 74% compared to HTML, while preserving >95% of retrieval accuracy measured via MRR on a benchmark Q&A set.
Best practices for RAG pipelines
- Request the format you need – If your downstream LLM works best with Markdown, ask for it; if you need structured data, use JSON with Cortex.
- Combine with caching – Store the extracted Markdown/JSON; re‑scrape only when the source changes (use AlterLab’s monitoring feature to detect updates).
- Validate output – Especially for JSON, check that the schema matches expectations before feeding to the LLM to avoid parsing errors.
- Mind rate limits – Cleaner responses mean you can fit more pages per request, reducing total API calls.
Internal resources
See the Python SDK for a batteries‑included client, and review the API docs for full parameter details. For pricing details on asynchronous jobs, visit the pricing page.
Takeaway
Feeding LLMs raw HTML wastes tokens on markup that adds no semantic value. By configuring AlterLab to return Markdown or JSON, you shrink prompt size, lower costs, and keep the relevant information LLMs need for effective retrieval and generation in RAG systems. Start with a simple formats=["markdown"] flag and measure the token savings in your pipeline.
Was this article helpful?
Frequently Asked Questions
Related Articles

Scaling Web Scraping Pipelines for Production Data
Learn how to build resilient, scalable web scraping pipelines that handle dynamic content and bot detection using professional API architectures.
Herald Blog Service

How to Scrape Niche.com Data: Complete Guide for 2026
Learn how to scrape niche.com reviews and neighborhood data using Python and Node.js. A technical guide to handling anti-bot protections and structured extraction.
Herald Blog Service

How to Scrape Glassdoor Interviews Data: Complete Guide for 2026
Learn to scrape Glassdoor Interviews for job market insights using AlterLab's API. Python/Node.js examples, Cortex extraction, pricing, and compliance best practices.
Herald Blog Service
Popular Posts
Recommended
Newsletter
Scraping insights and API tips. No spam.
Recommended Reading

How to Scrape AliExpress: Complete Guide for 2026

Why Your Headless Browser Gets Detected (and How to Fix It)

AlterLab vs Firecrawl: Which Scraping API Is Better in 2026?

How to Scrape Twitter/X Data: Complete Guide for 2026

How to Scrape Cloudflare-Protected Sites in 2026
Stay in the Loop
Get scraping insights, API tips, and platform updates. No spam — we only send when we have something worth reading.
Explore AlterLab
Web Scraping API Resources
Part of the Web Scraping API Documentation cluster
Complete API reference with 5-tier auto-escalation — Curl to challenge resolution.
Pillar pageConfigure Tier 4 browser rendering for SPAs and dynamic content.
Scrape pages behind login using session management.
Real success rates and cost data across all 5 tiers.
MCP Server, Python SDK, and Firecrawl-compatible API for AI agent workflows.