optimize for agents.md

Optimize your API for AI coding agent discovery

Work through the 4-layer coding agent decision stack — training data, web search, context retrieval, and tool execution — to boost your recommendation rate.

Coding agents don't pick APIs the way developers do. A developer reads your docs, evaluates a few options, and makes a decision. A coding agent runs a multi-step process that pulls from training data, performs web searches, retrieves structured context, and ultimately executes code — often in under a minute. If your API isn't optimized for each step in that process, you lose the recommendation before any human is involved.

This guide walks through the 4-layer decision stack and what you can do at each layer to improve your position.

The 4-layer decision stack

Every recommendation from a coding agent — whether Claude Code, Cursor, or GitHub Copilot — passes through four layers:

Layer What happens What influences it
1. Training data The agent's base knowledge from pre-training Your docs, GitHub repos, Stack Overflow, blog posts
2. Web search Real-time retrieval (~20% of prompts trigger this) robots.txt, SSR, comparison pages, content freshness
3. Context retrieval Structured access to your API description llms.txt, MCP servers, Agent Skills
4. Tool execution The agent writes and runs code using your API Error messages, response typing, quick start quality

Most teams optimize only for Layer 1 — hoping their docs were crawled before the model's training cutoff — and ignore the three layers where you have direct, immediate control. That is why smaller, newer libraries sometimes beat dominant players: they win on Layers 3 and 4 while incumbents coast on training data.

Layer 1: Training data

The base model underlying each coding agent learned about your API from documentation sites, GitHub repositories, Stack Overflow, blog posts, and package registries crawled before its knowledge cutoff. If your library existed and was well-documented before the cutoff, you have a baseline advantage.

What you can do:

How to verify Layer 1

Ask Claude or ChatGPT (with web search disabled) about your API:

If the responses are inaccurate, outdated, or favor competitors, your Layer 1 presence is weak. You can't fix the past training data, but improvements you make today will be included in future model versions — and Layers 2-4 can compensate in the meantime.

Layer 2: Web search

According to Vercel's AEO tracking research, coding agents perform web searches in roughly 20% of prompts. When the agent searches, it looks for official documentation, comparison content, recent tutorials, and GitHub READMEs. If your docs don't appear in those results — or appear but contain content the agent can't read — you're invisible for those prompts.

The robots.txt problem

The most common and most damaging Layer 2 failure is blocking AI crawlers in your robots.txt. Many documentation sites added crawler blocks during a wave of concern about AI scraping, and those blocks are still in place.

What not to do:

# robots.txt — blocks AI discovery entirely
User-agent: GPTBot
Disallow: /

User-agent: ClaudeBot
Disallow: /

User-agent: anthropic-ai
Disallow: /

What to do instead:

# robots.txt — allows AI crawlers, blocks only internal pages
User-agent: GPTBot
Allow: /

User-agent: ClaudeBot
Allow: /

User-agent: anthropic-ai
Allow: /

# Block admin and internal-only paths
User-agent: *
Disallow: /admin/
Disallow: /internal/

This fix takes five minutes and has high impact. It is the first thing to check.

Other Layer 2 fixes

Ensure server-side rendering. Crawlers don't execute JavaScript. If your documentation is built with a client-side-only framework, the crawler sees an empty page. Make sure your docs render meaningful content server-side.

Create comparison pages. When an agent searches "X vs Y", it looks for pages that directly answer that query. If you don't have a page titled "[Your API] vs [Competitor]", your competitor's page defines the narrative. In Lightsage's testing of 70+ APIs, APIs with dedicated comparison pages consistently outperformed those without.

Use specific page titles. "Acme API Quick Start" is indexed and found. "Getting Started" is not distinguishable from thousands of other pages.

How to verify Layer 2

Layer 3: Context retrieval

This is where most API teams have zero presence — and where the biggest opportunities exist. Beyond web search, coding agents can access structured context about your API through llms.txt files, MCP servers, and Agent Skills.

llms.txt

An llms.txt file is a machine-readable Markdown file at your domain root that gives AI systems a curated overview of your API. When an agent encounters a relevant prompt and finds your llms.txt, it has immediate context about what your API does, when to use it, and how to make its first call.

See Add llms.txt to improve coding agent discoverability for a full implementation guide.

Time to implement: 30 minutes
Impact: High

MCP servers

The Model Context Protocol (MCP) is an open standard that lets compatible AI clients connect directly to your API. When your API has an MCP server, the agent can discover it, call your endpoints directly, and verify that responses work — before recommending anything to the developer. That changes the recommendation from "I think this might work" to "I connected to this API and it worked."

See Build an MCP server for direct coding agent access for a full implementation guide.

Time to implement: 1-2 weeks
Impact: High

Agent Skills

Agent Skills are packaged workflows that users install into compatible AI clients. A skill for your API pre-loads preferred configuration patterns, common workflows, and error-handling guidance. When a developer has your skill installed, their agent can apply that context to relevant tasks without rediscovering the workflow each time.

Time to implement: 2-4 weeks
Impact: Medium

How to verify Layer 3

Layer 4: Tool execution

This is where recommendations become reality. When a coding agent recommends your SDK, it typically writes npm install your-package, generates integration code, and sometimes executes that code to verify it works. If any step in that chain fails — install, import, API call, or error handling — the agent may switch to an alternative.

What goes wrong at Layer 4

Failure mode What happens Agent response
Install fails Package not found or dependency conflict Recommends alternative
Import fails Wrong module path or missing export Suggests competitor
API call fails with generic error "Error 401" or "Internal server error" Switches recommendation
API call fails with specific error "Invalid API key. Expected format: sk_live_xxx" Recovers and retries
Unclear response shape Agent generates code against wrong field names Fails silently or errors

Error message quality matters more than you think

In Lightsage's testing of 70+ APIs, error message specificity had the largest impact on agent error recovery rate:

Error message style Agent recovery rate
"Invalid API key format. Expected: sk_live_xxx or sk_test_xxx" 89%
"Authentication failed. Check your API key." 67%
"Error 401" 34%
"Internal server error" 12%

Specific errors let agents self-correct. Generic errors send them into debugging loops that end with a competitor recommendation.

Layer 4 improvements

Write a working quick start. The exact code the agent will generate should be in your docs as a copy-pasteable block that works without modification. Test it against Claude Code and Codex directly.

Use typed responses. TypeScript definitions, JSON Schema, and OpenAPI specs help agents understand your API's response shape and generate correct code. In Lightsage's benchmarks, APIs with typed responses had 23% higher tool call success rates.

Consistent naming. Don't make the agent guess import paths or method names. If your npm package is @yourco/api but your import is require('yourco'), agents will get it wrong.

Single-step auth. Complex multi-step authentication confuses agents. If your API requires multiple setup steps before making the first call, document each step explicitly in your quick start and in your llms.txt.

How to verify Layer 4

Ask Claude Code to implement your API's primary use case:

Add [Your API] to my Next.js app. My API key is in .env as YOURAPI_KEY.

Watch what happens: does the code compile, does the API call succeed, and if it fails, can the agent recover? Run the same test with Codex and Cursor — behavior can differ significantly across agents.

Optimization priority table

If you're starting from scratch, work through these in order. Each later layer builds on the ones below it:

Priority Action Effort Impact
1 Fix robots.txt for AI crawlers 5 min High
2 Add llms.txt with positioning 30 min High
3 Create "X vs Y" comparison pages 2-4 hours Medium-High
4 Improve error messages 1-2 days Medium
5 Build MCP server 1-2 weeks High
6 Publish an Agent Skill 2-4 weeks Medium

[!TIP]
Start with the robots.txt check before anything else. It is the most common fixable problem and the one with the fastest time-to-impact. A blocked crawler makes every other optimization invisible.

How smaller libraries can beat dominant players

The 4-layer model explains a counterintuitive pattern: smaller, newer libraries sometimes beat dominant players in coding agent recommendations.

Layer Large player advantage Small player advantage
Training data More historical content, higher coverage None
Web search Better SEO, more backlinks Can target specific long-tail queries
Context retrieval Often missing llms.txt and MCP Can ship these quickly
Tool execution More edge cases, legacy patterns Clean modern API, better DX

A smaller library with a well-crafted llms.txt, a working MCP server, and descriptive error messages can outrank a dominant player that is relying entirely on its training data presence. This is where your leverage is.