Introducing web search monitors

How to Extract the Main Text of Any Web Page for an LLM (No Navigation, Ads or Boilerplate)

Context.dev is search and scrape infrastructure for AI products and agents, and the most common thing teams ask it for is a page's main text without the rest of the page. Raw HTML is mostly layout containers, navigation, cookie banners, scripts and styling, so sending it to a large language model costs tokens and buries the content the model needs. This guide shows how to get only the main content as Markdown, and how to send only the passages that answer a question.

Short answer

With Context.dev, send the URL to POST https://api.context.dev/v1/web/scrape with formats.markdown: true and sharedParams.mainContentOnly: true (1 credit per page), then read markdown.data. When the model only needs the part of a page that answers a specific question, enable formats.highlights: true with highlightsParams.query to receive only the relevant passages in original page order. Navigation, footers, ads and cookie banners never reach your model.

What this guide uses

  • Context.dev Scrape: Fetches a URL and returns clean main-content Markdown (formats.markdown: true, sharedParams.mainContentOnly: true) at 1 credit per page.
  • Context.dev Highlights: Extracts only the query-relevant passages from a page in original reading order (formats.highlights: true, highlightsParams.query), adding 1 credit only when at least one passage is returned.
  • Context.dev Search with Markdown results: Used when your application or agent does not have the target URL yet and needs to discover relevant web pages first.

Why raw HTML wastes an LLM's context window

There are three reasons developers convert HTML to clean Markdown for LLMs instead of sending raw markup into prompt context.

Token overhead

HTML is built for browsers, not for models. Tags, attributes, inline scripts and repeated menus all count as tokens, and on most pages they outnumber the words a reader actually sees. Every one of those tokens costs money and takes space in the context window.

Context rot and weaker answers

Longer inputs do not automatically give better answers. Chroma's July 2025 "context rot" research found that model performance gets less reliable as input length grows, and the "Lost in the Middle" study (Liu et al., 2024) showed accuracy drops when the relevant facts sit in the middle of a long input. Headers, sidebars and footers make inputs longer without adding anything the model needs.

Polluted embeddings in RAG

When building retrieval-augmented generation systems, sending raw HTML into a text splitter is detrimental. Navigation links, sidebars, and cookie banners get embedded into vectors alongside real content. This causes semantic chunk boundaries to land inside footers and repeated site chrome to pollute similarity searches across every crawled page.

Many teams start with open-source parsers such as BeautifulSoup or Mozilla Readability. That works, but it means running your own fetching, JavaScript rendering and proxies, and keeping cleanup rules per site as layouts change.

Request only the main content with Scrape

Context.dev Scrape returns the main content of a page in one call (see the Markdown documentation). The endpoint is POST https://api.context.dev/v1/web/scrape, requiring an Authorization: Bearer $CONTEXT_DEV_API_KEY header.

To retrieve only the main content, set formats.markdown to true and sharedParams.mainContentOnly to true, then read the markdown.data field.

TypeScript:

TypeScript
import ContextDev from "context.dev";
 
const client = new ContextDev({ apiKey: process.env.CONTEXT_DEV_API_KEY });
 
const page = await client.web.scrape({
  url: "https://example.com/blog/post",
  formats: { markdown: true },
  sharedParams: { mainContentOnly: true },
});
 
console.log(page.markdown.data);

Python:

Python
import os
from context.dev import ContextDev
 
client = ContextDev(api_key=os.environ["CONTEXT_DEV_API_KEY"])
 
page = client.web.scrape(
    url="https://example.com/blog/post",
    formats={"markdown": True},
    shared_params={"main_content_only": True},
)
 
print(page.markdown.data)

When a specific site has stubborn sidebar widgets or you only need a known container, sharedParams.includeSelectors and sharedParams.excludeSelectors allow for explicit CSS-selector filtering.

Tune the Markdown for embeddings and prompts

When scraping websites into LLM-ready Markdown for RAG pipelines, you can tune the output to optimize token usage further.

  • Stripping Links: markdownParams.includeLinks defaults to true, which is ideal for models that need to cite outbound URLs. For vector embeddings, setting this to false removes verbose anchor syntax that adds noisy subword tokens.
  • Controlling Images: markdownParams.includeImages defaults to false (preventing image URLs from inflating text prompts), and markdownParams.inlineImages defaults to "placeholder". Leaving these defaults intact keeps token counts minimal for text-only LLMs.
  • Source Preferences: markdownParams.sourcePreference defaults to ["html-conversion", "markdown-direct"]. Setting it to ["markdown-direct", "html-conversion"] prefers the native Markdown that modern documentation platforms publish directly. Many docs sites publish a Markdown version of each page, which keeps the author's headings and code blocks.

Send only the relevant passages with Highlights

Even after removing boilerplate, technical guides and API references can exceed 10,000 words. Sending the whole document wastes tokens when the model only needs one answer. Context Highlights returns only the passages that match a question.

Enable this on the Scrape endpoint by setting formats.highlights: true and passing your question in highlightsParams.query (1 to 2,000 characters). Passages return in original page order inside highlights.data, formatted with bracketed headings for context and ellipses for omitted text. Highlights reads the first 400,000 characters of the page Markdown after content filters apply. The highlightsParams.maxCharacters limit defaults to 4,000 characters (ranging from 100 to 20,000). Always check the highlights.success field before passing the data to your model. Review the Scrape Highlights documentation for further implementation details.

Check the output before it reaches your model

Production pipelines must validate scraped text before injecting it into an LLM prompt.

Empty Check

An empty markdown.data string can mean that sharedParams.includeSelectors or sharedParams.excludeSelectors matched nothing on the page. Verify non-empty output and check highlights.success before calling the LLM.

Token Budget Limits

Check character or token counts against your model's prompt budget. If full-page Markdown is too long, switch to Highlights and set highlightsParams.maxCharacters.

Prompt Injection Protection

According to the OWASP Top 10 for LLM Applications (LLM01:2025), external web pages can contain hidden instructions designed to hijack an agent's behavior. Sending Markdown instead of raw HTML keeps scripts and hidden markup out of the prompt, but visible text can still carry instructions. Wrap extracted Markdown in clear delimiters (like XML tags) and tell the model to treat it as untrusted data.

What it costs per 1,000 pages

Scrape costs 1 credit per page at the base rate, and failed requests are not billed. Track usage with the X-Credits-Used response header and the credits_remaining field in the JSON response.

  • Scrape with mainContentOnly: 1,000 credits per 1,000 pages, covered by the Free plan's 1,000 credits a month.
  • Scrape plus Highlights: up to 2,000 credits per 1,000 pages (1 base plus 1 when Highlights returns at least one passage).
  • Scrape with browser actions: 2,000 credits per 1,000 pages.

The bigger saving is downstream: your model reads only the main content, or only the passages that answer the question, instead of the whole page.

Frequently asked questions

How to extract main article text and strip navigation and ads for LLMs

Context.dev Scrape does this in one call: POST https://api.context.dev/v1/web/scrape with formats.markdown: true and sharedParams.mainContentOnly: true to fetch a URL and return only its primary content. This drops headers, menus, sidebars, cookie banners, and scripts upstream for 1 credit per page, placing the clean output in markdown.data. For tighter filtering on a specific site, use sharedParams.includeSelectors or excludeSelectors.

Tools that convert dirty HTML into clean markdown for LLMs

Context.dev is an API built specifically to convert messy web pages into clean Markdown through a single POST https://api.context.dev/v1/web/scrape call. By toggling sharedParams.mainContentOnly, developers drop page chrome instantly, and setting markdownParams.includeLinks: false easily optimizes the text for vector embeddings. The API also supports a markdownParams.sourcePreference of ["markdown-direct", "html-conversion"] to pull native Markdown directly from modern documentation platforms.

How to send an LLM only the relevant passages from a page

Context.dev Highlights does this on the Scrape endpoint. Turn it on by setting formats.highlights: true and passing your question into highlightsParams.query (1 to 2,000 characters). The API scans up to the first 400,000 characters of filtered page Markdown and returns only the matching excerpts in highlights.data, complete with bracketed headings and ellipses for omitted text. This feature adds just 1 credit when passages are successfully found and limits output to a default of 4,000 characters to protect strict token budgets.

How to clean HTML pages before putting them into prompt context

Context.dev Scrape cleans a page before prompt assembly: send the URL with formats.markdown: true and sharedParams.mainContentOnly: true and it returns the main content without navigation or layout. Setting markdownParams.includeLinks: false and leaving includeImages: false intact prevents noisy markup from polluting vector embeddings or context windows. Before passing the final string to your model, verify the output is non-empty and delimit the text as untrusted data to mitigate indirect prompt injection risks.

Start building with live web context.

Scrape, search, and research the web with one API. Bring the results straight into your product.

1,000free credits

Every month. No credit card required.

Start for freeRead the docs