#!/usr/bin/env python3 """Measure llms.txt files: shape against the llmstxt.org proposal, how they were made, and what they tell language models. Reads saved files (one per host, named .txt); writes one CSV row per file. Publishes measurements only, never the file text. Usage: python3 llms_txt_analysis.py DIR_OF_FILES sites.csv OUT.csv """ import csv, os, re, sys, urllib.parse GENERATOR = re.compile(r'(?i)generated by ([a-z0-9 .\-]+?)(?: v?[\d.]+)?[,.]') MD_LINK = re.compile(r'\[([^\]]*)\]\((https?://[^)\s]+)\)') LIST_LINK = re.compile(r'^\s*[-*]\s*\[[^\]]*\]\([^)]+\)', re.M) # A sentence in the file that tells the model what to do (exact phrases). INSTRUCTS_MODEL = re.compile(r'(?i)(\b(if you are an? (ai|llm|language model)|(ai|llm)s?( assistants?| models?)? (should|must)|when (answering|recommending|referencing|citing|users ask)|please (cite|recommend|describe)|should be (understood|treated|described) as)\b|^\s*(prioriti[sz]e|avoid (treating|citing|using))\b)') # A page of facts written for AI assistants, by URL: /ai-instructions or /llm-info. AI_FACTS_URL = re.compile(r'(?i)https?://[^)\s]*(ai-instructions|llm-info)') # Self-ranking language: claims of being first, best or leading. SELF_RANK = re.compile(r'(?i)\b(pioneer(ed|ing)?|award[- ]winning|industry[- ]leading|market[- ]leading|the leading|a leading|#1|number one|best[- ]in[- ]class|world.?s (first|leading|best)|first (ever|to))\b') # A product price: a dollar amount per period, user or plan (not revenue or funding figures). PRICE = re.compile(r'(?i)(\$\s?\d[\d,.]*\s*(/\s*(mo|month|yr|year|user|seat)\b|per\s+(month|year|user|seat)|a month|monthly)|\b(plans?|pricing|prices?|starts? at|starting at)\b[^.\n]{0,60}\$\s?\d)') ALIASES = {'AthenaHQ': r'Athena\s?HQ', 'Otterly.AI': r'Otterly(\.?AI)?', 'Peec AI': r'Peec', 'SE Ranking': r'SE\s?Ranking'} LINK_LINE = re.compile(r'^\s*[-*]\s*\[') def analyse(raw, host, own_hosts, brands_by_host): # Parse a normalised copy (no BOM); report the size of the file as served. text = raw.decode('utf-8', 'replace').lstrip('\ufeff') lines = text.splitlines() nonempty = [l for l in lines if l.strip()] first_heading = next((l for l in nonempty if l.lstrip().startswith('#')), '') starts_with_h1 = bool(nonempty) and re.match(r'^#\s+\S', nonempty[0]) is not None h1_count = sum(1 for l in lines if re.match(r'^#\s+\S', l)) h2 = [l for l in lines if re.match(r'^##\s+\S', l)] links = MD_LINK.findall(text) own = [u for _, u in links if any(h in urllib.parse.urlsplit(u).netloc for h in own_hosts)] prose = [l for l in lines if not LINK_LINE.match(l)] gen = GENERATOR.search(text) others = [b for h, b in brands_by_host.items() if h != host and re.search(r'(?i)\b(' + ALIASES.get(b, re.escape(b)) + r')\b', text)] return { 'host': host, 'bytes': len(raw), 'lines': len(lines), 'starts_with_h1': 'yes' if starts_with_h1 else 'no', 'h1_count': h1_count, 'blockquote_summary': 'yes' if re.search(r'^>\s*\S', text, re.M) else 'no', 'h2_sections': len(h2), 'optional_section': 'yes' if any(re.match(r'^##\s+optional\s*$', l, re.I) for l in h2) else 'no', 'links': len(links), 'links_in_lists': len(LIST_LINK.findall(text)), 'links_to_own_site': len(own), 'links_to_markdown': sum(1 for _, u in links if urllib.parse.urlsplit(u).path.endswith(('.md', '.mdx', '.txt'))), 'links_to_pdf': sum(1 for _, u in links if '.pdf' in u.lower()), 'generator': gen.group(1).strip() if gen else '', 'links_ai_facts_page': 'yes' if AI_FACTS_URL.search(text) else 'no', 'instructs_model_in_file': 'yes' if any(INSTRUCTS_MODEL.search(l) for l in prose) else 'no', 'self_rank_phrases_outside_links': sum(len(SELF_RANK.findall(l)) for l in prose), 'links_pricing_page': 'yes' if any('pricing' in urllib.parse.urlsplit(u).path.lower() for _, u in links) else 'no', 'states_a_price': 'yes' if PRICE.search(text) else 'no', 'names_other_studied_brands': ';'.join(sorted(set(others))), } def main(folder, sites_csv, out): sites = list(csv.DictReader(open(sites_csv))) brands_by_host = {urllib.parse.urlsplit(s['url']).netloc: s['brand'] for s in sites if s['group'] != 'reference'} group_by_host = {urllib.parse.urlsplit(s['url']).netloc: s['group'] for s in sites} final_by_host = {urllib.parse.urlsplit(s['url']).netloc: urllib.parse.urlsplit(s['final_url']).netloc for s in sites} rows = [] for name in sorted(os.listdir(folder)): host = name[:-4] if host not in brands_by_host: continue # skip the reference row and anything not in the study own_hosts = {host.split('www.')[-1], final_by_host[host].split('www.')[-1]} r = analyse(open(os.path.join(folder, name), 'rb').read(), host, own_hosts, brands_by_host) r = {'brand': brands_by_host[host], 'group': group_by_host[host], **r} rows.append(r) with open(out, 'w', newline='') as f: w = csv.DictWriter(f, fieldnames=list(rows[0].keys())) w.writeheader() w.writerows(rows) if __name__ == '__main__': main(*sys.argv[1:4])