KABUL, Afghanistan — The American diplomat most associated with the invasions of Iraq and Afghanistan says that American policy makers need to learn the lessons of the recent past as they weigh military options for the future, including for Syria and Iran:


¶ Remember the law of unintended consequences.


¶ Recognize the limits of the United States’ actual capabilities.


¶ Understand that getting out of a conflict once you are in can often be dangerous and as destructive for the country as the original conflict.


“You better do some cold calculating, you know, about how do you really think you are going to influence things for the better,” said Ryan C. Crocker, 63, the departing ambassador to Afghanistan and one of the pre-eminent American diplomats of the past 40 years. Even as he retires fighting an exhausting illness, Mr. Crocker cannot help keeping his mind at work on the crisis spots that have defined his career — in Pakistan and Afghanistan, Iran and Syria, Iraq and Lebanon.


Mr. Crocker, a wiry, intense man who for years was a dedicated distance runner, is retiring at the end of July after a career that began as the last American troops were leaving Vietnam and is ending as the curtain closes on an era of American state-building that has mostly fallen short of the results policy makers had hoped for.


In Iraq, the dream of a peaceful and democratic ally in the Arab world is giving way to a renewal of violence and an authoritarian government that lists toward Iran. In Afghanistan, the future is uncertain and hangs on dozens of “ifs” — if the elections are fair enough, if the Afghan security forces can fight off insurgents, if the government can become self-sufficient.


In the years ahead, Mr. Crocker sees, if anything, an increasingly fraught foreign landscape in a world set afire by war and revolution, a chapter bound to frustrate the best intentions and most sophisticated strategies of the United States. Although he speaks Arabic and has spent a lifetime immersed in the Arab world and Afghanistan, Mr. Crocker is deeply skeptical that Americans on foreign soil can be anything other than strangers in a strange land.


“We’re a superpower, we don’t fight on our territory, but that means you are in somebody else’s stadium, playing by somebody else’s ground rules, and you have to understand the environment, the history, the politics of the country you wish to intervene in,” he said.


Although publicly Mr. Crocker has sometimes presented the glass as half-full when assessing the situation in foreign countries, fellow diplomats say that his private analyses tend to be stark and unromantic — a vision shaped by his 38 years of experience in which he confronted over and over the limits of American power and the hostility of many in the world to what the United States stands for.


In 1983, while a political attaché in Lebanon, he was in his office when a delivery truck loaded with explosives slammed into the American Embassy in Beirut, killing 63 people, including 17 Americans. He was one of the first on the scene to walk through the smoldering wreckage looking for his colleagues.


A year earlier, in Lebanon, he was the American diplomat who walked through the Sabra and Shatila camps after 800 mostly Muslim refugees were slaughtered there by Christian militias. He used a transmitter to tell his colleagues back in Washington about the carnage around him.


In 1998, his residence was attacked while he was ambassador to Syria. Though he was away, his wife, Christine, was inside the house.


She has almost always traveled with him, even to war zones, and accompanied him to Baghdad despite the dangers. But she has not been with him in Afghanistan.


Most recently in Kabul, Mr. Crocker was in the embassy when it came under siege by suicide bombers armed with rockets who positioned themselves in an unfinished apartment building and shot at the embassy in an attack that lasted 19 hours.


With all that in mind, Mr. Crocker, who has a wry sense of humor, is generally leery of predictions in chaotic situations. “You know my hackneyed line — that an extreme-long-range prediction is a week from Thursday,” he said.