General Mark Milley, chairman of the Joint Chiefs of Staff, said he believed the US should have kept several thousand troops in the country to prevent the Taliban takeover that happened faster than forecast.
He refused to say what advice he gave President Joe Biden last spring when Mr Biden was considering whether to keep any troops in Afghanistan.
But he told the Senate Armed Services Committee it was his personal opinion that at least 2500 were needed to guard against a collapse of the Kabul government and a return to Taliban rule.
In a blunt assessment of the outcome to a war that cost 2461 American lives, General Milley called it a strategic failure.
"The enemy is in charge in Kabul," he noted, referring to the Taliban having taken control of the capital on August 15.
"There's no way else to describe that," he said, adding that it's possible the US military's biggest failure was to make the Afghan military overly dependent on an American presence and technology.
General Frank McKenzie, who as head of Central Command had overseen the final months of the US war, said he agreed with General Milley's assessment. He also declined to say what he had recommended to Mr Biden.
The Senate hearing was at times contentious, as Republicans sought to portray Mr Biden as having ignored advice from military officers and mischaracterised the military options the president was presented last spring and summer.
Senator Tom Cotton (Republican, Arkansas) asked General Milley why he did not choose to resign after his advice was rejected.
General Milley, who was appointed to his position as chairman of the Joint Chiefs of Staff by President Donald Trump and retained by Mr Biden, said it was his responsibility to provide the commander in chief with his best advice.
"The President doesn't have to agree with that advice," General Milley said.
"He doesn't have to make those decisions just because we are generals. And it would be an incredible act of political defiance for a commissioned officer to resign just because my advice was not taken."
Testifying alongside General Milley and General McKenzie, Defence Secretary Lloyd Austin defended the military's execution of a frantic airlift from Kabul.
He asserted it would be "difficult but absolutely possible" to contain future threats from Afghanistan without troops on the ground.
Under questioning, he, too, declined to say what advice he had given Mr Biden about whether to make a full troop withdrawal.
General Milley cited "a very real possibility" that al-Qaeda or the Islamic State group's Afghanistan affiliate could reconstitute in Afghanistan under Taliban rule and present a terrorist threat to the United States in the next 12 to 36 months.
It was al-Qaeda's use of Afghanistan as a base from which to plan and execute its attacks on the United States on September 11, 2001, that triggered the US invasion of Afghanistan a month later.
"And we must remember that the Taliban was and remains a terrorist organisation and they still have not broken ties with al-Qaeda," General Milley said.
"I have no illusions who we are dealing with. It remains to be seen whether or not the Taliban can consolidate power or if the country will further fracture into civil war."
Mr Austin questioned decisions made over the 20-year course of the US war in Afghanistan. In retrospect, he said, the American government may have put too much faith in its ability to build a viable Afghan government.
READ MORE: NSW's roadmap out of lockdown explained
"We helped build a state, but we could not forge a nation," he told the Senate Armed Services Committee.
"The fact that the Afghan army we and our partners trained simply melted away — in many cases without firing a shot — took us all by surprise. It would be dishonest to claim otherwise."
Asked why the United States did not foresee the rapid collapse of the Afghan army, General Milley said that in his judgment the US military lost its ability to see and understand the true condition of the Afghan forces when it ended the practice some years ago of having advisers alongside the Afghans on the battlefield.
"You can't measure the human heart with a machine, you have to be there," General Milley said.
Mr Austin acknowledged shortcomings in the final airlift from Hamid Karzai International Airport that began August 14, such as an initial wave of violence at and near the airfield that led to multiple deaths of Afghan civilians.
But he asserted that the airlift was a historic accomplishment that removed 124,000 people from Taliban rule.
"To be clear, those first two days were difficult," said Mr Austin, who is a veteran of the war.
"We all watched with alarm the images of Afghans rushing the runway and our aircraft.
"We all remember the scenes of confusion outside the airport. But within 48 hours, our troops restored order, and process began to take hold."
The Biden administration faces criticism on multiple fronts for its handling of the final months of the war.
Senator James Inhofe, the ranking Republican on the Armed Services panel, told Mr Austin and General Milley that the withdrawal and evacuation amounted to an "avoidable disaster".
Republicans in particular have intensified their attacks on Mr Biden's decision to pull all troops out of Afghanistan by August 30, saying it left the US more vulnerable to terrorism.
They are demanding more details on the suicide bombing in Kabul that killed 13 American service members in the final days of the withdrawal.
The withdrawal ended the longest war in US history. The Biden administration, and some Democrats in Congress, have argued that Trump bears some of the blame for the war ending in a Taliban victory, since his administration signed a deal with the Taliban in 2020 that promised a full American withdrawal by May 2021.
They also have pointed to a years-long US failure to build an Afghan military that could stand up to the Taliban.
"This is not a Democratic or a Republican problem," Senator Jack Reed, (Democrat, Rhode Island) said the day after the Taliban took over Kabul on August 15.
"These failures have been manifesting over four presidential administrations of both political parties."