# llms.txt generation


<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->

``` python
from fastcore.test import test_eq
from tempfile import TemporaryDirectory
```

A package’s `skill.py` is the single source of truth for LLM
documentation: `doc(pkg.skill)` renders it in Python, and this module
renders the same file as `llms.txt` for web readers without importing
it. Skill docstrings use `###`+ headings so `# pkg` and `## Section`
stay reserved for `llms.txt`.

## Parsing `skill.py`

[`parse_skill`](https://AnswerDotAI.github.io/pyskills/llms.html#parse_skill)
reads a `skill.py` file via AST, splitting its docstring into a one-line
`summary` and `body`, and reading literal `__docs__` and `__base_url__`
assignments.

------------------------------------------------------------------------

<a
href="https://github.com/AnswerDotAI/pyskills/blob/main/pyskills/llms.py#L20"
target="_blank" style="float:right; font-size:smaller">source</a>

### parse_skill

``` python
def parse_skill(
    path:str | pathlib.Path, # Path to a `skill.py` file
)->fastcore.basics.AttrDict: # `AttrDict(summary, body, docs, base_url)`
```

*Parse `skill.py`’s docstring and literal `__docs__` / `__base_url__`
via AST without importing it*

``` python
sk = parse_skill(Path('../pyskills/skill.py'))
assert sk.summary and sk.body
sk.summary, sk.docs
```

## Formatting reference sections

[`_mod_line`](https://AnswerDotAI.github.io/pyskills/llms.html#_mod_line)
formats one module cross-reference using its AST docstring summary and
`mod_urls` entry. Pure-docstring modules without a web page, such as
`guidance.py` or `createskill.py`, emit a sibling `llms-<name>.txt`
companion file copied verbatim by Quarto.

``` python
comps = {}
ln = _mod_line('pyskills.createskill', Path('../pyskills').resolve(), 'pyskills', {}, comps)
assert '[pyskills.createskill](llms-createskill.txt):' in ln and list(comps) == ['llms-createskill.txt']
ln
```

[`_fmt_sect`](https://AnswerDotAI.github.io/pyskills/core.html#_fmt_sect)
(shared with `pyskills.core`) renders one `## Section` block, mixing
module entries and `(title, href, desc)` link tuples.

``` python
_fmt_sect('Docs', [('mod', 'pyskills.core'), ('link', 'Guide', 'guide.html.md', 'Intro')],
    partial(_mod_line, pkg_dir=Path('../pyskills').resolve(), pkg_name='pyskills', urls={'pyskills.core': 'core.html.md'}, comps={}))
```

## Building `llms.txt`

[`pkg_llms`](https://AnswerDotAI.github.io/pyskills/llms.html#pkg_llms)
assembles the title, summary blockquote, normalized `Base URL:`, skill
body, reference sections, and trailing marker.

------------------------------------------------------------------------

<a
href="https://github.com/AnswerDotAI/pyskills/blob/main/pyskills/llms.py#L51"
target="_blank" style="float:right; font-size:smaller">source</a>

### pkg_llms

``` python
def pkg_llms(
    path:str | pathlib.Path, # Package directory containing `skill.py`
    base_url:str='', # Published docs base URL (normalized to end with `/`)
    mod_urls:dict=None, # Optional `{module_name: rel_url}` for modules with web doc pages
    docs:dict=None, # Fallback docs dict (e.g. from `docs_tree`) used when `skill.py` omits `__docs__`
    summary:str='', # Fallback summary when `skill.py` is absent
    body:str='', # Fallback body (e.g. exported index markdown) when `skill.py` is absent
    marker:str=None, # Generator marker override when `skill.py` is absent
)->tuple[str, dict[str, str]]: # `(llms_txt, {companion_filename: content})`
```

*Build `llms.txt` and pure-docstring companion files from
`path/skill.py` (or `summary`/`body` fallback) without importing it*

``` python
txt,comps = pkg_llms('../pyskills', base_url='https://AnswerDotAI.github.io/pyskills',
    mod_urls={'pyskills.core': 'core.html.md', 'pyskills.llms': 'llms.html.md'}, docs={'Docs': ['pyskills.createskill']})
assert 'Base URL: https://AnswerDotAI.github.io/pyskills/' in txt
assert '- [pyskills.core](core.html.md):' in txt and list(comps) == ['llms-createskill.txt']
txt[:500]
```

## Writing and updating `llms.txt`

[`update_llms_txt`](https://AnswerDotAI.github.io/pyskills/llms.html#update_llms_txt)
writes `llms.txt` and companion `llms-*.txt` files when absent or
generated, leaves hand-written files untouched, and removes stale
generated `llms-*.txt` files when `skill.py` or a companion disappears.

------------------------------------------------------------------------

<a
href="https://github.com/AnswerDotAI/pyskills/blob/main/pyskills/llms.py#L89"
target="_blank" style="float:right; font-size:smaller">source</a>

### update_llms_txt

``` python
def update_llms_txt(
    path:str | pathlib.Path, # Package directory containing `skill.py`
    fn:str | pathlib.Path=None, # Output `llms.txt` path (defaults to `<path>/../llms.txt`)
    base_url:str='', # Published docs base URL
    mod_urls:dict=None, # Optional `{module_name: rel_url}` map
    docs:dict=None, # Fallback docs dict (also synced to `pkg/_modidx.py` when `skill.py` exists)
    *, summary:str='', # Fallback summary when `skill.py` is absent
    body:str='', # Fallback body (e.g. exported index markdown) when `skill.py` is absent
    marker:str=None, # Generator marker override when `skill.py` is absent
)->list[pathlib.Path]: # Paths written or removed (empty when unchanged)
```

*Write `llms.txt` and companion files when absent or generated; remove
stale generated files*

Create a minimal temporary package with `skill.py`, `guidance.py`, and a
stale generated `llms-api.txt` to exercise disk writes, stale cleanup,
and idempotency.

``` python
_td = TemporaryDirectory()
d = Path(_td.name)
pkg = d/'demo'
pkg.mkdir()
(pkg/'skill.py').write_text('"""Demo summary.\n\n### Usage\n\n- fast\n"""\n__docs__ = {"Docs": [("Extra", "extra.html.md", "More")]}\n')
(pkg/'guidance.py').write_text('"""Domain rules.\n\nBe careful.\n"""\n')
(d/'llms-api.txt').write_text('old\n<!-- Generated by pyskills-llms; edit demo/skill.py, not this file -->\n')
```

Running
[`update_llms_txt`](https://AnswerDotAI.github.io/pyskills/llms.html#update_llms_txt)
writes `llms.txt` and `llms-guidance.txt`, deletes the stale
`llms-api.txt`, and returns the changed paths. A second identical call
returns an empty list.

``` python
fn = d/'llms.txt'
kw = dict(base_url='https://example.com/demo')
test_eq(set(update_llms_txt(pkg, fn, **kw)), {fn, d/'llms-guidance.txt', d/'llms-api.txt'})
test_eq(update_llms_txt(pkg, fn, **kw), [])
out = fn.read_text()
assert '- [demo.guidance](llms-guidance.txt): Domain rules.' in out and '- [Extra](extra.html.md): More' in out
assert (d/'llms-guidance.txt').exists() and not (d/'llms-api.txt').exists()
out
```

Without `skill.py`, passing `summary`/`body` still renders `llms.txt`
(without writing `docs` to `_modidx.py`). Removing both cleans up
generated files, while a hand-written `llms.txt` without a generator
marker is never overwritten.

``` python
(pkg/'skill.py').unlink()
test_eq(update_llms_txt(pkg, fn, summary='Fallback summary.', body='Intro text.', docs={'Docs': [('G', 'g.html.md', '')]}, **kw), [fn])
assert '> Fallback summary.' in fn.read_text() and 'Intro text.' in fn.read_text() and not (pkg/'_modidx.py').exists()
test_eq(set(update_llms_txt(pkg, fn)), {fn, d/'llms-guidance.txt'})
assert not fn.exists() and not (d/'llms-guidance.txt').exists()
fn.write_text('# Hand written\n')
test_eq(update_llms_txt(pkg, fn), [])
assert fn.read_text() == '# Hand written\n'
_td.cleanup()
```

## Command line

`pyskills-llms` exposes
[`update_llms_txt`](https://AnswerDotAI.github.io/pyskills/llms.html#update_llms_txt)
as a CLI.

------------------------------------------------------------------------

<a
href="https://github.com/AnswerDotAI/pyskills/blob/main/pyskills/llms.py#L118"
target="_blank" style="float:right; font-size:smaller">source</a>

### llms_cli

``` python
def llms_cli(
    path:str, # Package directory containing `skill.py`
    out:str=None, # Output `llms.txt` path (defaults to `<path>/../llms.txt`)
    base_url:str='', # Published docs base URL
):
```

*Generate `llms.txt` and companion files from `path/skill.py`*
