API Reference

Error Handling

Understanding and handling AMP API errors.

Error Response Format

All AMP errors follow a consistent format:

{
  "error": {
    "code": "RATE_LIMIT_EXCEEDED",
    "message": "Too many requests. Please retry after 60 seconds.",
    "details": {
      "limit": 100,
      "window": "60s",
      "retryAfter": 42
    },
    "requestId": "req_abc123",
    "timestamp": "2024-01-15T10:30:00Z"
  }
}

Error Codes

Authentication Errors

CodeDescriptionResolution
UNAUTHORIZEDInvalid or missing API keyCheck your API key
FORBIDDENAPI key lacks permissionsCheck key permissions
KEY_EXPIREDAPI key has expiredGenerate new key

Rate Limiting

CodeDescriptionResolution
RATE_LIMIT_EXCEEDEDToo many requestsImplement exponential backoff
QUOTA_EXCEEDEDMonthly quota reachedUpgrade plan or wait for reset

Validation Errors

CodeDescription
INVALID_USER_IDUser ID missing or invalid
INVALID_TASKTask description invalid
INVALID_REQUEST_IDRequest ID not found

Error Handling Strategies

Basic Try-Catch

try {
  const context = await amp.getContext({
    userId: user.id,
    task: query
  });
} catch (error) {
  if (error.code === 'RATE_LIMIT_EXCEEDED') {
    // Wait and retry
    await sleep(error.details.retryAfter * 1000);
    return retry();
  }

  if (error.code === 'UNAUTHORIZED') {
    // API key issue
    console.error('Invalid API key');
    throw error;
  }

  // Unknown error - use fallback
  return getDefaultContext();
}

Exponential Backoff

async function getContextWithRetry(params, maxRetries = 3) {
  for (let i = 0; i < maxRetries; i++) {
    try {
      return await amp.getContext(params);
    } catch (error) {
      if (error.code === 'RATE_LIMIT_EXCEEDED' && i < maxRetries - 1) {
        const delay = Math.pow(2, i) * 1000;
        await sleep(delay);
        continue;
      }
      throw error;
    }
  }
}

Graceful Degradation

let context;

try {
  context = await amp.getContext({ userId, task });
} catch (error) {
  // Log error but don't fail
  console.warn('AMP unavailable, using defaults:', error);

  // Fall back to sensible defaults
  context = {
    suggestedFraming: 'achievement',
    communicationStyle: 'conversational',
    complexity: 'break_into_steps',
    confidence: 0.5
  };
}

// Continue with context (AMP or fallback)

💡 Best Practice: Always have a fallback strategy. Your agent should work even if AMP is temporarily unavailable.

HTTP Status Codes

  • 200 - Success
  • 400 - Bad Request (validation error)
  • 401 - Unauthorized (invalid API key)
  • 403 - Forbidden (insufficient permissions)
  • 429 - Too Many Requests (rate limited)
  • 500 - Internal Server Error
  • 503 - Service Unavailable

Monitoring & Logging

// Log all errors for monitoring
amp.on('error', (error) => {
  logger.error('AMP Error', {
    code: error.code,
    message: error.message,
    requestId: error.requestId,
    userId: error.context?.userId
  });

  // Send to monitoring service
  sentry.captureException(error);
});

See Also