Zalmay Khalilzad was the U.S. ambassador to Afghanistan from 2003 to 2005 and ambassador to the United Nations from 2007 to 2009.

There is a risk that the United States and Afghanistan will not reach a deal on the U.S. military presence in Afghanistan after 2014. Both sides want to negotiate a bilateral security agreement. But President Obama, frustrated with Afghan President Hamid Karzai and determined to end the U.S. role in Afghanistan next year, has given Kabul until October either to conclude a deal or face unspecified unilateral actions by Washington. Recent leaks indicate that Obama is contemplating a total drawdown of U.S., and thus international, forces.

My discussions with officials in Washington and Afghanistan have left me thinking that an agreement is unlikely to be signed by October. Mistrust at the leadership level, different threat perceptions and Obama’s arbitrary but politically potent deadline for ending the war all pose significant obstacles.

Karzai’s allegations of bad faith against the United States have dampened the Obama administration’s enthusiasm for a bilateral security agreement. Whether Karzai fully appreciates the costs of alienating Washington is unclear. The U.S. proposal on the table would make Afghanistan the largest recipient of U.S. security and economic assistance for the foreseeable future, even ahead of close allies such as Israel.

Karzai’s distrust of the United States stems, at least in part, from a long list of political and personal affronts by the Obama administration. He remains bitter about U.S. allegations of corruption during the 2009 Afghan election, which he viewed as a campaign to engineer his electoral defeat and impugn the honor of his family and close associates.

U.S. policy toward Pakistan is another source of tension. Karzai is frustrated by Washington’s unwillingness to confront insurgent sanctuaries in that country or respond to firings across the Afghan border. From Karzai’s perspective, the botched opening of the Taliban’s political office in Doha last month was a U.S.-Pakistani conspiracy to divide Afghanistan. Karzai is using the issue to unite anti-Taliban Afghans against not just Pakistan but also the United States.

The Pakistan issue is related to a more fundamental difference in U.S. and Afghan threat perceptions. The Obama administration is concerned primarily with defeating al-Qaeda. Yet Afghanistan is threatened by not only al-Qaeda but also a wider nexus of Pakistan-based groups, including the Taliban and al-Qaeda affiliates such as the Islamic Movement of Uzbekistan. As a result, Washington and Kabul differ on the scope of the problem a bilateral security agreement should address.

Barring a negotiated settlement, Afghan officials believe that military attacks against cross-border sanctuaries offer the only realistic solution. But even though peace talks are unlikely to succeed in the near term, the Obama administration will not commit to using decisive force against Pakistani sanctuaries.

Obama’s reluctance to delay the timeline for ending direct U.S. involvement in Afghanistan is complicating negotiations. Within the administration, Karzai’s perceived intransigence has bolstered the position of senior officials who favor total U.S. withdrawal and argue that Obama should not renege on yet another public commitment. Obama is also pressing the October deadline because NATO will not arrive at its post-2014 agreement until the United States concludes its own negotiations.

Failure to conclude a security agreement would be a major strategic setback. The loss of Afghan facilities would inhibit Predator attacks against al-Qaeda in the region. It would signal a broader U.S. abandonment of Afghanistan that would intensify internal conflict and invite increased meddling by Pakistan, India, Iran, China and Russia, among others, to fill the vacuum.

Washington can still salvage the negotiations but must adjust its approach. Besides presenting the agreement to Afghans as insurance against catastrophic failure, U.S. officials should conceptualize it as a way to “harden” Afghanistan — that is, strengthen the country’s military, political and economic institutions. Pakistan-backed groups are unlikely to negotiate in good faith until they are convinced that the Afghan government is strong enough to withstand and deter insurgent attacks.

Washington should also address Afghan concerns about external aggression. The Karzai government has a list of diplomatic, political and military steps for the United States to consider in dealing with such threats. The proposed steps would impose costs on Pakistan and could deter further attacks.

These changes may be sufficient for Karzai to sign on. Should Karzai refuse, Obama should not simply abandon negotiations and embrace the “zero option” of withdrawal. Rather, he should opt for strategic patience and wait out Karzai. Most Afghan leaders, like the Afghan population generally, favor a bilateral security agreement with the United States. As long as Afghanistan’s presidential election occurs on schedule in April, Washington could sign the deal with Karzai’s successor without significantly altering the U.S. drawdown schedule.

Negotiations on a post-2014 agreement are a test of both countries’ leaders. Karzai faces a choice between remaining captive to personal grievances or acting to secure Afghanistan’s long-term interests. Obama needs to decide whether short-term expediency is worth the risk of undermining hard-won gains, allowing Afghanistan to become a haven for terrorist groups and forcing a future U.S. president to invade Afghanistan again.