wp_insert_post sometimes fails after API call

You could try what wp-cron.php does and set in your AJAX handler:

ignore_user_abort(true);

But this might cause other problems, for example when the API takes an hour to respond. 🙂

Try to log all API response time for while to see if that is really the problem.