In blistering 100-degree heat one recent afternoon at Valley State Prison in California’s Central Valley, inmates crowded around small windows in a prison yard to pick up their daily doses of buprenorphine, an opioid addiction medication.

At one window, Quennie Uy, a nurse, scanned inmate identification cards, then retrieved strips of the medication, slipping them through a sliding panel below the window. One by one, inmates deposited the strips in their mouths, then flashed their palms — proof they had not pocketed the drug that was helping to stanch their cravings.

The daily ritual is part of a sprawling health experiment in California that aims to unwind the often lasting damage of opioid use before, during and after incarceration. The state’s efforts also reflect the beginnings of a potential transformation in the nation’s approach to treating addiction in a part of American society that is often neglected.

“For the first time, there is a trend toward expanding access to treatment in jails and prisons,” said Dr. Justin Berk, an addiction medicine physician at Brown University and the former medical director for Rhode Island’s Department of Corrections. “There’s this better understanding that if we’re going to treat the opioid overdose crisis, one of the high-target populations to treat is people in jails and prisons.”

The federal government estimates that a majority of incarcerated Americans have substance use disorder, many of them with opioid addiction that can be complicated to manage in the age of potent synthetic opioids like fentanyl. Deaths in state prisons from drug or alcohol intoxication rose by more than 600 percent from 2001 to 2019, according to the Justice Department.

But addiction treatment is still only sporadically available in the nation’s prisons and jails. As of 2021, only about 630 of the roughly 5,000 correctional facilities in the United States provided medication for opioid use, according to the Jail and Prison Opioid Project, a group led in part by Dr. Berk that studies treatment among incarcerated people.

The Biden administration is seeking to change that, aiming to increase the number of prisons and jails offering opioid addiction treatment and working to install treatment programs in all federal prisons by this summer. In Congress, lawmakers from both parties are seeking to expand coverage of treatment in the weeks leading up to an inmate’s release.

Not treating opioid addiction in correctional facilities, said Dr. Ruth Potee, the medical director for the Franklin County Jail in Western Massachusetts, is “like running a psychiatric hospital without treating psychiatric disease.”

In 2019, California prisons recorded their highest rate of overdose deaths and the highest overdose mortality rate for a state prison system nationwide. The same year, state lawmakers approved a far-reaching plan by Gov. Gavin Newsom, a Democrat, for substance use treatment in prisons.

Today, the state is one of only a small number in the nation with a comprehensive treatment program across its prison system, an effort that has led to a significant reduction in overdose deaths. The program is costly, with a budget of $283 million for the current fiscal year. But in January, California became the first state to secure permission from the Biden administration to use Medicaid for health care in correctional facilities, which will allow officials to use federal funds to cover opioid treatment.

People who are incarcerated have a constitutional right to health care. But the standards of care can vary between states, said Regina LaBelle, who served as the acting director of the Office of National Drug Control Policy under President Biden. Some inmates can get treatment only if they also received it before incarceration, while others move between jails and prisons without consistent treatment. Shorter jail stays can frequently lead to withdrawal symptoms.

At Valley State Prison in Chowchilla, Calif., near vast fields of almond trees northwest of Fresno, inmates are screened for substance use upon entering the facility, allowing staff members to prescribe buprenorphine early in a prisoner’s sentence.

Medication, inmates said, has allowed them to become more engaged students or employees in the prison. But there is still reluctance among some in need of treatment to use it, said Alberto Barreto, an inmate who counsels others on their substance use.

Prison staff members and inmates need to “help them get to where they feel comfortable enough to at least listen to somebody else talk about their addiction,” he said as he leaned against the bathroom in a cell he shares with several inmates.

Current and former inmates in California said in interviews that visitors could still sometimes smuggle opioids into state prisons. Some inmates said that scents like vinegar or those emanating from machinery could prompt memories of or cravings for drugs.

The penal culture of incarceration can also lead to suspicions around drug use that discourage treatment, some inmates said. Carlos Meza, a Valley State Prison inmate doing push-ups in a prison yard on a recent morning, said he overdosed twice on fentanyl in a different prison, prompting staff members at the facility to suspect he was suicidal. He just wanted a high, he told them. They eventually started him on addiction treatment, Mr. Meza said.

At Valley State Prison, addiction treatment is paired with group behavioral therapy. The same morning that Mr. Meza did his push-ups, a group of inmates lined the walls of a small classroom to practice the act of apologizing, including in one scenario in which an inmate stole part of someone’s daily allotment of phone time.

Across the hallway, with textbooks sprawled across desks, another class discussed the science of substance use, an effort to understand the roots of addiction.

Going without treatment can leave the incarcerated vulnerable to recidivism once free, some inmates said. “They go hand in hand — they’re intertwined,” said Trevillion Ward, an inmate who works in a prison cafeteria, referring to how drug use can increase the risk of incarceration. Mr. Ward said he relapsed on drugs and was back in prison roughly three years after finishing his first prison sentence.

“I didn’t have any coping skills to go out and deal with life stressors,” he said. “And as a result, as soon as things got serious and hectic, I turned back to drugs.”

People in jails and prisons are especially vulnerable to fatal overdoses shortly after they are released, when tolerance for potent opioids like fentanyl can be weaker.

When inmates leave Valley State Prison and other state prisons in California, they are offered naloxone, and those being treated for opioid addiction also receive a 30-day supply of buprenorphine. That continuity is needed for treatment to be effective, said Dr. Shira Shavit, a physician at the University of California, San Francisco, and the executive director of the Transitions Clinic Network, a set of clinics offering health care to people who have left jail or prison.

The shift to the outside world can be harrowing, with parole-mandated activities squeezed among the responsibilities of working, moving into housing, securing benefits and attending medical appointments.

Robert Banuelos, who left a California prison in June, said that when his 30-day post-release supply of buprenorphine ran out, an urgent care clinic near San Diego could not confirm his insurance status. With the help of Sharon Fennix, who operates a hotline for the Transitions Clinic Network after spending nearly 40 years in prison, he verified that he had Medicaid. More recently, Mr. Banuelos moved to Los Angeles and struggled to obtain a new buprenorphine prescription, worried that any break in treatment could lead to a relapse.

“The loneliness is scary,” he said of his efforts to find friends and a job. Even with his daily strips of buprenorphine, he added: “I feel like I can’t move. I feel like my hands are tied up behind my back.”

On a recent afternoon, Delilah Sunseri, a wedding bartender who spent time in prison and now lives in her car, reported to a mobile health clinic in San Jose where health workers were administering injectable buprenorphine to formerly incarcerated patients. Ms. Sunseri was there for her monthly dose of the medication.

Ms. Sunseri said she chose to live in her car because she was worried about living near other drug users, whether at a friend’s house or in transitional housing.

“There’s people out there that are like: ‘Oh, you did this to yourself. You know, you got yourself in this mess. You need to get yourself out of it,’” she said. “But it’s a disease.”

Just before she arrived at the clinic, her daughter Blaise Sunseri received the same injection, determined not to relapse with fentanyl. The younger Ms. Sunseri had spent time in a series of California jails, she said. For both women, it took treatment after being released to stabilize their drug use. Delilah Sunseri said that addiction medication was not available during her time in prison, where she said inmates would overdose in the yard and die.

Treatment upon release is like a “safety net,” said Nicholas Brady, a recent jail inmate who received a buprenorphine injection at the San Jose clinic.

During his time in jail, he said, he saw inmates vulnerable to immediate relapse. Some people would plot their drug use for when they left jail, thinking they could avoid overdosing, he said. Inmates spent their time “thinking about it, fantasizing about it,” Mr. Brady said.

Karen Souder, a former food truck owner, has been piecing her life together after a prison sentence with the help of buprenorphine, which she stayed on after her release with Dr. Shavit’s help. The medication “really makes me be able to go throughout my day,” said Ms. Souder, who now cleans roads for California’s Department of Transportation.

Stable on buprenorphine, Ms. Souder said she found joy in the freedom to take a bath or put on makeup.

The day she was released this year, she drove to lunch with a woman who helped run a gardening class she took in prison. At the Red Lobster where they dined, Ms. Souder spotted flowers and plants outside the restaurant, marveling at their beauty. The sky was blue. They took a photo in front of the plantings. There were no fences surrounding them, Ms. Souder said. “We sat there for a minute,” she added, “and just took a deep breath.”