In 231, Zhuge Liang launched the fourth Northern Expedition and attacked Mount Qi again. He used the
wooden ox, a mechanical device he invented, to transport food supplies to the frontline. The Shu forces attacked
Tianshui Commandery and surrounded Mount Qi, which was defended by the Wei officers Jia Si (賈嗣) and Wei Ping (魏平). At Mount Qi, Zhuge Liang managed to convince
Kebineng, a
Xianbei tribal leader, to support Shu in the war against Wei. Kebineng went to
Beidi Commandery and rallied the locals to support Shu. At the time, as
Cao Zhen, the Wei grand marshal, was ill, Wei Emperor
Cao Rui ordered the general
Sima Yi to move to
Chang'an to supervise the Wei defenses in the
Guanzhong region against the Shu invasion. After making preparations for battle, Sima Yi, with
Zhang He,
Fei Yao, Dai Ling (戴陵) and
Guo Huai serving as his subordinates, led Wei forces to Yumi County (隃麋縣; east of present-day
Qianyang County, Shaanxi) and stationed there. He then left Fei Yao and Dai Ling with 4,000 troops to guard Shanggui County (上邽縣; in present-day Tianshui, Gansu), while he led the others to Mount Qi to help Jia Si and Wei Ping. When Zhuge Liang learnt of the Wei forces' approach, he split his forces into two groups. One group was to remain at Mount Qi while he led the other group to attack Shanggui County. He defeated Guo Huai, Fei Yao, and Dai Ling in battle and ordered his troops to collect the harvest in Shanggui County. In response, Sima Yi turned back from Mount Qi, headed to Shanggui County, and reached there within two days. By then, Zhuge Liang and his men had finished harvesting the wheat and were preparing to leave. Zhuge Liang encountered Sima Yi at Hanyang (漢陽) to the east of Shanggui County, but they did not engage in battle. Zhuge Liang ordered his troops to make use of the terrain and get into defensive positions; Sima Yi ordered his troops to get into formation and sent sending
Niu Jin to lead a lightly-armed cavalry detachment to Mount Qi. The standoff ended when Zhuge Liang and the Shu forces retreated to Lucheng (鹵城), took control of the hills in the north and the south, and used the river as a natural barrier. Although his subordinates repeatedly urged him to attack the enemy, Sima Yi was hesitant to do so after he sae the layout of the Shu camps in the hills. However, he eventually relented when Jia Si and Wei Ping mocked him and said that he would become a laughing stock if he refused to attack. Sima Yi then sent Zhang He to attack the Shu camp in the south, guarded by
Wang Ping, while he led the others to attack Lucheng head-on. In response, Zhuge Liang ordered
Wei Yan,
Wu Ban and
Gao Xiang to resist the enemy outside Lucheng, where the Wei forces suffered an unexpected and tremendous defeat: 3,000 soldiers were killed, and 5,000 suits of armour and 3,100 sets of hornbeam crossbows were seized by Shu forces. Even though the losses were heavy, Sima Yi still retained a sizeable army, which he led back to his camp. Despite the victory, Zhuge Liang could not press his advantage with a major offensive because of the dwindling food supply. Adverse weather prevented Shu's logistics from delivering matériel on schedule.
Li Yan, the Shu general responsible for overseeing the transportation of food supplies to the frontline, falsely claimed that Emperor
Liu Shan had ordered a withdrawal. The
Book of Jin claimed that Sima Yi launched an attack on Shu garrisons at this juncture and succeeded in capturing the Shu "covering camps." Zhuge Liang abandoned Lucheng and retreated under the cover of night, but Sima Yi pursued him and inflicted roughly 10,000 casualties on the Shu army. That account from the
Book of Jin is disputed by historians and is not included in the outstanding chronological 11th-century historical text
Zizhi Tongjian. In any case, according to
Records of the Three Kingdoms and
Zizhi Tongjian, Zhuge Liang retreated to the Shu for lack of supply but had not been defeated. and the Wei forces pursued him. The pursuit did not go completely smoothly for Wei. Sima Yi ordered
Zhang He to further pursue the enemy in an attempt to capitalise on their momentum. The
Weilüe mentions that Zhang He refused to obey Sima Yi's order and argues that according to classical military doctrine, one should refrain from pursuing an enemy force retreating to its home territory. However, Sima Yi refused to listen and forced Zhang He to carry out the order. Indeed, Zhang He fell into an ambush at Mumen Trail (木門道; near present-day
Qinzhou District, Tianshui, Gansu), where Zhuge Liang had ordered crossbowmen to hide on high ground and fire at approaching enemy forces when they entered a narrow
defile. Zhang He died after a stray arrow hit him in the right knee. Unlike the
Book of Jin records, Wei's army suffered a great deal of damage from pursuing Shu's retreating army. ==Fifth expedition==