The Battle of Nanpi happened in the first month of 205, during the period known as the end of the Han Dynasty. The battle spelled the annihilation of Yuan Tan, one of Yuan Shao's sons vying to succeed their father, by their common enemy Cao Cao, one of the serving Three Ducal Ministers. Having already dealt a major blow to another son Yuan Shang, Cao Cao's victory at Nanpi gave him uncontested control of the North China Plain, while the remnant Yuan power blocs were chased further north.
Yuan Shao, the powerful warlord of northern China, had been decisively defeated at the Battle of Guandu by Cao Cao in 200. Two years later, he died, leaving his expansive territories of Ji province, Qing province, Bing province, and You province to his three sons and a nephew: Yuan Tan, Yuan Xi, Yuan Shang, and Gao Gan. Although the eldest son was expected to succeed his father, Yuan Shang's supporters forged a document declaring the youngest son, him, as the successor. Yuan Tan, predictably resentful, rebelled against Yuan Shang after fending off Cao Cao's invasion together in 203. Yuan Tan had wanted to take the Yuan family headquarters of Ye, but Yuan Shang defeated the besiegers and chased them three hundred kilometers away to Nanpi, the capital of Bohai Commandery. The commandery was north of Qing province, where Yuan Tan had held the title of Inspector (刺史) since his father's time. Here he was joined by his former subordinates such as Wang Xiu and Guan Tong (管統), but at the same time some of them rebelled against him under Liu Xun (劉詢) - Yuan Tan's foothold in Nanpi could hardly have been stable.
In the autumn of 202, Cao Cao waged war against the Yuans' ally Liu Biao to his south, part of a strategy to let the Yuan brothers tire themselves out. Taking advantage of Cao Cao's apparent absence, Yuan Shang marched his men to Nanpi and defeated Yuan Tan there. Yuan Tan fled south into Qing province, to Pingyuan. Though it was Yuan Tan's former domain, Qing province had been slowly encroached by Cao Cao's general Zang Ba. So when Yuan Shang followed up his attack by laying siege to Pingyuan, Yuan Tan could not expect much help within his province. Yuan Tan's key advisor Guo Tu summarized the situation and proposed an unlikely alliance:
"Now, general, your domains are small and your soldiers are few, your supplies are lacking and your forces are weak. [Yuan Shang]'s coming cannot be fought off for long. Your doting servant believes it is possible to summon Duke Cao to attack [Yuan Shang]. When Duke Cao comes, he must first attack Ye, and [Yuan Shang] will return to save it. The general need only lead his soldiers to the west, and all north of Ye can be seized. If [Yuan Shang]'s army is defeated, his men will come fleeing, and they can be appropriated to resist Duke Cao. Duke Cao will be far afield, and with his food and provisions unsustainable, he shall surely flee. At that time, all to the north of state of Zhao shall be ours, and then it will be sufficient to confront Duke Cao. If you don't take this advice, all shall be in discord."
Guo Tu's proposal was a marked departure from the calls for reconciliation by Wang Xiu and Liu Biao, and though Yuan Tan was initially reluctant to act on this advice, he soon sent Xin Pi to negotiate with his father's nemesis.
When Xin Pi reached Xiping (西平), Cao Cao's staging post for his planned attack on Liu Biao, Cao Cao consulted with his advisors on how to react to the current situation. Xun Yu reasoned that Liu Biao was not ambitious enough to be a threat, so the time was right to take advantage of the Yuan family feud before the brothers reunited. Cao Cao agreed in principle, but was unsure of Yuan Tan's sincerity in an alliance. Now Xin Pi revealed his true colours, and reasoned that Cao Cao need not worry about Yuan Tan's intentions as long as the Yuan brothers remained at odds, since Cao Cao could easily triumph over their armies separately. Cao Cao, now happily convinced, turned his army north and crossed the Yellow River by the end of 203, also arranging to have one of his sons Cao Zheng (曹整) marry a daughter of Yuan Tan. Yuan Shang hurriedly led his army from Pingyuan back to Ye in response to the new alliance, while some of Yuan Shang's officers like the brothers Lü Kuang (呂曠) and Lü Xiang (呂翔) defected to Cao Cao's side. The Lü brothers were rewarded with enfeoffment, but Yuan Tan offered them seals in an attempt to persuade them to join him instead. This was regarded as Yuan Tan's first breach of trust, but Cao Cao took no action at this point.
Cao Cao made preparations to attack Ye in the year 204. Seemingly oblivious to this threat, Yuan Shang led another attempt to subdue Yuan Tan. Although Yuan Shang eventually returned with a relief force of 10,000 men when Cao Cao actually laid siege to Ye, Cao Cao had no trouble crushing his army and sent him fleeing north to Zhongshan Commandery; and in September, the city of Ye capitulated. Yuan Tan, now unchecked by his brother, marched north from Pingyuan and took the commanderies of Ganling (甘陵), Anping (安平), Bohai, and Hejian (河間), culminating in an assault in Zhongshan that drove Yuan Shang out of Ji province. At this point Cao Cao, having no more use for the alliance, accused Yuan Tan of not doing his part in the siege of Ye and only seeking to further his own ends. The accusation was followed by the cancellation of the alliance, the returning of Yuan Tan's daughter, and the movement of troops into Yuan Tan's territories at the beginning of 205.
Yuan Tan pitched camp at Longcou (龍湊) after his conquests, positioning himself between Pingyuan and Nanpi against Cao Cao. Despite his recent achievements, he had only reestablished himself in the northern part of Ji province for a few weeks at most and could not have held Longcou and Nanpi securely. When Cao Cao brought his army forth to Longcou, Yuan Tan fled the position and retreated to Nanpi at night, where he set up camp on the nearby Qing River (清河). Yuan Tan's retreat left Pingyuan undefended, and Cao Cao entered the city and established control over its counties. At this time, another of the Yuan clan's traditional allies, the Wuhuan tribe to the north under Supuyan (蘇僕延) prepared a force of 5,000 horsemen to help Yuan Tan. Cao Cao's envoy to the Wuhuan Qian Zhao (牽招) made an impressive show of force against a rival envoy at Supuyan's court and successfully convinced the Wuhuan king to disband the relief force.
In the next month, Cao Cao turned north to attack Nanpi and Yuan Tan came out to fight. The casualties were high on both sides, and Cao Cao contemplated a temporary truce. Cao Chun, the head of Cao Cao's Tiger and Leopard Cavalry (虎豹騎) force, dissuaded him from taking such a decision, apparently aware of Cao Cao's difficulties that Guo Tu had pointed out to Yuan Tan:
"Now we have trod after our enemies for a thousand miles, if we go back without victory we shall certainly lose face. Also, our army is isolated, we are a long way into enemy territory, and it will be difficult to keep this position for long. When they win, they grow vain; when we lose, we grow fearful. Using fear to fend against vanity, we shall surely be victorious."
Cao Cao agreed, ordered an aggressive strike, personally beat the drums to lead his soldiers in attack, and routed Yuan Tan's army before they had a chance to regroup. Yuan Tan himself fell off his horse while fleeing Cao Chun's men, and in a desperate bid to save himself, he turned to his pursuer and pled: "Tut! Spare me, I can make you wealthy." It was recorded that he was beheaded before he finished talking. With the death of Yuan Tan, Cao Cao captured Nanpi.
As Cao Cao entered the city, he heeded the advice of Li Fu (李孚), the newly surrendered Registrar of Ji Province (冀州主簿), to not sack the city, and sent Li Fu to announce to the populace that business should carry on as usual. However, Cao Cao was not as merciful towards Yuan Tan's chief associates — Guo Tu and the other leaders were summarily executed along with their families, while Yuan Tan's own family may have shared the same fate.
The fall of Nanpi, combined with the previous capture of Ye, ousted the Yuan clan from the Ji province and hence confirmed Cao Cao's authority over the North China Plain. The Heishan bandits confederacy leader Zhang Yan fully submitted to Cao Cao, as with most local officials who previously had served the Yuan family. Guan Tong held out in Le'an commandery (樂安) of Qing province and refused to surrender, and Cao Cao ordered Wang Xiu, who submitted to Cao Cao after arriving too late to save Yuan Tan, to kill him. Wang Xiu, however, argued that Guan Tong was being a loyal servant to a fallen state, and a pleased Cao Cao pardoned Guan Tong and took Wang Xiu onto his staff. With the two provinces of Ji and Qing under his control, Cao Cao took the advice of his advisor Guo Jia and invited local gentry leaders to join his administration. He also suppressed private feuding and vendetta, while also setting some sumptuary laws forbidding excessive funerary rites, over-extravagant tomb constructions, and the erection of steles.
Further north, Yuan Shang sought refuge with his second brother Yuan Xi, who was the Inspector of You Province. Even in the northern province of You, there were some who saw the turn of the tide. Wang Song (王松) in Zhuo Commandery and Xianyu Fu (鮮于輔) in Yuyang Commandery pledged allegiance to Cao Cao, while in Yuan Xi's headquarters he was faced with internal mutiny by Jiao Chu (焦觸) and Zhang Nan (張南). Jiao Chu succeeded in driving away the Yuan brothers, proclaimed himself the Inspector of You Province in Yuan Xi's place, and led many local officials to go over to Cao Cao. Yuan Xi and Yuan Shang fled northeast to their allies the Wuhuan, who soon attacked Xianyu Fu in Gongping (獷平; northeast of present-day Miyun) in support of the Yuans. Another source of opposition to Cao Cao was found in Zhao Du (趙犢) and Huo Nu (霍奴), local leaders apparently sympathetic to the Yuans, who attacked and killed the Grand Administrator of Zhuo and the Inspector of You. By autumn 205, after settling Ji province, Cao Cao came north to destroy Zhao Du and Huo Nu, and relieved Xianyu Fu from the Wuhuan. The Wuhuan remained a threat, but Cao Cao had no opportunity to deal with them as the recently surrendered Gao Gan in Bing province rebelled, perhaps inspired by the hope that the support from the Wuhuan may reverse the Yuan family's fortunes. Cao Cao defeated Gao Gan for good in 206, and it would be late 207 before he had decisively crushed the united forces of the Yuan family and the Wuhuan and become the uncontested power in northern China.