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 Gen. 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.