The U.S. military will halve the number of troops it has in Afghanistan within the next two months, Pentagon officials said Tuesday, as President Trump seeks to move closer to keeping a promise to end wars abroad despite concerns that the decision could undermine negotiations with the Taliban.

Pentagon officials also said they would make smaller cuts in Iraq, where U.S. forces have focused on countering the Islamic State.

“We owe this moment to the many patriots who made the ultimate sacrifice and our comrades who carry forward their legacy,” acting defense secretary Christopher C. Miller said at the Pentagon.

Miller said the military will carry out Trump’s orders in both countries by Jan. 15, with troop numbers reduced from about 5,000 to 2,500 in Afghanistan and from about 3,000 to 2,500 in Iraq. Even with the president’s repeated calls over the years to bring American troops home, the United States remains entangled in the wars in the closing weeks of the Trump administration.

In Afghanistan, which the United States invaded in the wake of the September 2001 terrorist attacks, the move appeared to mark a middle ground between Trump’s impulse to remove all American troops and recommendations from senior U.S. military officials to maintain the current numbers. In October, the president tweeted that all U.S. troops should be “home by Christmas.”

The announcement came eight days after Miller took over for ousted defense secretary Mark T. Esper, who had submitted a classified memo to the White House saying that the conditions on the ground in Afghanistan did not warrant such troop reductions.

Esper cited a recent surge in Taliban violence, safety concerns for remaining U.S. troops, possible damage to alliances and the chance that reducing troops could undermine negotiations with the Taliban to secure a landmark deal with the Afghan government.

Miller, a retired Special Forces officer who previously served Trump as a counterterrorism adviser, did not mention Esper’s dissent and took no questions from reporters. Miller said he was celebrating the decision, highlighting the toll that the wars in Iraq and Afghanistan have taken on military families, including 6,900 dead service members, 52,000 more wounded and others who carry scars “visible and invisible.”

The chairman of the Joint Chiefs of Staff, Army Gen. Mark A. Milley, did not appear alongside Miller at the lectern — raising questions about the view of top military officials. Milley has been seen in the White House as opposing deeper cuts, administration officials have said, and when national security adviser Robert C. O’Brien announced in October that Trump planned to withdraw 2,500 service members from Afghanistan this year, Milley called it “speculation.”

At the White House, O’Brien said the remaining American troops in both countries will defend U.S. Embassies, enable allies and deter foes. Trump’s plan to cut troops has been consistent, O’Brien added, citing his comments in October.

“By May, it is President Trump’s hope that they will all come home safely and in their entirety,” he said.

The United States has spent trillions of dollars in wars since 2001. Nearly 800,000 service members have deployed to Afghanistan at least once, according to Pentagon statistics, a figure that does not include diplomats, aid workers or private security contractors.

The decision comes about nine months after the Trump administration and the Taliban reached a deal that will remove all U.S. troops there by May if certain conditions are met.

Senior U.S. military officials have raised concerns about the Taliban’s commitment to meeting the terms of the deal, citing a spike in violence against Afghans since the agreement was signed and questions about whether the militant group will break with al-Qaeda.

Miller said the troop cuts are consistent with the administration’s established plans and strategic objectives, and are based on continuous conversations with national security advisers in the Cabinet.

During his remarks, Miller said he had spoken with military commanders in recent days, “and we will all execute this repositioning in a way that protects our fighting men and women, our partners in the intelligence community, our diplomatic corps and our superb allies.”

A senior defense official said before Miller’s announcement that the administration believes the cuts are the best way to drive toward a peace agreement between the Afghan government and the Taliban.

The official said the plan was reached “in consultation” with commanders, but declined to comment on Esper’s memo or on specific recommendations from senior military officers, including Milley and Army Gen. Austin “Scott” Miller, the top U.S. commander in Afghanistan.

A U.S. military official, speaking on the condition of anonymity to discuss internal planning, said officials were in the process of determining what the smaller U.S. mission could include given the current security conditions, which have deteriorated sharply.

The official said the mission would probably include counterterrorism operations, high-level advising of Afghan security forces, and aerial surveillance and airstrikes, both for counterterrorism efforts and in support of Afghan forces.

It was not clear how many bases the United States would retain.

The official said the U.S. mission in Iraq would remain roughly the same.

In Baghdad, the news of the partial withdrawal was greeted with possible trouble, as several rockets were fired in the city, shaking some neighborhoods and killing at least one civilian, security officials said. The violence appeared to end a month-long truce announced by Iranian-backed militias, which have launched long-range attacks on U.S. positions, triggering warnings from American officials that they will retaliate if U.S. troops are killed.

In a rare rebuke, NATO Secretary General Jens Stoltenberg warned that Afghanistan could again become a platform for terrorists to launch attacks overseas. And the Islamic State, he said, could rebuild in Afghanistan after largely being stamped out in Iraq and Syria.

“We have been in Afghanistan for almost 20 years, and no NATO ally wants to stay any longer than necessary,” Stoltenberg said in a statement. “But at the same time, the price for leaving too soon or in an uncoordinated way could be very high.”

U.S. lawmakers appeared deeply divided on the pullout plans, offering a mix of responses.

Rep. Mac Thornberry (Tex.), the ranking Republican on the House Armed Services Committee, said he thinks the decision will undercut negotiations with the Taliban, which he said had not met conditions of the agreement.

“As long as there are threats to Americans and American national security in the world, the U.S. must be vigilant, strong, and engaged in order to safeguard our people and fulfill our duty under the Constitution,” he said in a statement.

But Sen. Josh Hawley (R-Mo.) said in a letter sent to Miller on Tuesday, before the announcement at the Pentagon, that he supports the president’s plan and believes that most policymakers have ignored Americans’ desire to end the war.

“They are certainly entitled to keep advocating nation-building, but they have no right to force working Americans to pay the price for their agenda,” Hawley wrote. “The American people deserve an end to this war. They deserve to know that their sons and daughters will not be put in harm’s way unless it is absolutely necessary.”

Among Democrats, Sen. Jack Reed (R.I.), a member of the Senate Armed Services Committee, issued a scathing statement in which he said the United States cannot let its national security and relationships with partners “become a casualty of President Trump’s wounded ego.”

“There are no easy solutions to the conflicts in Afghanistan and Iraq, but this shortsighted approach won’t bring peace and is more likely to threaten America’s interests,” Reed said.

But Rep. Adam Smith (D-Wash.), the chairman of the House Armed Services Committee, signaled support for the cuts, saying that they “must be responsibly and carefully executed to ensure stability in the region.”

James Dobbins, a veteran diplomat who worked on Afghanistan during the administrations of George W. Bush and Barack Obama, said the decision to withdraw troops stood in contradiction to the Taliban’s promise to break with al-Qaeda and reduce violence in its February agreement with the United States.

“So this is directly rewarding bad behavior,” he said.

He saw the decision as more about Trump’s domestic political concerns than national security.

“Clearly, it’s a legal order,” Dobbins said. “The military isn’t not going to obey. They’re going to salute.”

Louisa Loveluck in Baghdad, Adam Taylor in Washington and Michael Birnbaum in Riga, Latvia, contributed to this report.