US Defense Secretary Ashton Carter arrived in Afghanistan on Tuesday for talks with President Ashraf Ghani, days after the United States and NATO pledged to keep thousands of troops in the troubled country. Carter flew in to Bagram air base outside Kabul, and was due to meet with Chief Executive Abdullah Abdullah in the capital later. The Pentagon chief's visit follows a renewed commitment to Afghanistan from NATO, which said over the weekend it would keep forces there at least until the end of 2017. Most are American, but some 40 countries have deployed troops there. Their official role is to train Afghan forces, who are now responsible for their country's security. Despite a massive, nearly 15-year international effort to defeat the Taliban, the resurgent group controls large areas of Afghanistan and have vowed to keep fighting until foreign forces leave. President Barack Obama, elected eight years ago on a pledge to end the wars in Afghanistan and Iraq, has been unable to do so given the fragile security situations in both countries. Last week he dialed back plans to cut US troop numbers in Afghanistan from 9,800 to 5,500 by the end of the year. Instead, some 8,400 US troops will remain, providing training and air support to the Afghans. Obama has also relaxed rules of engagement, making it easier for US troops to target the Taliban — a move that the head of NATO forces in Afghanistan, General John Nicholson, said was in "recognition of the difficulties that we are facing." "We can strike the Taliban and disrupt the enemy," he told reporters in Kabul over the weekend, adding that Obama had granted "all the capabilities that we requested." Local forces took the lead in providing security in 2015, but are struggling to contain Taliban offensives and prevent attacks from Daesh (the so-called IS) and Al-Qaeda. More than 5,000 Afghan security forces were killed last year, and attacks continue. The Taliban claimed responsibility last month for a suicide attack that killed more than 30 Afghan police cadets in Kabul. Further complicating matters is endemic corruption and allegations of rights abuses. About 13,000 NATO troops, most of whom are American, are currently stationed in Afghanistan under Operation Resolute Support to train and assist Afghan security forces. NATO has agreed about 12,000 troops will remain in Afghanistan through at least 2017, though in reality local troops are likely to need foreign support and funding for years to come. In return, NATO is demanding reforms of the Afghan security forces. Carter's visit followed a brief trip to Baghdad on Monday, where he pledged hundreds of additional US troops to assist Iraqis fighting against Daesh.