API for Humans vs AI Agents
Most APIs are designed for human consumption, which relies on implicit dependencies like documentation, trial and error, training, and technical support. Real-time AI agents (like Generative Agent) must perform API calls in one shot. They rely exclusively on specifications to determine how to call the API. We recommend using OpenAPI specifications for this purpose.
Treat the API’s specification not as documentation about the API, but as an integral, machine-readable part of the API itself.
HTTP API and LLM Tools
In virtually all AI Agent use cases, middleware (such as MCP) handles transforming LLM requests to API calls. This need is critical as LLMs work best with targeted, RPC-like tool calls (such asupdate_address
or refund_order
with parameters for the ID or fields to update). They are not as successful with constructing actual HTTP API requests (such as a call_api
function with parameters for verb, path, body, etc.).
When you decide to update your APIs for AI agents, there are two approaches to consider:
- Expose an API that is entirely focused on AI agents and let human developers work around it
- Your APIs will be focused on RPC-like tool calls
- Expose an API that is designed for both human developers and AI agents
- Ideally this is a RESTful API where the middleware can convert RESTful actions into RPC-like tool calls
HTTP API Design
Guidance and best practices for designing HTTP APIs that will be used by AI agents.
Tool Call Design
Guidance and best practices for designing tool calls that will be used by AI agents.
API Design Principles
Being consistent and using common schema patterns makes it easier for AI agents. We show examples using OpenAPI specifications.There are many good general API design guides out there such as Google’s API design guide.
Simplify Field Names
Use clear, descriptive, and simple field names. Avoid abbreviations that aren’t immediately clear.Use Intuitive, Human-Friendly Concepts
Design APIs that work for both humans and AI agents by balancing technical primitives with human-friendly concepts. Esoteric terms like “record”, “details”, or “session” are difficult for humans to understand without context—AI agents will struggle even more. Instead, design around resources that can express their current state and requirements clearly. This makes it easier for AI agents to understand what data is needed and how to provide it. This will make your APIs easier to expand and map to RPC-like tool calls that work well with Generative Agent.Be Verbose in Descriptions
Use the summary and description fields in OpenAPI for each endpoint and parameter. Describe the purpose in clear, simple language. Generative Agent can use this text to map a user’s natural language request (e.g., “find a customer by their email”) to the correct API call.Define Strict Schemas
Meticulously define the data structures for every request and response using JSON Schema within the OpenAPI document. Specify data types (string
, integer
), formats (date-time
, email
), and constraints (minLength
, maximum
). This is how an agent knows exactly what to send and how to parse the result.
Standardize Data Formats
Use universal standards for common data types:- Timestamps: ISO 8601 format (
2024-01-15T10:30:00Z
) - Currency: ISO 4217 codes (
USD
,EUR
) - Phone numbers: E.164 format (
+1234567890
)
Consistent Naming Conventions
Use a predictable naming schema for your API endpoints such as plural nouns for collections (/users
, /orders
). Maintain consistent case style (e.g., camelCase
for JSON properties).
Provide Rich Context
Don’t just return an ID. If you have a userId, also include a userName or userEmail. Design API responses to be self-contained and descriptive.Ensure Idempotency
For operations likePUT
and DELETE
, ensure they can be called multiple times with the same input and produce the same result. Generative Agent may need to retry operations, and idempotency makes this safe.
Design Structured Errors
Provide detailed error information that AI systems can understand. Provide a structured JSON error payload that an agent can parse.Cursor Pagination
Implement cursor-based pagination for list endpoints. Include metadata for navigating to next/previous pages. Provide total count when possible.Essential OpenAPI Elements
We recommend using OpenAPI specifications for your APIs. Here are some essential elements to consider:Descriptive Documentation
- Use clear
summary
anddescription
fields for each endpoint - Describe parameters and responses in simple language
- Include examples that show expected usage
Unique Operation IDs
- Provide descriptive
operationId
values (e.g.,getUserById
,createOrder
) - Use consistent naming conventions across your API
- Avoid generic names like
getData
orprocessRequest
Tool Call Design Best Practices
Granular Tool Names for Specific Use Cases
Generative Agent and other LLMs work best with targeted, RPC-like tool calls. If you want to have Generative Agent update a user’s address, instead of directly calling a genericupdate_user
tool, you can create a tool called update_user_address
.
Start Simple, Expand as NeededYou can start with basic CRUD tools based on your existing APIs, but expand on them to be more specific to the task. A basic exposing of RESTful API results in tools like
create_user
, update_user
which may be okay for some use cases, but often you’ll end up exposing specific tools for a specific task.Flatten Complex Structures
Often a good RESTful design has logical nesting for different entities within a given resource. But LLMs can have a hard time understanding deeply nested objects. Flatten nested objects into a single level. Avoid deep nesting of objects.Focus on Essential Data
- Remove optional or additional fields that are not directly needed for the specific task
- Only expose the specific fields from the API that are needed for the specific task
- The more fields, the more likely Generative Agent may make a mistake in providing unnecessary information or getting confused by a large API response
Schema Consistency Across Tools
When exposing multiple tools to Generative Agent, ensure you expose consistent naming to Generative Agent regardless of the underlying system. If you have a person called auser
in one API and an account
in another, it may confuse Generative Agent. You should use a consistent naming convention across all the APIs and ensure you use that same naming in the task instructions.