Boris Johnson warned of "perils" on the ground as he signalled the end of the UK's 20-year military campaign in Afghanistan and promised not to turn his back on the country.
The prime minister said the majority of British forces have now withdrawn and paid tribute to the more than 450 British personnel who died during the US-led war, as his military chief spoke of the possibility of the Afghan state collapsing and a descent into civil war.
Foreign forces are leaving even as the Taliban gain ground and concern grows about the return of an international terrorist threat from within Afghanistan's borders.
Mr Johnson, however, made clear that the operation, launched in the wake of the 9/11 attacks on the United States, had significantly degraded al Qaeda's capabilities and improved the standard of living for millions of Afghans.
"No one should doubt the gains of the last 20 years," he told MPs.
But he added: "Nor can we shrink from the hard reality of the situation today."
He said the international military presence in Afghanistan was never intended to be permanent and there could "never be a perfect moment" to leave.
Yet he said the exit of the majority of Britain's 750-strong military footprint from the country did not mark the end of the UK's interest in the country.
"I hope no one will leap to the false conclusion that the withdrawal of our forces somehow means the end of Britain's commitment to Afghanistan.
"We are not about the turn away nor are we under any illusions about the perils of today's situation and what may lie ahead."
Speaking separately, the UK's military chief, himself a veteran of the Afghan conflict, described the reality on the ground as "pretty grim".
General Sir Nick Carter said the possibility of Afghanistan collapsing into a civil war - with the Afghan government fracturing and fighting between the Taliban and other warlords - was plausible.
But he said the chance of a deal between the Afghan government and the Taliban was also "intimately possible". While the Taliban hold nearly half of all rural districts, they have not yet captured any provincial cities.
The chief of the defence staff insisted the 150,000 British military personnel who served in the country since 2001 should "hold their heads up high" over what had been achieved.
This included preventing Afghanistan from again being used as a launchpad for international terrorism since the 11 September 2001 attacks.
Yet, in a sign the terrorist threat remains, highly symbolic flag-lowering ceremonies to mark the finale of the UK's military campaign - dubbed Operation Toral - took place in secret in Kabul to avoid attracting unwanted attention, with the last one occurring on 24 June.
The UK and other non-US NATO allies have been forced to withdraw from Afghanistan after US President Joe Biden set 11 September of this year as the date by which the dominant American mission would end.
This followed on from a pledge by his predecessor Donald Trump to pull US troops out as part of a peace agreement with the Taliban.
In reality the US exit has been much faster, with the majority of its forces also out already.
The UK has made clear it did not support the American decision at a time when the Taliban is gaining strength and the security situation is deteriorating.
But London had little choice other than to call time on its contribution.
It will retain a small number of military personnel to support a US-led effort to protect diplomats who are staying in the country.
Britain plans to keep its highly-fortified embassy open, with additional protection provided by a private security company.
There will likely also be a UK special forces contingent to counter emerging threats from al Qaeda and Islamic State.
British veterans of the war who suffered mental and physical injuries and the families of the 457 personnel who died have been left wondering whether their sacrifice was worthwhile, with many concluding it was not.
General Carter, however, said he believed that the campaign had improved the lives of millions of Afghans.
He pointed to an increase in girls' education, a reduction in infant mortality and greater access to clean water and electricity.
"Our veterans should hold their heads up high because they have done something and their successors will be immensely impressed with what they have achieved," he said.
The combat phase of the US and NATO-led operation in Afghanistan, which lasted from 2001 until 2014, involved the highest numbers of British and other NATO forces, in particular when the UK led a surge into southern Afghanistan in 2006 to take on the Taliban.
The end of that mission - dubbed for the UK troops Operation Herrick - was marked by significant fanfare as the UK and its NATO allies transitioned to a more supportive role alongside the Afghan security forces.
This time around, Britain has quietly been pulling out the vast majority of its 750 service personnel who comprised Operation Toral, which succeeded Operation Herrick.
The Ministry of Defence said the RAF had flown the 50 flights to bring home troops and equipment. This included the redeployment of three Puma helicopters.