Tahirid dynasty
Rafi was originally in the service of the Tahirid dynasty, which controlled Khurasan as virtual viceroys in the name of the Abbasid Caliphate. Tahirid (and implicitly also Abbasid) control of Khurasan was challenged in the 860s by the revolt of Ya'qub al-Saffar, who, beginning from his home province of Sistan, defeated the Tahirid armies and by 873 had seized the provincial capital, Nishapur. The failure of Ya'qub to seize Baghdad in 876, however, and his death soon after weakened the Saffarid regime that his brother Amr ibn al-Layth inherited.