Dev.to AI ๐Ÿค– Ai ๐Ÿ‘ 0 ๐Ÿ“– 4 min read

A "read this URL" tool for your AI agent that returns Markdown instead of HTML

I built the Apify actor used below, so keep that in mind. Most agent setups I've seen have some kind of "fetch this page" tool. The lazy version grabs the raw HTML and drops it into the context. It works, kind of. But y

I built the Apify actor used below, so keep that in mind.

Most agent setups I've seen have some kind of "fetch this page" tool. The lazy version grabs the raw HTML and drops it into the context. It works, kind of. But you pay for every nav menu, script tag and cookie banner in tokens, and the model has to dig the actual content out of all that.

What I wanted was simple. Give it a URL, get back the main content as Markdown, plus a title and a rough token count so the agent can decide if it's worth reading. No search step, no crawling. Just the pages I already know about.

How much smaller it gets

I ran it on the Python json module docs this morning. The raw HTML is 116,296 bytes. The Markdown that came back is 32,615 characters, about 8,150 tokens by the actor's estimate. Code blocks, tables and links survive. The sidebar, header and footer don't.

The Crawlee quick start page came back as 756 words, about 1,700 tokens. Both pages took under a second each over plain HTTP.

The call

You need an Apify account and an API token (Console, Settings, API and Integrations). Here's the whole thing with curl. The sync endpoint waits for the run and hands back the results as JSON.

curl -X POST "https://api.apify.com/v2/acts/datlab~web-page-to-markdown/run-sync-get-dataset-items" \
  -H "Authorization: Bearer $APIFY_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "startUrls": [
      {"url": "https://docs.python.org/3/library/json.html"},
      {"url": "https://crawlee.dev/js/docs/quick-start"},
      {"url": "https://httpbin.org/status/404"}
    ],
    "outputFormats": ["markdown"],
    "maxLinksPerPage": 3
  }'

That run took about 5 seconds end to end for three URLs.

What comes back

One item per URL. Here's the Crawlee one, with the Markdown cut short:

{
  "url": "https://crawlee.dev/js/docs/quick-start",
  "crawl": {
    "httpStatusCode": 200,
    "requestStatus": "handled",
    "renderedWith": "raw-http",
    "durationMs": 618
  },
  "metadata": {
    "title": "Quick Start | Crawlee for JavaScript ยท Build reliable crawlers. Fast.",
    "languageCode": "en",
    "canonicalUrl": "https://crawlee.dev/js/docs/quick-start"
  },
  "wordCount": 756,
  "tokenEstimate": 1696,
  "error": null,
  "markdown": "## Quick Start\n\nWith this short tutorial you can start scraping with Crawlee in a minute or two...",
  "links": [{ "url": "https://crawlee.dev/js", "text": "", "internal": true }]
}

The 404 doesn't blow up the run. It comes back as its own item with "requestStatus": "failed" and "error": "HTTP 404 Not Found", and it isn't billed. That matters for agents, because a bad link the model made up shouldn't kill the whole batch.

renderedWith tells you if the page needed a real browser. By default it tries plain HTTP first and only starts Chromium when the HTML looks like an empty JS shell. Most docs and blogs never need it.

Wrapping it as a tool in Python

Here's a minimal version with the apify-client package. The agent passes a list of URLs, and gets back a short summary line plus the Markdown for each page.

import os
from apify_client import ApifyClient

client = ApifyClient(os.environ["APIFY_TOKEN"])

def read_urls(urls: list[str], max_tokens_per_page: int = 6000) -> list[dict]:
    run = client.actor("datlab/web-page-to-markdown").call(
        run_input={
            "startUrls": [{"url": u} for u in urls],
            "outputFormats": ["markdown"],
            "includeLinks": False,
        }
    )
    pages = []
    for item in client.dataset(run["defaultDatasetId"]).iterate_items():
        if item.get("error"):
            pages.append({"url": item["url"], "error": item["error"]})
            continue
        md = item["markdown"]
        if item["tokenEstimate"] > max_tokens_per_page:
            # rough cut, about 4 chars per token
            md = md[: max_tokens_per_page * 4] + "\n\n[truncated]"
        pages.append({
            "url": item["url"],
            "title": item["metadata"]["title"],
            "tokens": item["tokenEstimate"],
            "markdown": md,
        })
    return pages

Register read_urls with whatever tool calling setup you use. The token estimate is the useful bit. The agent can look at titles and sizes first and only pull the full text of the pages that matter.

Or skip the code and use MCP

If you're in Claude, Cursor or VS Code, Apify runs a hosted MCP server. Pin just this actor and it shows up as a tool:

https://mcp.apify.com/?tools=datlab/web-page-to-markdown

What it costs

The first 100 pages each month are free. After that it's $0.50 per 1,000 pages over HTTP and $1.00 per 1,000 that needed a browser, plus a tiny start fee per run. Failed pages and pages blocked by robots.txt aren't charged.

What it won't do

It doesn't search the web. If you pass search terms instead of URLs it says so and stops. It doesn't follow links either. If you want a whole docs site, that's a crawler job, and I wrote about that here.

It also respects robots.txt, waits between requests to the same host, and never logs in or tries to get past a captcha or paywall. PDFs aren't supported yet.

The actor is here if you want to try it: Web Page to Markdown. If some page comes out messy, tell me the URL and I'll take a look.

๐Ÿ“ฐ Read the original article on Dev.to AI

Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes โ€” full credit and traffic to the original publisher.