KABUL, Afghanistan — Last Saturday, Secretary of State John Kerry called President Hamid Karzai of Afghanistan with a simple question: If Mr. Kerry came to Kabul, would it help break a deadlock in negotiations to keep American forces in Afghanistan beyond next year?

The Afghan leader suggested it would, and Mr. Kerry arrived here on Friday for a previously unannounced visit that, in the estimation of many Afghan and American officials, represented the best and, possibly, the last chance to head off a complete American withdrawal when the NATO combat mission here ends in 2014.

Mr. Kerry’s decision to make the trip provided a respite from the pessimism that has spread rapidly in Kabul over the past week as the depth of the impasse faced by negotiators became apparent. Some Afghan and American officials reasoned that Mr. Kerry would not make such a public bid to rescue the talks if his chances of success were slim.

Still, few here thought success was a given for Mr. Kerry. Hours before his arrival, a senior Western diplomat put the odds of a deal at “no better than 50-50.”

Alongside the deadlocked negotiations, Afghan and American officials have also struggled in the past week to contain another potential crisis. A week ago, American forces intercepted a convoy of Afghan intelligence agents and seized a senior leader of the Pakistan Taliban, an offshoot of the Afghan insurgent movement, whom the agents were taking to Kabul, said Aimal Faizi, the spokesman for Mr. Karzai.

The Taliban leader, Latif Mehsud, was turned by Afghan intelligence roughly two years ago and had become a valuable asset, said an Afghan familiar with the situation. He was on his way to meet with senior Afghan intelligence officials in Kabul when American forces took him away at gunpoint along a road in Logar Province, south of the capital. He is now believed to be in American custody at Bagram Air Base, north of Kabul.

American officials said the Afghans had turned over Mr. Mehsud at the request of American forces, but the officials did not provide any additional details.

Mr. Karzai is said to be furious about the seizure of Mr. Mehsud by the Americans, raising further doubts about his readiness to compromise on a deal with the United States.

The doubts surrounding the talks to keep troops here are a sharp departure from only months ago, when American generals spoke of a post-2014 force as an inevitability and Afghan officials said the only question was how many troops would remain, not if they would get any at all.

But that certainty has given way to a last-minute scramble after nearly a year of talks. The Obama administration has set an Oct. 31 deadline for their conclusion, and Mr. Karzai and Mr. Obama have both signaled they are willing to walk away if necessary.

The sticking points are two Afghan demands that Mr. Karzai has said are crucial to the country’s sovereignty, but that the Obama administration says it will not consider.

The first is Mr. Karzai’s insistence that the United States guarantee Afghanistan’s security as if it were a NATO ally. That could compel the United States to send troops on raids into Pakistan, an ally of Washington and a nuclear-armed power.

Mr. Karzai is also refusing to allow American forces to continue hunting here for operatives of Al Qaeda. Instead, he wants any intelligence gathered by the United States handed over to Afghan forces, who would then conduct the raids on their own.

Mr. Kerry, one of the few American officials who still have a good relationship with Mr. Karzai, arrived late Friday afternoon and headed straight to the presidential palace for meetings and a dinner. The evening concluded with him and Mr. Karzai taking a short walk together alone.

The two sides were “candid about their differences,” a senior State Department official said. But the “differences coming in were narrowed,” the official said, declining to elaborate.

Mr. Karzai raised the seizing of the Pakistan Taliban leader, the official said, adding that talks were to continue Saturday morning before Mr. Kerry left for Paris.

American officials sought to temper expectations that Mr. Kerry would walk away from Kabul with a final agreement.

“The negotiations were going on before he got here. They’ll be going on after he leaves,” the State Department official said. “What this is really about is building momentum for the negotiators.”

A complete American withdrawal would force the European allies to pull out as well, and would most likely lead to a steep drop in the billions of dollars in annual aid, which pays roughly 80 percent of Afghanistan’s bills and props up its biggest businesses, American and European officials have said.

The Taliban, meanwhile, remain a potent threat that Afghan forces are not yet ready to face entirely on their own. Most of the post-2014 force, as envisioned by American commanders, would be assigned to train the Afghans; a smaller element would be made up of Special Operations forces focused on targeting Qaeda operatives.

Mr. Kerry has previously cajoled Mr. Karzai into taking a deal he initially opposed. After the disputed 2009 election here, Mr. Kerry persuaded Mr. Karzai to accept a runoff against his leading contender for president. His rival then conceded the race as part of the deal brokered by Mr. Kerry.