WASHINGTON — Month after month, as American diplomats pushed for the release of Brittney Griner and Paul Whelan from Russian prisons, they received the same infuriating answer: If you want both prisoners, we want Vadim Krasikov as part of the deal.

Krasikov is an assassin who murdered Zelimkhan Khangoshvili, a Chechen separatist commander who fought against Russian forces in the early 2000s, in a park in Berlin in broad daylight in 2019, a brazen killing that German authorities say was committed at the behest of Moscow’s intelligence services. Convicted and sentenced to life in prison in Germany, Krasikov was not in U.S. custody to be traded to Russia.

It was, the Americans thought, hardly a viable request for a swap that would include Griner, a WNBA star, and Whelan, a former Marine, who were being detained on what Biden administration officials considered trumped-up charges. American officials felt out their German counterparts to see if they might agree and were hardly surprised when Berlin refused to release what they considered a cold killer.

Trying to be creative, the Americans even explored some sort of three-way deal that would give the Germans something in return, but that did not go anywhere, either.

Privately, some of the administration’s diplomats concluded that the insistence on freeing Krasikov was a stalling tactic by President Vladimir Putin of Russia, who they believed was determined not to deliver any kind of political victory to President Joe Biden before the midterm elections in the United States last month.

Others believed the Russians were serious and saw it as a face-saving way for Moscow’s security services to give up Whelan, whom they convicted of espionage despite flat denials from Washington that he was a spy.

Either way, how Biden came to agree to a swap that freed Griner but not Whelan was a tale of feints and intrigue carried out through secret negotiations and public posturing, all against the backdrop of a brutal war in which U.S.-armed Ukrainians were battling Russian invaders. At the end of the day, according to senior U.S. officials directly involved in the negotiations who spoke on the condition of anonymity to describe delicate diplomacy, it left the president with the unpalatable choice of liberating one American while leaving another behind.

Griner arrived in the U.S. early Friday, landing in San Antonio, where she received medical evaluations at Brooke Army Medical Center and reunited with family, a relief for Biden and his team. Whelan remains in a dismal Russian prison, his long-term fate unknown as his frustrated family waits and while the president vows to redouble efforts to bring him home.

The outlines of a possible agreement were on the table as far back as last spring. Interacting through intelligence agencies, the Russians made clear that they were willing to trade Griner for Viktor Bout, a notorious Russian arms dealer arrested in Thailand in 2008 and serving a 25-year sentence in a U.S. prison. But they were not willing to include Whelan in the package deal.

To the Russians, trading Griner for Bout was a swap of two criminals, according to U.S. officials. Whelan, on the other hand, was supposedly a U.S. agent, in the Russian telling, and so only another agent or someone of equivalent importance would merit giving him up. The Americans do not currently have a Russian spy in custody to trade. And that is where Krasikov came in.

Frustrated by the demand for a swap they could not make, American officials broke with years of tradition by telling reporters that they had made a “substantial” offer to the Russians, making clear that Biden would trade Bout for Griner and Whelan. They hoped the public pressure would move the Russians off their insistence for Krasikov and into a deal.

It did not — for many months.

But just after Election Day, Russian contacts delivered a new message, raising the possibility of excluding Whelan from a deal and instead focusing exclusively on Griner. If that were the case, the Russians said, Moscow might consider Bout a fair trade.

The disparity of their offenses was vast: Bout was an illegal arms merchant to some of the world’s most violent forces, including some intent on killing Americans; Griner was detained for traveling with vape cartridges containing hashish oil. But it was the first time that the Russians had made what the American diplomats considered a real counteroffer, if they were serious about following through.

At a meeting in the Oval Office early last week, Biden was ready to sign off.

Wary of undercutting the deal, the American diplomats carefully made one last appeal for Whelan, asking the Russians if there was anyone other than Krasikov whom they might want in exchange for both Whelan and Griner. They got a firm no, but the Russians did not use the effort as an excuse to back out of the developing arrangement for Griner.

Still worried that the deal could fall apart at the last minute, Biden waited to sign Bout’s clemency until the last minute.