CREDIT GOES to Secretary of State John F. Kerry for a marathon negotiating effort in Kabul over the weekend that may have rescued a vital security agreement between the United States and Afghanistan. Mr. Kerry and Afghan President Hamid Karzai said they settled two contentious issues that were blocking an accord, under which the United States and its NATO allies would station training and counterterrorism forces in Afghanistan after 2014. There now seems to be a decent chance the deal will be finalized before an Oct. 31 deadline set by President Obama, who had threatened to cancel plans for a post-2014 deployment if agreement were not reached.

Mr. Kerry may have saved both governments from a self-inflicted disaster. Without U.S. or NATO forces to back them up, Afghanistan’s army and government would be in danger of collapse after next year, risking everything that Mr. Karzai, two U.S. presidents and the NATO alliance have built in the past dozen years, at a dear cost in lives. Yet Mr. Karzai, who is due to leave office next year, appeared unwilling to bend on demands that the United States guarantee Afghanistan’s security — a commitment that Washington feared might bring it into conflict with Pakistan — and cede all counterterrorism operations to Afghan forces.

Mr. Obama and his advisers, for their part, seemed ready to repeat the mistake they made with Iraq in 2011. There they convinced themselves that a U.S. residual force was unnecessary and so did not aggressively pursue an agreement with the Iraqi government. Iraq has now relapsed into sectarian warfare. Yet White House officials have been talking up a similar conclusion about Afghanistan, saying the al-Qaeda threat is located elsewhere and the cost of a continued presence is unjustified — a view that discounts the dangers of a renewed Afghan civil war.

There appears to be some fudging in the deal reached by Mr. Kerry and Mr. Karzai. The United States provided language about defending Afghanistan against invasion that Mr. Karzai deemed adequate, while U.S. officials said current rules about anti-terrorism operations, under which U.S. forces can act with Afghan collaboration or agreement, would be codified. Still outstanding is the question of whether U.S. soldiers will be exempted from Afghan courts, a potential deal-breaker that Mr. Karzai said he would submit to a traditional loya jirga meeting and the Afghan parliament. U.S. officials had reason to hope that was a political cover: Mr. Karzai has accepted the immunity provision before and typically uses the loya jirga meetings to ratify his decisions.

Between Mr. Karzai’s erratic behavior and Mr. Obama’s lukewarm commitment to his own strategy, the deal could still crumble. Were that to happen, the best U.S. response would be patience. An election campaign for Mr. Karzai’s replacement has begun, and several of the leading candidates have endorsed a bilateral security agreement. For all the disappointments, Afghanistan has made enormous progress since 2001, and its armed forces are capable of defending those gains with U.S. and NATO help. It would be foolish for either government to sabotage the alliance, and the consequences would be tragic.