NEW YORK (AP) — Of the courtiers to Camelot’s king, Theodore C. Sorensen, who died Sunday, ranked just below Bobby Kennedy. He was the adoring, tireless speechwriter and confidant to President John F. Kennedy, whose term was marked by Cold War struggles, growing civil rights strife and the beginnings of the U.S. intervention in Vietnam.

Soaring rhetoric helped make Kennedy’s presidency a symbol of hope and liberal governance, and the crowning achievement for Sorensen was the inaugural address that was the greatest collaboration between the two and set the standard for modern oratory.

With its call for self-sacrifice and civic engagement — “Ask not what your country can do for you, ask what you can do for your country” — and its promise to spare no cost in defending the country’s interests worldwide, the address is an uplifting but haunting reminder of national purpose and confidence, before Vietnam, assassinations, Watergate, terrorist attacks and economic shock.

But to the end, Sorensen was a believer.

He was 82 when he died at noon at Manhattan’s New York Presbyterian Hospital/Weill Cornell Medical Center from complications of a stroke, his widow, Gillian Sorensen, said.

Sorensen had been in poor health in recent years and a stroke in 2001 left him with such poor eyesight that he was unable to write his memoir, “Counselor,” published in 2008. Instead, he had to dictate it to an assistant.

President Barack Obama issued a statement saying he was saddened to learn of Sorensen’s death.

“His legacy will live on in the words he wrote, the causes he advanced, and the hearts of anyone who is inspired by the promise of a new frontier,” Obama said.

Kennedy’s daughter, Caroline Kennedy, called Sorensen a “wonderful friend and counselor” for her father and all of her family.

“His partnership with President Kennedy helped bring justice to our country and peace to our world. I am grateful for his guidance, his generosity of spirit and the special time he took to teach my children about their grandfather,” she said in a statement.

Hours after his death, Gillian Sorensen told The Associated Press that although a first stroke nine years ago robbed him of much of his sight, “he managed to get back up and going.”

She said he continued to give speeches and traveled, and just two weeks ago, he collaborated on the lyrics to music to be performed in January at the Kennedy Center in Washington — a symphony commemorating a half-century since Kennedy took office.

“I can really say he lived to be 82 and he lived to the fullest and to the last — with vigor and pleasure and engagement,” said Gillian Sorensen, who was at his side to the last. “His mind, his memory, his speech were unaffected.”

Her husband was hospitalized Oct. 22 after a second stroke that was “devastating,” she said.

Some of Kennedy’s most memorable speeches, from his inaugural address to his vow to place a man on the moon, resulted from such close collaborations with Sorensen that scholars debated who wrote what. He had long been suspected as the real writer of the future president’s Pulitzer Prize-winning “Profiles in Courage,” an allegation Sorensen and the Kennedys emphatically — and litigiously — denied.

They were an odd but utterly compatible duo, the glamorous, wealthy politician from Massachusetts and the shy wordsmith from Nebraska, described by Time magazine in 1960 as “a sober, deadly earnest, self-effacing man with a blue steel brain.” But as Sorensen would write in “Counselor,” the difference in their lifestyles was offset by the closeness of their minds: Each had a wry sense of humor, a dislike of hypocrisy, a love of books and a high-minded regard for public life.

Kennedy called him “my intellectual blood bank” and the press frequently referred to Sorensen as Kennedy’s “ghostwriter,” especially after the release of “Profiles in Courage.” Presidential secretary Evelyn Lincoln saw it another way: “Ted was really more shadow than ghost, in the sense that he was never really very far from Kennedy.”

Sorensen’s brain of steel was never needed more than in October 1962, with the U.S. and the Soviet Union on the brink of nuclear annihilation over the placement of Soviet missiles in Cuba. Kennedy directed Sorensen and Bobby Kennedy, the administration’s attorney general, to draft a letter to Nikita Khrushchev, who had sent conflicting messages, first conciliatory, then confrontational.

The carefully worded response — which ignored the Soviet leader’s harsher statements and included a U.S. concession involving U.S. weaponry in Turkey — was credited with persuading the Soviets to withdraw their missiles from Cuba and with averting war between the superpowers.

Sorensen considered his role his greatest achievement.

“That’s what I’m proudest of,” he once told the Omaha (Neb.) World-Herald. “Never had this country, this world, faced such great danger. You and I wouldn’t be sitting here today if that had gone badly.”

Robert Dallek, a historian and the author of “An Unfinished Life: John F. Kennedy, 1917-1963,” agreed that Sorensen played a central role in that crisis and throughout the administration.

“He was one of the principal architects of the Kennedy presidency — in fact, the entire Kennedy career,” he said Sunday.

Of the many speeches Sorensen helped compose, Kennedy’s inaugural address shone brightest. Bartlett’s Familiar Quotations includes four citations from the speech — one-seventh of the entire address, which built to an unforgettable exhortation: “Ask not what your country can do for you; ask what you can do for your country.”

Much of the roughly 14-minute speech — the fourth-shortest inaugural address ever, but in the view of many experts rivaled only by Lincoln’s — was marked by similar sparkling phrase-making:

— “Let every nation know, whether it wishes us well or ill, that we shall pay any price, bear any burden, meet any hardship, support any friend, oppose any foe, in order to assure the survival and the success of liberty.”

— “If a free society cannot help the many who are poor, it cannot save the few who are rich.”

— “Let us never negotiate out of fear. But let us never fear to negotiate.”

As with “Profiles in Courage,” Sorensen never claimed primary authorship of the address. Rather, he described speechwriting within Kennedy’s White House as highly collaborative — with JFK a constant kibitzer.