Sub topic 6
Writing an llms.txt file
This chapter assumes you know how a site is served over HTTP, can read and write Markdown, and have seen a robots.txt file. By the end you will be able to write a valid llms.txt, check it with a script, and avoid the common mistakes. The chapter covers the file's format. Whether any AI system reads the file is a separate question, covered in a later chapter.
What the format is trying to do
llms.txt is a proposal. It is not an IETF or W3C standard. It was suggested as a plain Markdown file at the root of a site, at /llms.txt. A language model or an agent that has only a small context window can read it to find the pages that matter.
The file is meant to be read by a model, and by a person too. So it is Markdown, not XML or JSON. It lists links, each with a short note. The links should point to clean pages, ideally Markdown versions of your documentation.
Think of it as a curated table of contents. A sitemap lists every URL. An llms.txt lists the few URLs that someone with limited time should read first, and says why.
The structure
The proposal defines an order of elements. Only the first is required.
| Element | Markdown form | Required | Purpose |
|---|---|---|---|
| Title | `# Name` (H1) | Yes | Names the project or site |
| Summary | `> text` (blockquote) | No | A short description with the key facts |
| Details | Paragraphs or lists, no headings | No | Notes on how to read the files |
| File lists | `## Section` (H2) with a list of links | No | Groups of links to pages |
| Optional section | `## Optional` | No | Links that can be skipped when context is short |
Each list item follows one pattern: a Markdown link, then optionally a colon and a note.
- [Link title](https://example.com/page.md): Notes about the pageHere is a complete file for a fictional library.
# Example Docs
> Documentation for the Example library, a Python client for the Example API.
The quickstart is the best place to begin. All links point to plain Markdown pages.
## Docs
- [Quickstart](https://example.com/docs/quickstart.md): Install the library and make a first request
- [API reference](https://example.com/docs/api.md): Endpoints, parameters and error codes
## Optional
- [Changelog](https://example.com/changelog.md): Release historyNotice what the file leaves out. It has no marketing text, no navigation menu, and no list of every page. Each link has a note that tells a reader what the page contains.
> The Markdown rules above use no block quotes in this chapter's own text. The > line in the example is part of the llms.txt format, not a formatting choice.
Rules for each part
The title
Use one H1 only. Name the product or site, not a slogan. A reader who sees only this line should know whose file it is.
The summary
Write one or two sentences. Put in the facts a model needs to interpret the rest: what the product is, what language or platform it targets, and any limit that is easy to get wrong. For example, "Supports Python 3.9 and later" is more useful than "A powerful, flexible client".
The details block
This block sits between the summary and the first H2. It may hold paragraphs and lists, but it must not contain headings. Use it for guidance such as "All links point to plain Markdown pages" or "The v1 API is deprecated; use v2".
The link sections
Each H2 names a group, such as Docs, Guides or Examples. Keep groups small. Twenty well-described links work better than two hundred. Use absolute URLs, so the file still works when it is copied elsewhere.
The Optional section
The name Optional has a special meaning in the proposal. A tool that must fit the file into a small context can drop that section and keep the rest. Put secondary material there: changelogs, historical notes, long background pages.
> Do not put the one page a reader cannot do without under Optional.
Link targets matter
A link in llms.txt can point to an ordinary HTML page. The proposal recommends pointing to Markdown versions, usually by adding .md to the page URL. HTML pages carry navigation, scripts and layout that waste tokens and may hide the text. The next chapter covers these Markdown variants.
Whatever you choose, check that every link returns a 200 status, that the content type is sensible, and that the page does not require a login. A link that redirects to a login page tells a model nothing.
Serving the file
Place the file at https://yoursite.example/llms.txt. A few practical points:
- Serve it as
text/plainortext/markdown, with UTF-8 encoding. - Make sure it returns 200 with no redirect chain and no authentication.
- Do not block it in robots.txt. A crawler that obeys robots.txt will not fetch a file you disallowed.
- Generate it from the same source as your docs, so it does not go stale. A stale llms.txt that lists deleted pages is worse than none.
- Keep it small. If it grows past a few thousand words, you are probably listing too much.
For a site with subsections, the proposal allows the file at other paths too, such as /docs/llms.txt. Treat the root file as the main one.
A script that checks your file
You can lint the file before you publish it. This script checks the title, the summary, the link syntax and the Optional section. It uses only the standard library.
import re
import sys
LINK_RE = re.compile(r"^- \[(?P<title>[^\]]+)\]\((?P<url>https?://[^)\s]+)\)(?::\s*(?P<note>.+))?$")
def lint(text):
problems = []
lines = text.splitlines()
h1s = [i for i, line in enumerate(lines) if re.match(r"^# \S", line)]
if len(h1s) != 1:
problems.append(f"expected exactly one H1, found {len(h1s)}")
elif h1s[0] != 0:
problems.append("the H1 must be the first line")
if len(lines) > 1 and not any(line.startswith("> ") for line in lines[1:5]):
problems.append("no summary blockquote near the top")
section = None
seen_sections = []
for number, line in enumerate(lines, start=1):
if line.startswith("## "):
section = line[3:].strip()
seen_sections.append(section)
elif line.startswith("- "):
match = LINK_RE.match(line)
if not match:
problems.append(f"line {number}: not a valid link item")
elif not match.group("note"):
problems.append(f"line {number}: link has no note")
if section is None:
problems.append(f"line {number}: link appears before any H2 section")
if not seen_sections:
problems.append("no H2 sections found")
if seen_sections and seen_sections[-1] != "Optional":
problems.append("Optional section, if present, should come last")
return problems
if __name__ == "__main__":
content = open(sys.argv[1], encoding="utf-8").read()
issues = lint(content)
for issue in issues:
print("WARN", issue)
print("OK" if not issues else f"{len(issues)} issue(s)")Save it as lint_llms.py and run python lint_llms.py llms.txt. The script only checks structure. It does not fetch the links, so add a status check for each URL if you want to catch dead pages.
A diagram of how the pieces fit
Common mistakes
- Listing everything. A dump of every URL duplicates the sitemap and hides the pages that matter.
- Empty notes. A link titled "Page 1" with no note gives a reader nothing to decide on.
- Marketing language. Words such as "powerful" or "best-in-class" do not help a model choose a page.
- Relative URLs. They break when the file is copied or cached elsewhere.
- Links to HTML pages full of navigation. Prefer clean Markdown targets.
- Putting essentials under Optional. A tool may drop that section.
- Treating the file as a control. It cannot block or permit anything. Use robots.txt for crawler preferences.
- Expecting a ranking boost. Writing the file does not promise citations or better placement in AI answers.
Keeping the file honest
Review the file whenever you restructure the site. Add a check to your build that fails when a listed URL returns an error. Keep the summary factual and update it when versions change. Later chapters show how to see in your access logs whether anyone requests the file at all, which is the real test of whether the effort pays off.
Exercise
A teammate's llms.txt has two H1 headings, relative links such as /docs/api, and the page that explains authentication listed under ## Optional. Name the three problems and say how to fix each.
Show answer
First, the file must have exactly one H1 title, so remove or demote the second one to an H2 section. Second, relative links break when the file is copied or cached, so change them to absolute URLs such as https://example.com/docs/api.md. Third, a tool with a small context may drop the Optional section, so move the authentication page into a main section such as ## Docs and keep only secondary material under Optional.
Exercise
Write a short llms.txt for a site called "Trail Notes", a blog about hiking in Scotland. It needs a title, a one-sentence summary, one main section with two links, and an Optional section with one link. Every link needs a note.
Show answer
One valid answer: # Trail Notes > A blog with route guides and gear advice for hiking in Scotland. ## Guides - [Ben Nevis route guide](https://example.com/guides/ben-nevis.md): Path options, timings and safety notes - [Winter gear list](https://example.com/guides/winter-gear.md): Clothing and equipment for snow conditions ## Optional - [Archive](https://example.com/archive.md): Older posts, listed by year