American soldiers in Kunduz, Afghanistan, in October 2010. — Photograph: Damon Winter/The New York Times.

“THE United States is not losing in Afghanistan, but it is not winning either, and that is not good enough,” reads the opening sentence of a top-secret review of the war in Afghanistan commissioned by President George W. Bush in 2008, according to multiple participants in that review. Subsequent classified reviews of the American strategy in the war have repeated that conclusion.

The Trump administration undertook the latest rethinking of the war in August. President Trump's advisers again reviewed its causes: opium, corruption, ethnic factionalism and, above all, the support and sanctuary provided to the Taliban by Pakistan, through the covert action arm of its powerful spy agency, the Directorate for Inter-Services Intelligence.

Why is this problem so hard? Why, since the September 11 attacks, has the United States been unable to prevent Pakistan, a notional ally that has received billions of dollars in aid, from succoring the Taliban at such a high cost in American lives and Afghan misery?

One major reason is American war aims in Afghanistan have been, and remain, riddled with contradictions and illusions that Inter-Services Intelligence can exploit. President Bush, President Barack Obama and President Trump have all offered convoluted, incomplete or unconvincing answers to essential questions: Why are we in Afghanistan? What interests justify our sacrifices? How will the war end?

Mr. Trump is departing from his predecessors by getting tougher on Pakistan. His administration is withholding as much as $1.3 billion worth of annual aid to Pakistan until it does more to pressure the Taliban. Unfortunately, the record of using threats and sanctions to change Pakistan's conduct is a dismal one, and the influence and leverage of the United States in Pakistan is shrinking.

Mr. Trump is not the first president to struggle over how to align goals with reality. In 2009, as President Obama escalated combat troop levels in Afghanistan, his advisers identified only two vital American interests in the war, according to participants, the kinds of interests that might justify sending soldiers into battle.

One was the security of Pakistan's nuclear arsenal. The second was the terrorist threat posed by Al Qaeda “and its affiliates.” Neither problem really existed in Afghanistan; they resided over the border, in Pakistan. After 2002, Al Qaeda's most lethal operators largely fled to Pakistan. Mr. Obama's strategists nonetheless rationalized their escalation on the grounds that if Afghanistan fell into chaos, Al Qaeda would return — a plausible fear but an indirect and even speculative reason to send American men and women to war.

President Obama and his strategists also debated whether the Afghan Taliban posed the same threat to the United States that Al Qaeda did. Mr. Obama did not think so; he wanted a laser focus on Al Qaeda. Some Pentagon commanders did want to fight the Taliban. But the national security cabinet, including Defense Secretary Robert Gates, recognized that a United States-led war against the Taliban could not be won, at least not fast enough or at an acceptable cost. The Taliban were part of Afghanistan's “political fabric”, Mr. Gates noted accurately.

The Obama strategists decided to try to “degrade” the Taliban and “reverse its momentum,” according to participants, while building up Afghan security forces to take charge. The language was vague and subjective because the goals were, too. The idea was to buy time and give the Afghan government a chance.

The leaders of Inter-Services Intelligence understood that they could wait Washington out. Mr. Obama made this obvious when he announced in 2009 that American troops would start withdrawing and handing off the war to Afghan forces in 2011. Pakistan's generals, led then by the Army chief General Ashfaq Parvez Kayani, a former director of the spy agency, privately told American and NATO military leaders that they would fail. “Given the number of troops you have and the time constraints, you won't be able to do it,” General Kayani said, according to a participant in the meeting.

He meant that the American-led effort against the Taliban would not be decisive and that Afghan forces would never cohere enough to win. General Kayani wanted a less ambitious plan aimed at clearing radicals out of the Afghan-Pakistan border. Considering Inter-Services Intelligence's role in the conflict, however, his prediction of American failure could be heard as much as a threat as a forecast. Pakistan's objective has been to prevent Afghanistan's violence from spilling over its border and to prevent India from gaining influence in a neighboring country.

Apart from the convoluted policies of the United States, there are other reasons the Pakistani spy agency's approach has prevailed despite American frustration and periodic threats. Because keeping Pakistan's nuclear bombs out of the wrong hands has long been a top priority for the United States and Europe, it follows that Pakistan's overall stability is crucial. Yet the more violent the Afghan war became after 2001, the more it destabilized Pakistan.

After Al Qaeda took refuge in Pakistan, it collaborated with local radicals. Starting in 2007, those networks turned against the Pakistani state and touched off the worst years of domestic terrorism Pakistan has ever known. Suicide and car bombings rocked major cities, and tens of thousands of Pakistani civilians, security personnel and insurgents died. The country has paid a steep price for Inter-Services Intelligence's coddling of groups like the Taliban for decades.

Only since 2016 has Pakistan somewhat restored domestic security; last year was the least deadly since 2005, according to the South Asia Terrorism Portal, a research project, yet more than 500 Pakistani civilians perished in terrorist attacks.

The thinking of the United States and the European governments has been consistent, if rarely enunciated in public: To keep Pakistan stable and its nuclear arms under control, there is a limit to how much outside pressure can be brought to bear on the country.

President Obama authorized the C.I.A. to attack Al Qaeda with armed drones in Pakistan's tribal areas along the Afghan border. And of course, Mr. Obama authorized a daring SEAL raid inside Pakistan to kill Osama bin Laden, without asking permission. But the larger terrorist infrastructure — the Afghan Taliban's leadership and many other violent groups nurtured and tolerated by Inter-Services Intelligence — remains unscathed, operating from Lahore to Karachi to Quetta to Pakistani-controlled Kashmir.

In effect, Pakistan's strategy of nuclear deterrence, conceived to keep India's military at bay, has also deterred the United States. The United States has so feared the risks of violent disarray in Pakistan that it has tolerated interference by Inter-Services Intelligence in Afghanistan since 2001 that it otherwise would most likely not have accepted.

It is understandable that Afghan leaders and American generals express fury over the spy agency's complicity in Afghanistan's violence, including in the deaths of American soldiers. Through the first nine months of 2017, the United Nations reported the deaths of 2,640 civilians, including nearly 700 children, a toll similar to that of the same period last year. Most of the civilian deaths are caused by the Taliban and other anti-government guerrillas. This month the Taliban claimed responsibility for an attack on the Intercontinental Hotel in Kabul that killed at least 22 people, including Americans.

There are alternatives to accepting the status quo. If sanctions against Inter-Services Intelligence or Pakistan's military were combined with serious diplomacy to engage China, which is by far Pakistan's most important ally, as well as other regional powers, there might be a path to improvement.

China, Pakistan, Russia and Iran share an interest with the United States in preventing the Islamic State, which has established a foothold in eastern Afghanistan, from expanding. China has long protected Pakistan from outside pressure on terrorism and nuclear proliferation issues, but it has an interest in a more stable region where there is a reduced need for an American combat presence.

For years, almost every American general dispatched to command the Afghan war has conceded that the conflict must ultimately end with a political settlement, supported by regional powers, and that there is no purely military solution possible against the Taliban. Nonetheless, the United States continues to prioritize military action over diplomacy. Stalemated civil wars like Afghanistan's can last a very long time. They end only through negotiations with the enemy.

The Obama administration tried talking in secret to the Taliban and made some progress but was undone by the contradictions in its own strategy and by Inter-Services Intelligence, which wanted a hand in any deal, even though the Taliban's leadership preferred to be free from Pakistan's influence. Many Afghan government officials and former Taliban leaders have tried on their own to talk their way to peace during the past decade; some have been assassinated by hard-liners.

For the United States, an alternative to pursuing difficult and uncertain negotiations would be to give up and leave, but the most likely result of a unilateral military pullout now would be more violence and rising influence for the Taliban and the Islamic State.

The most rational course is one for which President Trump would seem poorly suited: to work closely with allies, prioritize high-level diplomacy, be smart in pressuring the Inter-Services Intelligence and accept that in Afghanistan, a starting point for any international policy is humility.