Updated

U.S. Secretary of State John Kerry flew to Afghanistan Friday for urgent talks with Afghan President Hamid Karzai as an end of October deadline looms for completing a security deal that would allow American troops to remain in Afghanistan after the end of the NATO-led military mission next year.

Kerry's unannounced visit to Kabul comes as talks on the Bilateral Security Agreement have foundered over issues of Afghan sovereignty despite a year of negotiations.

The U.S. wants a deal by the end of the month, but the discussions have stalled over Karzai's demand for American guarantees against future foreign intervention and U.S. demands for any post-2014 residual force to be able to conduct counterterrorism and counterinsurgency operations.

U.S. officials insist they are optimistic about a deal but the continuing deadlock leaves it doubtful that any agreement will be reached by month's end. If no deal is signed there will be no U.S. forces in Afghanistan after 2014.

Officials traveling with Kerry told reporters aboard the secretary's plane that the U.S. continues to believe the Oct. 31 deadline is "doable and desirable" and that failing to meet it would create significant problems.

They said uncertainty caused by the lack of an agreement by the end of the month would make it more difficult to plan the next phases of withdrawal from Afghanistan and could erode the resolve of NATO allies that are considering leaving troops there for training.

Without the United States on board, it is unlikely that NATO or any of its allies would keep troops in Afghanistan. Germany has already indicated it will not commit the 800 soldiers it has promised.

"That's why we're pressing," said one of the officials traveling with Kerry.

However, the officials, who spoke on condition of anonymity because they were not authorized to publicly preview Kerry's discussions with Karzai, stressed that Kerry is not expecting to clinch an agreement during his visit.

Instead, the trip, which Kerry and Karzai set up in an Oct. 5 phone call, is meant to build momentum for the negotiators who will continue their talks after Kerry departs, they said.

The atmosphere surrounding the talks has been soured by recent angry and emotional comments from Karzai complaining about the conduct of NATO forces.

Earlier this week, Karzai alleged that the U.S. and NATO inflicted suffering on the Afghan people and repeatedly violated his country's sovereignty. The comments drew a sharp response from NATO's secretary-general who recalled how much the alliance has bled in Afghanistan.

Nearly 3,390 members of the NATO coalition have been killed since the U.S. invasion, which marked its 12th anniversary on Oct. 7. They include at least 2,146 members of the U.S. military.

There currently are an estimated 87,000 international troops in Afghanistan, including about 52,000 Americans.

The U.S. wants to keep as many as 10,000 troops in the country to go after the remnants of al-Qaida, but if no agreement is signed, all U.S. troops would have to leave by Dec. 31, 2014.

The agreement would give the U.S. a legal basis for having forces in Afghanistan after that date and also allow it to lease bases around the country. It would be an executive agreement and not a treaty, meaning the Senate would not have to ratify it.

President Barack Obama told The Associated Press in an interview last week that he would consider keeping some American forces on the ground after the conflict formally ends next year, but acknowledged that doing so would require an agreement. He suggested that if no agreement can be reached, he would be comfortable with a full pullout of U.S. troops.

Roughly 95 percent of the dozen-page agreement is complete and the rest is penciled in until the two sides can agree on language, U.S. officials say.

The officials with Kerry said the positions of the two sides on the remaining issues are not incompatible. But they described the differences as "complex" and not easily overcome.

Afghanistan wants American guarantees against future foreign intervention, a veiled reference to neighboring Pakistan. Afghanistan accuses its neighbor of harboring the Taliban and other extremists who enter Afghanistan and then cross back into Pakistan where they cannot be attacked by Afghan or U.S.-led international forces.

The second sticking point is about the role and conduct of the counterterrorism force the U.S. wants to leave behind. Karzai has said the Afghan people cannot allow foreign troops to attack and kill Afghans on Afghan soil.

Karzai is calling a meeting of Afghan tribal elders to advise him on whether to sign a security deal. But that conference will not be held until November, Afghan officials say.

If they endorse the agreement, then Karzai has political cover to agree to it. He is keenly aware that previous leaders of his country historically have been punished for selling out to foreign interests and wants to make sure that any U.S.-Afghan agreement is not seen in that context. Karzai, who cannot run for a third term, is slated to step down at the end of next year -- the same time nearly all international troops are to have left the country.

In addition to the security talks, Kerry will be seeking assurances from Karzai on preparations for the election to replace him, the officials said.