Later in 617, Li Yuan was attacking Hedong but could not capture it quickly, and there were rumors that Eastern Tujue and a rebel ruler it supported, Liu Wuzhou the Dingyang Khan, would attack Taiyuan. At that time, Pei advocated withdrawing back to Taiyuan and defend it. Li Shimin and another son of Li Yuan, Li Jiancheng, however believed that retreat would lead to inevitable defeat and therefore successfully persuaded Li Yuan not to retreat. Instead, per Pei's suggestions, after Li Yuan subsequently defeated Sui forces at Huoyi , he left part of his army to siege Hedong, while himself crossing the Yellow River into Guanzhong . After Li Yuan captured Chang'an later that year and declared Emperor Yang's grandson the Prince of Dai emperor , he himself served as regent, and he awarded Pei with fields, a mansion, and the title of Duke of Wei.
During Emperor Gaozu's reign
In 618, Emperor Yang was killed in a coup at Jiangdu led by the general Yuwen Huaji. When news of Emperor Yang's death reached Chang'an, Li Yuan had Emperor Gong yield the throne to him, establishing Tang Dynasty as its Emperor Gaozu. When he took the throne, he stated to Pei Ji, "The person who brought me here is you, Duke." He made Pei the Right ''Shangshu Pushe'' , one of the two deputy heads of the executive bureau of the government -- a post that was considered a post for a , and bestowed Pei with great treasures. He also commissioned Pei and Liu Wenjing to revise the Sui laws. Soon, however, Pei and Liu, who were previously friends, became enemies over their conflicting suggestions, and Liu was dissatisfied that he was ranked below Pei. Subsequently, Liu was accused of using witchcraft to try to change his situation, and while Li Shimin and the officials Xiao Yu and Li Gang all argued that Liu was not committing treason and should be spared, Emperor Gaozu, at Pei's suggestion, nevertheless executed Liu.
In 619, Liu Wuzhou captured Taiyuan, forcing Emperor Gaozu's son Li Yuanji the Prince of Qi to flee. Liu then advanced south deep into Tang territory. Pei volunteered to lead the army against Liu. However, when he engaged Liu's general Song Jin'gang at Dusuo Plain , Song cut off his water supplies and then defeated him, and he lost most of his soldiers. He had to flee to Pingyang . He sent a submission to Emperor Gaozu, requesting to be punished, but Emperor Gaozu did not punish him and continued to keep him in command of the forces in the region. However, Pei, who was described to be fearful and not a capable general, was reduced to ordering the people into fortified cities, leaving the countryside for Liu to control and causing the dissatisfied people to rebel against Tang. The situation became sufficiently severe that Emperor Gaozu considered abandoning the entire modern Shanxi region. Li Shimin, however, argued that was inadvisable, and he led an army against Liu and Song, gradually fighting back and defeating Liu in 620, forcing Liu to flee to Eastern Tujue. Meanwhile, Emperor Gaozu summoned Pei back to Chang'an and put him under arrest briefly, but soon released him and continued to honor him. When Emperor Gaozu was away from Chang'an, he often had Pei in charge of the capital in his absence. In 621, when Emperor Gaozu began minting new coins, he bestowed one mint press on Pei, allowing Pei to mint his own money, and he also took a daughter of Pei's to be the wife and princess of his son Li Yuanjing the Prince of Zhao. During the years, Pei offered several times to retire, and in 626, Emperor Gaozu granted him the honorific title of ''Sikong'' and allowed him to effectively retire, but further continued to set up rotations of junior officials from the executive bureau who would attend to Pei on a daily basis.