Esc

    ↑ ↓ to moveEnter to openEsc to close

    Guide

    Serving Markdown to Agents

    How to give AI agents a clean markdown copy of every page, with the PHP and nginx that do it on this site.

    Every page on this site also comes as markdown, to any agent that asks for it. It took a couple of small functions and one header, and this is how it works.

    Why Bother

    When an agent reads a web page, it gets everything, from the navigation and scripts to the footer, with your content somewhere in the middle. All of that goes into its context window, which is limited and, for the person paying for it, not free.

    A markdown version drops everything but the content. Cloudflare, which now offers to convert pages to markdown at its edge, tried it on its own announcement post and went from 16,180 tokens of HTML to 3,150 of markdown, about 80% fewer. Less noise also means less for the model to misread.

    I wrote about why this matters more every month in An Audience of Agents, after Cloudflare's CEO said AI agents now make up more of the web's traffic than people do. If a growing share of your readers are software, giving them a clean copy is just good hosting.

    3 Ways an Agent Finds Markdown

    There's no single standard, so I'd support all 3 of these, since each one catches different agents:

    MethodHow it worksWho uses it
    Content negotiationThe agent sends Accept: text/markdown and gets markdown from the normal URLClaude Code and OpenCode, according to Cloudflare
    .md twinsEvery page also lives at a URL ending in .mdSuggested by the llms.txt proposal, and easy for any agent to guess
    A link in the head<link rel="alternate" type="text/markdown" href="..."> points at the twinAny agent that reads the HTML first, and also suggested by llms.txt

    The llms.txt proposal puts its twins at the page's address with .md added, so a page at /about/ would have one at /about/index.md. This site uses /about.md instead, which is shorter to guess, and the head link points at whichever one you pick.

    My llms.txt guide covers the file that tells agents the markdown exists. This guide is about serving it.

    Content Negotiation

    HTTP has always let a client say which formats it prefers, with the Accept header. A browser sends something like this:

    Text
    Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
    

    An agent that wants markdown puts it first, with HTML as a fallback:

    Text
    Accept: text/markdown, text/html;q=0.9, */*;q=0.8
    

    Each type can carry a q weight from 0 to 1, and without one it's 1 (RFC 9110, the HTTP spec). So the server's job is to work out whether the client ranks text/markdown above text/html, and answer with whichever wins.

    The tempting shortcut is str_contains($accept, 'text/markdown'), but that sends markdown to a client that says text/markdown;q=0.1, text/html, which is a client asking for HTML. Here's a simplified version of the function this site uses, from lib/markdown-views.php:

    PHP
    function prefersMarkdown(string $accept): bool
    {
        // Per type: [q, how specific the match was, minus its position in the list].
        $best = ['text/markdown' => [0.0, 0, 0], 'text/html' => [0.0, 0, 0]];
        foreach (explode(',', $accept) as $position => $range) {
            $params = explode(';', $range);
            $type = strtolower(trim(array_shift($params)));
            $q = 1.0;
            foreach ($params as $param) {
                [$name, $value] = array_map('trim', explode('=', $param, 2) + [1 => '']);
                if (strtolower($name) === 'q') {
                    $q = (float) $value;
                }
            }
            foreach ($best as $candidate => [, $specificity]) {
                $match = match ($type) {
                    $candidate => 3,
                    'text/*' => 2,
                    '*/*' => 1,
                    default => 0,
                };
                if ($match > $specificity) {
                    $best[$candidate] = [$q, $match, -$position];
                }
            }
        }
        return $best['text/markdown'][0] > 0 && $best['text/markdown'] > $best['text/html'];
    }
    

    For each of the 2 types, it finds the most specific range in the header that covers it (an exact match beats text/*, which beats */*) and keeps that range's weight. Then it compares them. PHP compares arrays element by element, so the higher q wins, then the more specific match, then whichever the client listed first.

    That last tiebreak matters. text/markdown, text/html gives both types a weight of 1 with an exact match, and the client listed markdown first, so it gets markdown. A header with no weights at all is perfectly valid, and the HTTP spec leaves a tie up to the server, so a server that breaks it toward HTML would hand that client HTML. A browser's */*;q=0.8 covers markdown too, but text/html at 1 beats it, so browsers always get HTML.

    When the header is missing or says only */*, send HTML. The real version also ignores malformed q values and only answers markdown to GET and HEAD requests.

    The Vary Header

    This is the part that's easy to forget. Once one URL can answer with two different bodies depending on a request header, you have to tell every cache in between, which is what Vary is for:

    Text
    Vary: Accept
    

    Without it, a CDN or a browser cache can store the markdown an agent asked for and hand it to the next person, who sees a page of raw markdown. Or it can do the opposite and keep serving HTML to agents.

    Send it on both versions, the HTML and the markdown. A cache decides from whichever response it stored first. Check your CDN as well. Cloudflare only started honoring Vary in its Cache Rules in July 2026, and only when you turn it on in a cache rule, so if your CDN caches HTML pages, make sure it keys on Accept or doesn't cache those pages at all.

    Routing It in PHP

    Here's how this site's router (lib/router.php) puts it together, trimmed down:

    PHP
    // Markdown twins: /index.md for the home page, /{slug}.md for the rest.
    if (preg_match('#^/([a-z0-9][a-z0-9-]*)\.md$#', $uri, $m)) {
        $view = markdownView($m[1] === 'index' ? '' : $m[1]);
        if ($view === null) {
            notFound(true);
        } else {
            sendMarkdown(...$view);
        }
        return;
    }
    
    // Pages and articles come as HTML, or as markdown to a client that prefers it.
    header('Vary: Accept');
    if (wantsMarkdown()) {
        $view = markdownView($path);
        if ($view === null) {
            notFound();
        } else {
            sendMarkdown(...$view);
        }
        return;
    }
    
    // ...render the HTML page as usual
    

    markdownView() returns the markdown for a path along with the URL of its HTML version, and sendMarkdown() sends it:

    PHP
    function sendMarkdown(string $markdown, ?string $canonical = null): void
    {
        header('Content-Type: text/markdown; charset=utf-8');
        if ($canonical !== null) {
            header('Link: <' . $canonical . '>; rel="canonical"', false);
        }
        echo $markdown;
    }
    

    The charset isn't optional, either. RFC 7763, which registers text/markdown, makes it a required parameter.

    The canonical Link header matters for the twins. Without it, a search engine that finds /about.md might index it as a second copy of /about/. With it, the HTML page stays the one that counts, and Google accepts a canonical sent this way for files that can't carry a <link> tag.

    A 404 follows the same rule, so an agent asking for a missing page gets a short markdown note pointing to the article list and llms.txt, instead of an HTML error page.

    What Goes in the Markdown

    For an article, the markdown is almost free, because the article is already written in markdown. This site sends the title as an H1, then the subheading, a byline line with the date and the page's URL, then the intro and the body exactly as written. Root-relative links like /tools/ get the domain added, since an agent reading the markdown on its own has no page to resolve them against.

    Other pages need a little more thought. The home page's markdown lists the latest articles and guides as links with a line about each. The contact page says the form needs a browser, since an agent can't fill it in. Anything the HTML version shows that a reader would miss should get a sentence.

    Linking the Twin

    The last piece is telling agents the twin exists. In the head:

    HTML
    <link rel="alternate" type="text/markdown" href="https://joncphillips.com/about.md">
    

    This site sends the same thing as an HTTP header too, so an agent that only makes a HEAD request still finds it:

    Text
    Link: <https://joncphillips.com/about.md>; rel="alternate"; type="text/markdown"
    

    The nginx Side

    On this site nginx does nothing special. The .md URLs aren't files, so the usual try_files hands them to PHP:

    Nginx
    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }
    

    The one thing to watch is the rule that adds trailing slashes. This site's only matches paths without a dot, so /about.md is left alone instead of being redirected to /about.md/:

    Nginx
    if ($request_uri ~* "^(/[^/.?](?:[^.?]*[^/])?)$") {
        return 301 $1/;
    }
    

    A static site

    If your site is static and your build writes an index.md beside each index.html, nginx can do the negotiation itself. This is a generic example, not this site's config:

    Nginx
    map $http_accept $wants_markdown {
        default                 0;
        "~*^\s*text/markdown"   1;
    }
    
    server {
        # ...
        location / {
            add_header Vary Accept always;
            if ($wants_markdown) {
                rewrite ^(.*/)$ $1index.md last;
            }
            try_files $uri $uri/ =404;
        }
    
        location ~ \.md$ {
            types { }
            default_type "text/markdown; charset=utf-8";
            add_header Vary Accept always;
        }
    }
    

    The map goes in the http context. It's cruder than the PHP version, since it only sends markdown when text/markdown comes first in the header. That covers a header like the one above, though not a client that lists markdown later with a higher weight, and it ignores weights entirely, so text/markdown;q=0.1, text/html gets markdown too. A page with no index.md gets a 404 for markdown requests, so make sure your build writes one for every page.

    Test It

    These 3 curl commands cover it:

    Bash/Shell
    curl -sI https://example.com/about/ | grep -iE "content-type|vary"
    curl -sI -H "Accept: text/markdown, text/html;q=0.9, */*;q=0.8" https://example.com/about/ | grep -iE "content-type|vary"
    curl -s https://example.com/about.md | head -20
    

    The first should say text/html and the second text/markdown, both with Vary: Accept.

    If a growing share of your readers can't see your design, give them the one thing they came for.

    Then run the AI Agent Readiness Check. It asks for your page with an Accept: text/markdown header and checks that the answer is markdown with Vary: Accept. It also looks for the rel="alternate" link, and reads your llms.txt while it's there.