It was often difficult to discern, doctors said, who was genuinely troubled, who was seeking attention and, most worrisome, who was in danger. “All of the leaders that I met were like, ‘No one is dying on my watch,’” Dr. Rosecrans said.

In 2004, after men began refusing food to protest their detention, she was asked to devise a protocol for evaluating the mental health of those on prolonged hunger strikes. Dr. Rosecrans believed that mentally competent people had the right to choose not to eat — even if that meant they would die. The American Medical Association and international medical organizations endorse that position. But the government has insisted on forced feedings, which are permissible in federal prisons. Detainees have described the procedures used at Guantánamo as particularly painful, with some likening them to torture.

Musa’ab al-Madhwani, a Yemeni captured in Pakistan and suspected in a terrorism plot, the evidence for which the United States eventually largely disavowed, joined a large group of hunger strikers in 2013 protesting conditions at the prison. He had arrived at Guantánamo in 2002, barely out of his teens, after being held at a C.I.A. prison. He had violent nightmares and other psychiatric problems after harsh treatment there, his medical records show.

Over the years, judges threw out his admissions during interrogations, finding they were tainted by mistreatment at the C.I.A. prison and coercive questioning at Guantánamo. But his detention stretched on, and after both of his parents died, Mr. Madhwani said in a letter to a federal judge that he was “utterly hopeless.” He added: “I have no reason to believe that I will ever leave this prison alive. It feels like death would be a better fate than living in these conditions.”

It was up to the psychiatrists and psychologists to decide how seriously to take such statements, and how to respond to them. “What do you do if they say they’re suicidal?” said Dr. Elspeth Cameron Ritchie, an Army colonel, now retired, and psychiatrist who was dispatched to Guantánamo in late 2002 after a spate of attempts. “Are they really suicidal, or are they manipulating the system?”

More than 600 “suicide gestures” had been recorded at Guantánamo by 2009, with more than 40 categorized as suicide attempts, according to a medical article. The doctors had to distinguish genuine attempts — reflecting desperation or, as American officials contended, a desire for martyrdom — from acts aimed at improving their conditions of confinement.

To date, at least six deaths have been have classified as suicides, though critics have raised questions about foul play in some cases. One Guantánamo commander referred to three of them, which were simultaneous, as acts of warfare against America. Several of the dead had been treated by mental health providers for serious disorders.