A Laravel package to serve markdown to AI agents original

by Freek Van der Herten – 3 minute read

We just published a new package called Laravel Markdown Response that lets your Laravel app serve markdown versions of your HTML pages. Your existing controllers and views stay exactly the same.

Let me walk you through what the package can do.

Why this package is needed

AI agents are consuming more and more web content. When they fetch a regular HTML page, they have to process all the navigation, scripts, styling, and other noise that has nothing to do with the actual content. That extra HTML markup translates to a lot of wasted tokens.

Markdown strips all of that away, leaving just the content itself. Fewer tokens means faster processing, lower costs, and more room in the context window for what actually matters.

Using the package

After installing the package via Composer, you add the ProvideMarkdownResponse middleware to any routes you want to make available as markdown.

use Spatie\MarkdownResponse\Middleware\ProvideMarkdownResponse;

Route::middleware(ProvideMarkdownResponse::class)->group(function () {
    Route::get('/about', [PageController::class, 'show']);
    Route::get('/posts/{post}', [PostController::class, 'show']);
});

That's it. When an AI agent visits /about, or when a user visits /about.md, they receive a clean markdown version of the page instead of HTML.

The package detects markdown requests through three mechanisms: Accept: text/markdown headers, .md URL suffixes, and known AI bot user agents like GPTBot and ClaudeBot. You don't need to configure any of this, it works out of the box.

Converted responses are cached by default, so repeated requests skip the conversion entirely. This means the performance impact on your application is minimal.

You can also convert HTML to markdown directly using the Markdown facade.

use Spatie\MarkdownResponse\Facades\Markdown;

$markdown = Markdown::convert($html);

The package also ships with testing helpers that follow Laravel's standard fake/assert pattern.

use Spatie\MarkdownResponse\Facades\Markdown;

it('converts the about page to markdown', function () {
    Markdown::fake();

    $this->get('/about.md')->assertOk();

    Markdown::assertConverted();
});

In closing

We already use this package on spatie.be to serve markdown versions of our pages to AI agents. So:

If you want to see all the ways you can serve markdown to AI agents (global middleware, controller attributes, route exclusions), check out this page in our docs.

You can find the full documentation on our documentation site and the source code on GitHub.

This is one of the many packages we've created at Spatie. If you want to support our open source work, consider picking up one of our paid products.

Join 9,500+ smart developers

Every month I share what I learn from running Spatie, building Oh Dear, and maintaining 300+ open source packages. Practical takes on Laravel, PHP, and AI that you can actually use.

No spam. Unsubscribe anytime. You can also follow me on X.

Found something interesting to share? Submit a link to the community section.