President Barack Obama has gone back on his plan to pull a majority of troops from Afghanistan by the end of the year, saying Thursday that the U.S. would maintain a military presence in the country until at least 2017.

Obama announced his decision to keep 9,800 troops in Afghanistan through the end of 2015. For his last year in office, 5,500 troops will remain by the end of 2016.

"As commander in chief, I will not allow Afghanistan to be used as a safe haven for terrorists to attack our nation again," the president said. "Afghan forces are still not as strong as they need to be."

Obama ensured that U.S. forces would not engage in combat operations, but would look to aid the Afghan military in counter-terrorism measures.

The war in Afghanistan has become the longest in U.S. history. One of Obama's biggest goals in office was put an end to the war, as well as the one in Iraq. In 2014, he announced plans to decrease the number of troops in Afghanistan to 1,000 by the end of 2016.

Obama's new decision comes in light of recent events. Taliban insurgents have grown in power, temporarily seizing the city of Kunduz in northern Afghanistan.

Frequent conversations with Afghan President Ashraf Ghani also influenced the revision.

Sen. John McCain, R-Ariz., chair of the Senate Armed Services Committee, approved of the new decision. He had been critical of Obama's plans to withdraw troops from the country, and according to USA Today, he now questions whether the residual forces will be enough.

"I am concerned that the number of troops will not be sufficient to perform the critical tasks being set for them: counter-terrorism and continuing to train and advise our Afghan partners," McCain said.

While Obama's previous plan was expected to cost $10 million, the announcement now brings projected costs to $14.6 million.