AI-friendly APIs are designed to optimize interactions for AI agents while maintaining usability for human developers. These APIs require a shift in philosophy, focusing on clear communication, context preservation, and adaptability to support intelligent agents effectively.
I want to acknowledge that I’m not an expert in API design. This document is a living resource, meant to evolve over time. Contributions, suggestions, and discussions are highly encouraged. Other established API best practices still apply! The ideas presented here are additional recommendations to enhance API usability for AI agents.
APIs should guide AI agents by clearly indicating actions, decision points, and success or failure states.
Example:
{
"status": "email_verification_required",
"recommendedNextAction": "verify_email",
"availableActions": ["resend_verification", "update_email", "cancel"],
"actionPriority": "high"
}
To support chain-of-thought reasoning, APIs must track state transitions and dependencies between operations.
Example:
{
"orderStatus": "payment_processing",
"contextChain": {
"previousStates": ["cart_created", "address_verified", "payment_initiated"],
"dependentOperations": ["inventory_check", "fraud_assessment"]
}
}
APIs should quantify confidence levels, offer alternative options, and specify triggers for human intervention.
Example:
{
"prediction": "fraudulent_transaction",
"confidence": 0.87,
"alternatives": [
{"prediction": "legitimate_transaction", "confidence": 0.12}
],
"requiresHumanReview": true
}
APIs should provide decision traces and reasoning chains to explain logic.
Example:
{
"decision": "loan_denied",
"reasoningChain": {
"primaryFactors": [
{"factor": "credit_score", "impact": "negative"},
{"factor": "debt_ratio", "impact": "negative"}
],
"decisionTrace": "credit_score_check → risk_assessment"
}
}
APIs must eliminate ambiguity with strict typing, enumerated values, and explicit relationships. Clear endpoint and parameter descriptions are essential for understanding functionality.
Example:
{
"userStatus": {
"type": "enum",
"value": "ACTIVE",
"allowedValues": ["ACTIVE", "SUSPENDED", "PENDING"],
"description": "Indicates the current status of the user account."
},
"profileCompletion": {
"type": "integer",
"description": "Percentage of profile completed by the user."
}
}
APIs should adjust responses based on the AI agent's needs and capabilities.
Example:
{
"responseComplexity": "detailed",
"resourceIntensity": {
"cpu": "medium",
"memory": "high"
}
}
APIs must include a detailed error descriptions should explain why a call failed and how to correct it in subsequent attempts.
Example:
{
"error": {
"code": "INVALID_INPUT",
"message": "User ID is missing in one of the entries.",
"remediation": "Ensure all entries include a valid 'userId' field."
}
}
APIs must include validation, dry-run options, and recovery mechanisms.
Example:
{
"operation": "bulk_user_update",
"validationEndpoint": "/validate/bulk_user_update",
"dryRunOption": "?mode=dry_run",
"rollbackStrategy": {
"available": true,
"window": "24h"
}
}
APIs should communicate resource implications such as cost, rate limits, and allocations.
Example:
{
"operationCost": {
"creditsCost": 5,
"estimatedDuration": "2s"
},
"rateLimits": {
"current": 98,
"limit": 100
}
}
APIs should specify time-related aspects like validity periods and unambiguous time zones.
Example:
{
"dataTimestamp": "2024-01-06T15:30:00Z",
"validityPeriod": {
"start": "2024-01-06T15:30:00Z",
"end": "2024-01-06T16:30:00Z"
}
}
APIs must enforce ethical AI behavior by exposing bias metrics, defining constraints, and assessing impacts.
Example:
{
"ethicalMetrics": {
"biasScore": {
"gender": 0.02
},
"fairnessMetrics": {
"equalOpportunity": 0.99
}
},
"impactAssessment": {
"privacyImpact": "medium"
}
}
- Version APIs with explicit AI-friendly indicators.
- Provide machine-readable specifications in OpenAPI format.
- Include AI-specific endpoints as necessary.
- Maintain backward compatibility.
- Document AI-specific features separately.