section_path: Political career
section_name: Political career
paragraph_text: Political career
  
  At the age of 20, he began his political career in the chancellery of the Tunisian ruler Ibn Tafrakin with the position of Kātib al-'Alāmah (seal-bearer), which consisted of writing in fine calligraphy the typical introductory notes of official documents. In 1352, Abū Ziad, the sultan of Constantine, marched on Tunis and defeated it. Ibn Khaldūn, in any case unhappy with his respected but politically meaningless position, followed his teacher Abili to Fez. There, the Marinid sultan, Abū Inan Fares I, appointed him as a writer of royal proclamations, but Ibn Khaldūn still schemed against his employer, which, in 1357, got the 25-year-old a 22-month prison sentence. Upon the death of Abū Inan in 1358, Vizier al-Hasān ibn-Umar granted him freedom and reinstated him to his rank and offices. Ibn Khaldūn then schemed against Abū Inan's successor, Abū Salem Ibrahim III, with Abū Salem's exiled uncle, Abū Salem. When Abū Salem came to power, he gave Ibn Khaldūn a ministerial position, the first position to correspond with Ibn Khaldūn's ambitions.
  
  The treatment that Ibn Khaldun received after the fall of Abū Salem through Ibn-Amar ʻAbdullah, a friend of Ibn Khaldūn's, was not to his liking, as he received no significant official position. At the same time, Amar successfully prevented Ibn Khaldūn, whose political skills he knew well, from allying with the Abd al-Wadids in Tlemcen. Ibn Khaldūn, therefore, decided to move to Granada. He could be sure of a positive welcome there since at Fez, he had helped the Sultan of Granada, the Nasrid Muhammad V, regain power from his temporary exile. In 1364, Muhammad entrusted him with a diplomatic mission to the king of Castile, Pedro the Cruel, to endorse a peace treaty. Ibn Khaldūn successfully carried out this mission and politely declined Pedro's offer to remain at his court and have his family's Spanish possessions returned to him.
  
  In Granada, Ibn Khaldūn quickly came into competition with Muhammad's vizier, Ibn al-Khatib, who viewed the close relationship between Muhammad and Ibn Khaldūn with increasing mistrust. Ibn Khaldūn tried to shape the young Muhammad into his ideal of a wise ruler, an enterprise that Ibn al-Khatib thought foolish and a danger to peace in the country. As a result of al-Khatib's influence, Ibn Khaldūn was eventually sent back to North Africa. Al-Khatib himself was later accused by Muhammad of having unorthodox philosophical views and murdered despite an attempt by Ibn Khaldūn to intercede on behalf of his old rival.
  
  In his autobiography, Ibn Khaldūn tells little about his conflict with Ibn al-Khatib and the reasons for his departure. Orientalist Muhsin Mahdi interprets that as showing that Ibn Khaldūn later realised that he had completely misjudged Muhammad V.
  
  Back in Ifriqiya, the Hafsid sultan of Bougie, Abū ʻAbdallāh, who had been his companion in prison, received him with great enthusiasm and made Ibn Khaldūn his prime minister. Ibn Khaldūn carried out a daring mission to collect taxes among the local Berber tribes. After the death of Abū ʻAbdallāh in 1366, Ibn Khaldūn changed sides once again and allied himself with the Sultan of Tlemcen, Abū l-Abbas. A few years later, he was taken prisoner by Abu Faris Abdul Aziz, who had defeated the sultan of Tlemcen and seized the throne. He then entered a monastic establishment and occupied himself with scholastic duties until 1370. In that year, he was sent for to Tlemcen by the new sultan. After the death of ʻAbdu l-Azīz, he resided at Fez, enjoying the patronage and confidence of the regent.
  
  Ibn Khaldūn's political skills and, above all, his good relationship with the wild Berber tribes were in high demand among the North African rulers, but he had begun to tire of politics and constantly switching allegiances. In 1375, he was sent by Abū Hammu, the ʻAbdu l Wadid Sultan of Tlemcen, on a mission to the Dawadida Arabs tribes of Biskra. After his return to the West, Ibn Khaldūn sought refuge with one of the Berber tribes in the west of Algeria, in the town of Qalat Ibn Salama. He lived there for over three years under their protection, taking advantage of his seclusion to write the Muqaddimah "Prolegomena", the introduction to his planned history of the world. In Ibn Salama, however, he lacked the necessary texts to complete the work. Therefore, in 1378, he returned to his native Tunis, which had meanwhile been conquered by Abū l-Abbas, who took Ibn Khaldūn back into his service. There, he devoted himself almost exclusively to his studies and completed his history of the world. His relationship with Abū l-Abbas remained strained, as the latter questioned his loyalty. That was brought into sharp contrast after Ibn Khaldūn presented him with a copy of the completed history that omitted the usual panegyric to the ruler. Under pretence of going on the Hajj to Mecca, something for which a Muslim ruler could not simply refuse permission, Ibn Khaldūn was able to leave Tunis and to sail to Alexandria.