Pakistan and the United States agreed Thursday to resume broad partnership talks that were suspended two years ago amid widespread anger here over American drone attacks and other perceived slights to Pakistani sovereignty.

Still, Secretary of State John F. Kerry said during a visit to the Pakistani capital that the drone strikes will continue as long as the United States sees the need.

Kerry said he is aware of the sensitivity of the issue but warned Pakistanis that the terrorists in their midst are the ones who “violate the sovereignty of this country.”

Later, in an interview with Pakistan TV, he said there is a timeline for ending drone strikes.

“I think the program will end as we have eliminated most of the threat and continue to eliminate it,” Kerry said. “I think the president has a very real timeline, and we hope it’s going to be very, very soon.”

That is more specific than anything that U.S. officials have said before, but it still allows the Obama administration discretion to continue a practice that has become a symbol of heavy-handed U.S. military and counterterrorism tactics around the world.

Past Pakistani leaders have quietly cooperated with drone strikes, allowing the aircraft to fly from a base inside the country even while publicly condemning the operations.

Announcing the modest accomplishment of new talks, Kerry also said that Pakistan’s new prime minister, Nawaz Sharif, is expected to make an official visit to Washington in about a month, at President Obama’s invitation.

The administration is trying to get off on the right foot with Sharif, whose election this year marked the first peaceful transfer of power in Pakistan from one democratically elected civilian government to another. Kerry’s two-day visit to Islamabad highlighted civilian cooperation on development, education and energy issues.

But the military and counterterrorism relationships with nuclear-­armed Pakistan are the crux of U.S. interests here — and the biggest sources of friction in a mutually mistrustful relationship that has careened from crisis to crisis for years.

“America does not want to have a transactional relationship,” Kerry said at a news conference Thursday. “We do not want to have a relationship based on the moment” and focused solely on fighting terrorism or dominated by the war in neighboring Afghanistan.

High-level cooperation on a range of issues, including energy and security, was suspended in 2011, after three sessions between then-Secretary of State Hillary Rodham Clinton and her Pakistani counterpart.

Sharif’s foreign policy adviser, Sartaj Aziz, said Thursday that the next such session would be held within six months. He made a point of saying that Pakistani officials had reiterated to Kerry their long-standing opposition to U.S. drone strikes inside Pakistan — operations that U.S. officials say are a necessary counterterrorism measure because Pakistan will not do the job on its own.

“Drone attacks are counterproductive in terms of our relationship,” Aziz told reporters at an appearance with Kerry. ”Discussion will continue, this dialogue, on how to stop this policy of drone attacks.”

The number of U.S. drone strikes inside Pakistan has continued to decline this year, and the targets appear to be more limited to senior terrorism suspects than they were in the past. A tally by the New America Foundation lists 16 strikes in 2013, compared with 48 in 2012, 73 in 2011 and 122 in 2010.

The influential governmental role of Pakistan’s military — long the real arbiter of national decision-making — hung in the background during Kerry’s visit. The Pakistani military for years has ignored some of the extremist groups that pose the greatest risk to U.S. forces in Afghanistan, despite pleas from American officials. At the same time, it has pursued militant groups that have waged a long-running insurgency at home.

Like numerous top U.S. officials before him, Kerry is trying to convince Pakistani leaders that all extremists within the country pose a risk to the Islamabad government. Successive leaders in Islamabad have been unresponsive to that argument, and U.S. intelligence agencies report that the Pakistani military or intelligence services maintain links to militant groups.

A Taliban assault on a prison this week freed hundreds of inmates, including militants, and called into question Pakistan’s ability to effectively fight the insurgency. Prison guards and local military posts appeared overwhelmed or caught off guard.

Kerry is the first U.S. secretary of state to visit Pakistan since 2011, when the always-fractious relationship sank to a new low. Crisis piled atop crisis that year — the cresting of Pakistani anger over drone strikes, the fatal raid on Osama bin Laden’s secret compound near Islamabad and the detention of an American CIA contractor after a shooting.

An errant U.S. military strike that killed Pakistani soldiers along the border with Afghanistan opened a new rift and led Pakistan to cut off vital ground-supply routes for U.S. forces for months. The routes reopened last year, and U.S. officials traveling with Kerry said relations have slowly improved.

Sharif has shown some willingness to expand cooperation with the United States but has moved slowly. Kerry said he was encouraged by the Pakistani prime minister’s resolve to confront extremists, but Sharif has not pledged to eradicate havens for the terrorists who cross into Afghanistan.

The roughly $1.5 billion a year in U.S. aid to Pakistan that Kerry helped negotiate as a senator has not given the United States much visible leverage, and it is expected to be cut back once the package deal expires next year.