
Building Efficient RAG Pipelines with Clean Markdown and JSON to Reduce LLM Token Waste
TL;DR Structure your scraped data as clean Markdown and JSON before feeding it to an LLM in a RAG pipeline. This removes boilerplate, cuts token usage by...
AlterLab handles this automatically — scrape any URL with one API call. No infrastructure required.
Try it freeTL;DR
Structure your scraped data as clean Markdown and JSON before feeding it to an LLM in a RAG pipeline. This removes boilerplate, cuts token usage by 30‑50%, and keeps the context focused on the information that actually matters for generation.
Introduction
Retrieval‑augmented generation (RAG) relies on feeding relevant snippets to a large language model. If those snippets are bloated with HTML tags, whitespace, or irrelevant metadata, the LLM spends tokens on noise instead of signal. By transforming raw scraped pages into minimal Markdown and targeted JSON, you reduce the token load, speed up inference, and lower cost—all while preserving or improving answer quality.
Why Markdown and JSON Reduce Token Waste
HTML documents often contain dozens of kilobytes of boilerplate: scripts, style attributes, navigation, and redundant tags. When you send that straight to an LLM, each character becomes a token (or part of one). Markdown replaces hierarchical tags with lightweight syntax (e.g., # Heading instead of <h1>), and JSON lets you select only the fields you need—such as title, content, and metadata—discarding the rest.
Consider a typical product page:
- Raw HTML: ~45 KB → ~11 000 tokens
- Clean Markdown: ~12 KB → ~3 000 tokens
- JSON with only title and description: ~3 KB → ~750 tokens
That’s a 70‑90% reduction in tokens sent to the model, directly cutting API costs and latency.
Structuring Scraped Data for RAG
- Fetch the page using a scraping API that handles JavaScript and anti‑bot measures.
- Convert to Markdown – either via a built‑in formatter or a library like
markdownify. - Extract a JSON schema – pick the fields that answer your likely queries (e.g.,
product_name,price,availability). - Store both representations – Markdown for semantic search (embedding models work well on plain text) and JSON for precise field‑level retrieval.
- Feed the top‑k chunks to the LLM, using the Markdown for context and JSON for structured answers when needed.
This dual‑format approach gives you the best of both worlds: semantic richness from Markdown and precision from JSON.
Step‑by‑Step: Building the Pipeline
Code Examples
Below are equivalent ways to scrape a page, convert it to Markdown, and pull out a JSON payload using AlterLab’s Python SDK and raw cURL.
from alterlab import AlterLabSync
client = AlterLabSync(api_key="YOUR_API_KEY")
result = client.scrape(
"https://example.com/product",
extraction_schema={"type": "object", "properties": {"title": {"type": "string"}, "price": {"type": "string"}, "description": {"type": "string"}}},
)
print(result["filtered_content"]) # dict matching your schemacurl -X POST https://api.alterlab.io/v1/scrape \
-H "X-API-Key: YOUR_KEY" \
-d '{
"url": "https://example.com/product",
"render_js": true,
"formats": ["markdown"]
}'Both snippets retrieve the page, ask AlterLab to return Markdown (saving you a conversion step), and then build a compact JSON object containing only the fields you need for generation. You can adjust the formats array to also include json if the API offers a pre‑built JSON extraction feature.
TryIt Block
See the transformation in action with a live example:
Try scraping this page with AlterLab and view the Markdown output
Best Practices
- Limit fields: Only include JSON properties that directly affect the answer. Every extra key adds tokens.
- Chunk wisely: Split Markdown at semantic boundaries (headings, paragraphs) so each chunk stays under ~256 tokens for embedding models.
- Cache conversions: If you scrape the same URL repeatedly, store the Markdown/JSON results to avoid re‑processing.
- Monitor token usage: Log the input token count before each LLM call; aim for a steady decrease as you refine your schema.
- Combine with metadata: Add a small JSON block at the top of each Markdown chunk (e.g.,
{"source": "product-page", "timestamp": "2024-09-01"}) to help the LLM weigh recency without bloating the prompt.
Takeaway
Turning raw scraped HTML into lean Markdown and targeted JSON is a straightforward way to shrink the token footprint of your RAG pipeline. The result is faster responses, lower LLM costs, and often better relevance because the model focuses on the signal, not the noise. Start by asking your scraping API for Markdown output, distill the data into a minimal JSON schema, and feed both to your retrieval and generation stages. You’ll see immediate savings and a cleaner workflow.
Was this article helpful?
Frequently Asked Questions
Related Articles

Akamai Bot Manager Sensor Checks: Technical Guide
Learn how Akamai Bot Manager sensor checks work, why requests fail, and how ethical scraping pipelines use browser execution, sessions, and tiered requests.
Herald Blog Service

Scheduling Recurring Scrapes with Python and Cron
Learn how to schedule recurring scrapes with Python and cron, choose safe intervals, prevent duplicate runs, handle failures, and build reliable data pipelines.
Herald Blog Service

Job Listing Aggregation with Web Scraping
Build job listing aggregation with web scraping. Learn source discovery, extraction, normalization, deduplication, and refresh design for data engineers.
Herald Blog Service
Popular Posts
Recommended

Selenium Bot Detection: Why You Get Flagged and How to Fix It

Understanding Anti-Bot Detection: Fingerprinting and Rate Limits

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

Mastering Playwright Stealth for Agentic Web Workflows

Playwright Bot Detection: What Actually Works in 2026
Newsletter
Scraping insights and API tips. No spam.
Recommended Reading

Selenium Bot Detection: Why You Get Flagged and How to Fix It

Understanding Anti-Bot Detection: Fingerprinting and Rate Limits

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

Mastering Playwright Stealth for Agentic Web Workflows

Playwright Bot Detection: What Actually Works 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.