Ahmad was the son of al-Khasib ibn 'Abd al-Hamid, a finance officer in Egypt during the reign of Harun al-Rashid (r. 786–809). Early in Ahmad's career, he entered the service of the Turkic general Ashinas, becoming the latter's secretary ("katib"). In 838 he is mentioned as assisting Ashinas in thwarting a plot by several military officers to assassinate the caliph al-Mu'tasim (r. 833–842) during the Amorion campaign. During the reign of al-Wathiq (r. 842–847), he was one of the targets of a general crackdown against the government secretaries in 843-4, during which he and his subordinates were fined and forced to hand over one million gold dinars to the caliph. Under al-Mutawakkil (r. 847–861) he was assigned as secretary to al-Muntasir, the caliph's eldest son and heir.