President Obama said Tuesday that he would freeze U.S. troop levels in Afghanistan at 9,800 through the end of the year but vowed to end the American war by the end of his presidency.

Obama announced that he would slow the planned withdrawal of U.S. troops after a White House meeting with Afghan President Ashraf Ghani. The decision to keep more U.S. forces in Afghanistan this year was driven by the administration’s confidence in Ghani, whom Obama praised repeatedly. Relations between Ghani’s predecessor, Hamid Karzai, and the U.S. government had been steadily deteriorating for years. Obama lauded Ghani, who took office in September, for taking on “the mantle of commander in chief in a way that we have not seen in the past from an Afghan president.”

But Obama also made it clear that he wouldn’t depart from his current plan to close the remaining U.S. bases in Afghanistan and consolidate the remaining U.S. forces in Kabul by the end of 2016.

“Afghanistan is still a dangerous place,” Obama said. “The way it’s going to become less dangerous is by Afghan security forces­ being capable of keeping law and order and security in the country, and that is not going to happen if foreign forces­ are continually relied upon.”

The White House decision to keep about 5,000 additional troops in Afghanistan came at Ghani’s request and also reflected the growing concern of U.S. military commanders. Last year’s fighting season was one of the bloodiest for Afghan civilians and security forces­ since the U.S. invasion in 2001.

Obama insisted that Afghan security forces­ were capable of defending the country against the Taliban. “They’re better equipped than the Taliban,” he said. “They are better equipped than the Haqqani network,” an insurgent group closely allied with the Taliban.

Keeping the extra U.S. troops through the end of this year would ensure that the Afghan forces­ continue to make improvements to guarantee that far-flung units have the food, ammunition and intelligence required to fight on their own, Obama said.

Ghani, who is the most important figure in the administration’s plans to hold the country together, said that Afghan troops had made remarkable strides in recent years. But he noted that the U.S. and European troops have better training, superior weapons and the backing of attack planes and helicopters that can race to their aid.

“The success has to be judged comparatively,” he said in an interview. “Not a single province has fallen; not a single battalion has deserted; not a single army corps has refused to fight. They secured the election; they have borne the casualties, and they’ve moved from defensive to offensive.”

The still-unanswerable question is whether the Afghan army and police forces­ will perform better than the U.S.-trained Iraqi troops that collapsed within years after the Americans left the country. “Everyone looks forward to the day when Afghans can meet all of their own security needs, but Iraq has shown us the consequences of leaving a fragile ally too early,” said the House Armed Services Committee chairman, Rep. Mac Thornberry (R-Texas). “The bottom line is that our own security is at stake.

Ghani left open the possibility that he might ask the Obama administration to keep more troops in the country after 2016 but said that his focus for the moment was on preparing Afghan forces for the fighting season, which will begin in a few weeks.

“Things can be done sequentially; let’s not preempt options,” he said. “We have had a very important window provided to us and that window is what we must use to maximum effect.”

Both Obama and Ghani stressed that an end to the war in Afghanistan could only come through a political deal with the Taliban. In recent years, talks between the Taliban and the Afghan government have shown few signs of progress. Since taking over as president, Ghani has sought to smooth relations with Pakistan, whose support would be critical to any long-term peace deal.

Obama praised Ghani’s “bold leadership,” which he described as “critical to the pursuit of peace.”

The extra U.S. forces would allow the Americans to keep troops in regions of Afghanistan that have been most hotly contested by the Taliban and possibly strengthen the Afghan government’s position in peace talks. “It lessens the chances that the Taliban can score a major victory this year and thereby could support a negotiating strategy,” said Carter Malkasian, a former top political adviser to the U.S. military in Afghanistan and researcher with CNA Corp.

For Ghani, the trip to Washington this week was part of a broader campaign to build confidence in his government, which is heavily dependent on billions of dollars in annual U.S. aid for its survival. The Afghan government brings in only about $1 billion in annual revenue but spends more than $4 billion on its army and police forces­.

Ghani and Obama highlighted their shared experiences at Columbia University, where they both studied, and in Washington, where Ghani spent several years working at the World Bank.

In a nod to U.S. officials and human rights advocates concerned about the mistreatment of women, Ghani noted that about 20 percent of his cabinet ministers are women.

“Hopefully, one day, we will see an Afghan woman president,” he said.

The upbeat tone from Obama and Ghani did not reflect the often-dire assessments of Afghanistan analysts and former government officials.

Shortly before Obama and Ghani met, several former senior officials from the Obama and Bush administrations released an open letter that described the war in Afghanistan as a “stalemate.” Among those who signed the letter were Ryan Crocker, Obama’s former ambassador to Afghanistan; and Michèle Flournoy, who was among Obama’s top choices to serve as defense secretary.

“The Taliban are intensifying their pressure on Afghan civilians,” the letter said. “Meanwhile the political and economic situation is fragile.”

The 23 experts said that a full U.S. withdrawal in 2016 would “unnecessarily put at risk hard-won gains of the last 13 years.”