Establishment of rule For the first several years of his reign, Tuoba Gui had to endure constant gravitating of positions by tribal chiefs, and his position was not secure. As he gradually asserted his leadership, however, the tribal chiefs began to coalesce around him. In spring 386, Tuoba Gui set his capital at Shengle (盛樂, in modern
Hohhot,
Inner Mongolia), and was said to encourage agriculture to try to rest his people. In summer 386, he changed his title to Prince of Wei (and thus the state became known in history as Northern Wei). In fall 386, with support of
Western Yan and Liu Xian, Tuoba Gui's youngest uncle Tuoba Kuduo () made a claim to the throne, and there were many chiefs under Tuoba Gui who secretly conspired with Tuoba Kuduo, causing Tuoba Gui to panic to flee to his maternal uncles' Helan tribe, while seeking assistance from
Later Yan. Later Yan's emperor
Murong Chui sent his son
Murong Lin to assist, and together they defeated Tuoba Kuduo, who fled to and was executed by Liu Weichen. Around the new year 387, Murong Chui offered the titles of Western
Chanyu and Prince of Shanggu to Tuoba Gui, but as the title of Prince of Shanggu was not as honored of one as Prince of Wei, Tuoba Gui refused them. Despite Later Yan's assistance of him and his status as a Later Yan vassal, Tuoba Gui began to secretly consider whether he could eventually conquer Later Yan. In 388, he sent his cousin Tuoba Yi the Duke of Jiuyuan to offer tributes to Murong Chui but also to observe the Later Yan court, to consider whether he would have eventual chance of attacking it. Murong Yi concluded that Murong Chui was growing old, and that his
crown prince Murong Bao was incompetent—and that there were many potential claimants who would weaken Later Yan. This encouraged Tuoba Gui greatly in his eventual planning. In 391, Helan Na's brother Helan Rangan () plotted to kill Helan Na, and the brothers engaged in wars against each other. Tuoba Gui took this opportunity to request Later Yan to jointly attack the Helan tribe—notwithstanding Helan Na and Helan Rangan's status as his uncles. In summer 391, Murong Lin captured Helan Na and Helan Rangan, but allowed Helan Na to remain free and be in command of his tribe, while taking Helan Rangan as a prisoner. It was after this campaign that Murong Lin, seeing Tuoba Gui's abilities, suggested to Murong Chui that Tuoba Gui be detained. Murong Chui refused. In fall 391, an incident occurred that would lead to the break of relations between Later Yan and Northern Wei. That year, Tuoba Gui sent his brother Tuoba Gu to Later Yan to offer tribute, and Murong Chui's sons detained Tuoba Gu and ordered Tuoba Gui to offer horses to trade for Tuoba Gu's freedom. Tuoba Gui refused and broke off relations with Later Yan, instead entering into an alliance with Western Yan. Early in his reign, Tuoba Gui was considered to be a benevolent and merciful ruler who encouraged his people to take up agriculture. As an example of his leniency during this period, when his attendant Wuniuyu Huan () plotted with many of the tribes of Dai to arrest Tuoba Gui and surrender to Tuoba Kuduo, and the matter was found out, Tuoba Gui only executed the five people involved in the creation of the plot and did not put the others who joined them on trial. However, despite this leniency, Tuoba Gui did not compromise the law when handling the actions of his ministers.
Following hostility with Later Yan In 391, Tuoba Gui attacked
Rouran—which had been a Dai vassal but had never submitted to him—inflicting major damage on Rouran, but was unable to destroy it. Rouran would remain an annoyance, and often a menace, for the rest of Northern Wei's history. In winter 391, Liu Weichen sent his son Liu Zhilidi () to attack Northern Wei, and Tuoba Gui, despite having a much smaller army than Liu Zhilidi, defeated him, and further crossed the Yellow River to attack Liu Weichen's capital Yueba (悅拔, in modern
Ordos,
Inner Mongolia), capturing it, forcing Liu Weichen and Liu Zhilidi to flee. The next day, Liu Weichen was killed by his subordinates, and Liu Zhilidi was captured. Tuoba Gui annexed Liu Weichen's territory and people into his own, and slaughtered Liu Weichen's clan and associates regardless of age—more than 5,000 people. Liu Wenchen's youngest son
Liu Bobo, however, fled to the Xuegan () tribe, whose chief
Taixifu refused to turn him over despite Northern Wei demands, and Liu Bobo would eventually marry the daughter of
Later Qin's vassal
Moyigan of the Poduoluo (破多羅) tribe and became dependent on him. To punish Taixifu, Tuoba Gui attacked him in 393 and slaughtered much of his tribe, although Taixifu himself fled. Despite his general leniency during this period, these massacres perhaps already displayed his cruel streak. In 394, Western Yan's emperor
Murong Yong, under heavy attack by Murong Chui, sought aid from Tuoba Gui, but Tuoba Gui, while sending an army by his cousin Tuoba Qian () the Duke of Chenliu and the general Yu Yue () to distantly try to distract Later Yan, Northern Wei forces never actually engaged Later Yan, and Murong Yong was captured and killed later that year when his capital Zhangzi (長子, in modern
Changzhi,
Shanxi) fell, and Western Yan was annexed into Later Yan. In 395, Tuoba Gui led raids against Later Yan's border regions. Later that year, Murong Chui commissioned an 80,000-men army led by Murong Bao, assisted by his brothers
Murong Nong and Murong Lin, to try punish Northern Wei. Tuoba Gui, hearing about Murong Bao's army, abandoned Shengle and retreated west across the Yellow River. Murong Bao's army quickly reached the river in fall 395 and prepared to cross the river. However, by this point, Northern Wei scouts had cut off the line of communication between Murong Bao's army and the Later Yan capital Zhongshan (中山, in modern
Baoding,
Hebei), and Northern Wei had the captured Later Yan messengers declare that Murong Chui had already died, causing great disturbance in the Later Yan army. The Later Yan and Northern Wei forces stalemated across the Yellow River for 20 odd days, when Murong Lin's followers tried to start a coup and support Murong Lin as new leader, and while the coup failed, uncertainty fell on Later Yan forces. As winter came, Later Yan forces retreated and, not realizing that the Yellow River would freeze to allow Northern Wei forces to cross easily, Murong Bao left no rearguard as he retreated. Tuoba Gui personally gave chase, catching Later Yan forces unprepared at the
Battle of Canhe Slope, killing or capturing nearly the entire Later Yan army, and only Murong Bao and a number of officers escaped. Tuoba Gui selected the talented men amongst the captives to be his own officials, wanted to release the others back to Later Yan to show his mercy. However, his relative by marriage Kepin Jian () and the other generals were fearful of the captives and wanted to slaughter them. Tuoba Gui was afraid that slaughtering the captives would endanger his reputation in Later Yan, but he followed their suggestion and executed the remainder, though he would soon regret this. In 396, concerned that Northern Wei would then view Murong Bao lightly, Murong Chui personally led another expedition against Northern Wei, initially successful and killing Tuoba Qian. Tuoba Gui became concerned and considered abandoning Shengle again. However, as the army reached Canhe Slope, the soldiers cried out loud for their fathers and brothers, and Murong Chui became angry and ill, forcing the Later Yan forces to retreat to Zhongshan. He soon died, and Murong Bao succeeded him as emperor. In fall 396, Tuoba Gui led his Northern Wei troops and made a surprise attack on Bing Province (并州, modern central and northern
Shanxi), defeating Murong Nong and forcing him to flee back to Zhongshan. Tuoba Gui then advanced east, ready to attack Zhongshan. Accepting Murong Lin's suggestion, Murong Bao prepared to defend Zhongshan, leaving the Northern Wei forces free to roam over his territory, believing that Northern Wei would retreat once its forces are worn out. However, this had the effect that all of the cities' garrisons in modern
Hebei abandoned them, except for Zhongshan and two other important cities—Yecheng (in modern
Handan,
Hebei) and Xindu (信都, in modern
Hengshui,
Hebei). After making an initial attack against Zhongshan and failing, Tuoba Gui changed his tactic to establishing his rule over the other cities while leaving Zhongshan alone. In spring 397, Xindu fell. Meanwhile, however, Tuoba Gui had received news of a rebellion near his capital Shengle and offered peace—which Murong Bao rejected, and Murong Bao fought the Northern Wei forces at the
Battle of Baisi as Tuoba Gui prepared to retreat, but instead was defeated by Tuoba Gui at great loss. At this time, concerned about a coup attempt by Murong Lin, Murong Bao abandoned Zhongshan and fled to the old
Former Yan capital Longcheng (龍城, in modern
Jinzhou,
Hebei). However, the remaining garrison at Zhongshan supported Murong Bao's nephew Murong Xiang () the Duke of Kaifeng as their leader, and Tuoba Gui was unable to take Zhongshan immediately. Realizing that he had alienated the Later Yan people by having slaughtered the captives at Canhe Slope, Tuoba Gui changed his policy and tried to be gentle with the conquered Later Yan territory, and while time would be required, the territories began to abide by his rule. Murong Xiang, meanwhile, declared himself emperor, and put Tuoba Gu to death to try to show his resolve. In the fall, however, Murong Lin made a surprise attack on him, killing him and taking over Zhongshan. Murong Lin also claimed imperial title, but was unable to stand Northern Wei military pressure, and Zhongshan fell to Tuoba Gui, who was largely gentle to Zhongshan's population despite their resistance—although he slaughtered the clans of those who advocated Tuoba Gu's death. It was around this time, however, that his army suffered a serious plague that might have killed as much as half of the army and livestock. When his generals tried to persuade him to suspend the campaign, Tuoba Gui gave a response that might be quite demonstrative of his personality: :
This is the will of Heaven, and I can do nothing about it. A state can be established anywhere on earth where there are people. It only depends on how I govern it, and I am not fearful that the people would die. Around the new year 398, with Tuoba Gui ready to attack Yecheng, Yecheng's defender
Murong De abandoned it and fled south of the Yellow River, to Huatai (滑台, in modern
Anyang,
Henan), where he declared an independent
Southern Yan state. With resistance north of the Yellow River largely gone, Tuoba Gui left Tuoba Yi and Suhe Ba () as
viceroys over the former Later Yan territory, and returned to Shengle. In order to enhance communications and control, Tuoba Gui constructed a highway between Wangdu (望都, in modern
Baoding,
Hebei) and Dai (代, in modern
Zhangjiakou,
Hebei), over the
Taihang Mountains. He soon, however, recalled Tuoba Yi to be his prime minister and replaced him with his cousin Tuoba Zun () the Duke of Lueyang. In summer 398, Tuoba Gui considered restoring the old name of the state, Dai, but at the suggestion of Cui Hong (), he kept the name Wei. He moved the capital from Shengle south to Pingcheng (平城, in modern
Datong,
Shanxi), to be in greater proximity with the conquered territories. He also issued edicts to standardize weights and measures throughout the state, and to establish standard ceremonies based on Chinese and
Xianbei traditions. Around this time, Tuoba Gui issued a series of reforms to various administrative systems. He broke up the old Xianbei tribes, forcing them to disperse and no longer allowing them to control their own armies. To replace them, he created eight artificial tribes, placed in the newly declared capital region to supervise agricultural practice in the area. When he conquered Yan, he moved a large amount of their Xianbei and Han nobility and population to the new capital to establish greater control over them, and he had them cultivate the capital region's farmland. As well, he adopted a more detailed administrative system, using elements from earlier Chinese dynasties in his system. Due to his agricultural reforms, his reign saw many abundant harvests, but the
Book of Wei did not consider the harvests sufficient to sustain Tuoba Gui's frequent military campaigns and later harsh rule. Around the new year 399, Tuoba Gui declared himself emperor. He also claimed descent from the mystical
Yellow Emperor, to legitimize his reign over the
Han. During the middle period of his reign, Tuoba Gui was still considered to be a lenient emperor, despite his actions at Canhe Slope and against Liu Weichen. An example of his continued leniency would be that the generals of Murong Bao, Zhang Xiang (), Li Chen () and Murong Wen () had defected from Later Yan to Northern Wei but then back to Later Yan, yet when they were captured, Tuoba Gui pardoned them - but Zhang Xiang's son would soon after lead a rebellion against him, while Li Chen was later involved in a rebellion soon after Tuoba Gui's death. At this time, he commissioned the officials Cui Hong and Wang De () to revise the simplistic Dai legal code, and in their project abolished Later Yan laws that were considered to be cruel or difficult to follow. == Early reign as emperor ==