1. Developer guides

Configure llms.txt support

Version: 2.x

Content SDK Next.js apps can serve an llms.txt file whose content is managed via SitecoreAI configuration. The SDK is responsible for exposing the content at the /llms.txt path; SitecoreAI is responsible for generating and storing the llms.txt content itself.

llms.txt is served with the text/markdown; charset=utf-8 content type, per the llms.txt specification.

Note

This feature only serves the content configured in SitecoreAI for the resolved site. It doesn't generate llms.txt content, and it doesn't aggregate content automatically.

Route handler for App Router

Use createLlmsTxtRouteHandler from @sitecore-content-sdk/nextjs/route-handler to scaffold the GET implementation. It requires the following:

  • A Sitecore client instance
  • A list of sites (SiteInfo) (commonly imported from .sitecore/sites.json)

See the following example in app/api/llms-txt/route.ts:

import { createLlmsTxtRouteHandler } from '@sitecore-content-sdk/nextjs/route-handler';
import sites from '.sitecore/sites.json';
import client from 'lib/sitecore-client';

export const { GET } = createLlmsTxtRouteHandler({
  client,
  sites,
});

You must also define a Next.js rewrite in next.config.ts so the handler responds at the canonical /llms.txt path:

const nextConfig: NextConfig = {
  ...
  rewrites: async () => {
    return [
      ...
      {
        source: '/llms.txt',
        destination: '/api/llms-txt',
        locale: false,
      },
    ];
  },
};

The handler resolves the site from the incoming Host header, the same way robots.txt and sitemap.xml route handlers do.

Caching and revalidation

By default, responses are cached for 60 seconds. To adjust or disable revalidation:

  • Pass a number (seconds) to set a new duration.
  • Pass false to skip revalidation and cache indefinitely (only do this if content is truly static).
  • Omit revalidate to use the default.

See the following example with a custom duration:

export const { GET } = createLlmsTxtRouteHandler({
  client,
  sites,
  revalidate: 90, // cache for 90 seconds
});

No content configured

When no llms.txt content is configured for the resolved site, the handler returns a 404 response with default placeholder content, so the route always returns a valid llms.txt document instead of an empty response.

Middleware for Pages Router

For Pages Router apps, use LlmsTxtMiddleware from @sitecore-content-sdk/nextjs/middleware in an API route.

See the following example in pages/api/llms-txt.ts:

import { LlmsTxtMiddleware } from '@sitecore-content-sdk/nextjs/middleware';
import scClient from 'lib/sitecore-client';
import sites from '.sitecore/sites.json';

const handler = new LlmsTxtMiddleware(scClient, sites).getHandler();

export default handler;

You must also define a rewrite in next.config.js as shown:

const nextConfig = {
  ...
  rewrites: async () => {
    return [
      ...
      // llms.txt route
      {
        source: '/llms.txt',
        destination: '/api/llms-txt',
      },
    ];
  },
};

The Pages Router middleware doesn't cache responses; each request fetches the current content from SitecoreAI.

SitecoreClient API

Both the Route Handler and the middleware use SitecoreClient.getLlmsTxt:

getLlmsTxt(siteName: string, fetchOptions?: FetchOptions): Promise<string | null>

Call this method directly if you need the llms.txt content outside of the provided Route Handler or middleware, for example in a custom route.

  • @sitecore-content-sdk/content/site exports LLMS_TXT_CONTENT_TYPE and DEFAULT_LLMS_TXT for use in custom implementations.
  • @sitecore-content-sdk/nextjs/route-handler exports createLlmsTxtRouteHandler.
  • @sitecore-content-sdk/nextjs/middleware exports LlmsTxtMiddleware.
If you have suggestions for improving this article, let us know!