Both Mr. Karzai and American officials hear the clock ticking. American forces are turning over their combat role to Afghan forces and preparing to leave Afghanistan this year, and the campaigning for the Afghan national election in April has begun. An orderly transition of power in an Afghanistan that can contain the insurgency on its own would be the culmination of everything that the United States has tried to achieve in the country.

“We’ve been through numerous cycles of ups and downs in our relations with President Karzai over the years,” Ambassador James B. Cunningham said during a briefing with reporters last week. “What makes it a little different this time is that he is coming to the end of his presidency, and we have some very important milestones for the international community and for Afghanistan coming up in the next couple of months.”

Mr. Karzai has been increasingly concerned with his legacy, officials say. When discussing the impasse with the Americans, he has repeatedly alluded to his country’s troubled history as a lesson in dealing with foreign powers. He recently likened the security agreement to the Treaty of Gandamak, a one-sided 1879 agreement that ceded frontier lands to the British administration in India and gave it tacit control over Afghan foreign policy. He has publicly assailed American policies as the behavior of a “colonial power,” though diplomats and military officials say he has been more cordial in private.

Mr. Karzai reacted angrily to a negative portrayal of him in a recent memoir by the former Defense Secretary Robert M. Gates, and he is still bitter over the 2009 presidential election, when hundreds of thousands of fraudulent ballots were disqualified and, as he sees it, the Americans forced him into an unnecessary runoff against his closest opponent.

Domestic Interests

In some respects, Mr. Karzai’s outbursts have been an effort to speak to Afghans who want him to take a hard line against the Americans, including many ethnic Pashtuns, who make up nearly all of the Taliban. With the American-led coalition on its way out and American influence waning, Mr. Karzai is more concerned with bridging the chasms of Afghan domestic politics than with his foreign allies’ interests.

If the peace overture to the Taliban is indeed at an end, as officials believe, it is unclear what Mr. Karzai will do next. He could return to a softer stance on the security agreement and less hostility toward the United States, or he could justify his refusal to sign the agreement by blaming the Americans for failing to secure a genuine negotiation with the insurgents.

Mr. Karzai has insisted that he will not sign the agreement unless the Americans help bring the Taliban to the table for peace talks. Some diplomats worry that making such a demand allows the Taliban to dictate the terms of America’s long-term presence in Afghanistan. Others question Mr. Karzai’s logic: Why would the insurgency agree to talks if doing so would ensure the presence of the foreign troops it is determined to expel?