Yahya was the son of
Khalid ibn Barmak, the first member of the family to achieve prominence in the
Abbasid court, serving as
de facto chief minister to the first Abbasid
caliph,
al-Saffah (), and then in a number of provincial governorships in
Fars,
Tabaristan, and
Mosul under
al-Mansur (). Yahya gained his first experience in administration as his father's aide. ". An illustration from the
Akhbar-i-Barmakiyan, created in
Mughal India, dated When his father was governor of Tabaristan in northern Iran (766/67–772), Yahya was appointed as his representative in
Rayy, where the Caliph's son and heir, the future
al-Mahdi (), was serving as viceroy for the eastern caliphate. While there, the two men became close, to the point that when al-Mahdi's younger son, the future
Harun al-Rashid () was born, he was nursed by Yahya's wife, Zubayda bint Munir, while al-Mahdi's wife,
al-Khayzuran, nursed Yahya's young son
al-Fadl, who had been born a few days earlier. This unique, and apparently accidental, relationship created strong bonds between them that proved crucial for the future fate of the Barmakids; by Arab custom, ties of
fosterage were equivalent to blood kinship. Already in 775, Yahya was himself appointed governor of
Adharbayjan. When his father entered his second governorship of Fars under
al-Mahdi (), Yahya once again accompanied him as his assistant. In 778, he was named as the tutor and secretary to Harun, replacing Aban ibn Sadaka, and enhancing their relationship: Yahya became a sort of father figure to the young prince, while in turn Yahya and his family attached their fortunes to Harun. When Harun was sent, as his first independent command, to lead an expedition against the
Byzantine Empire in 780, Yahya accompanied him. Yahya was placed in charge of the army's supplies, while he and the chamberlain
al-Rabi ibn Yunus (who would become a political ally of Yahya) functioned as advisors to Harun on all military decisions. The expedition was a modest success, resulting in the capture of the fortress of Samalu, further strengthening Yahya's position. When Harun was proclaimed second heir (after his older brother,
al-Hadi) and given charge of the provinces of Adharbayjan and
Arminiya, Yahya administered these on his behalf. ==Under al-Hadi==