united-states
The legal duty to provide medical care in jails and prisons
Relief under federal constitutional law
U.S. law has a higher standard of care for inmates who have not been convicted of a crime and are awaiting trial, than it does for inmates who have been convicted of crimes, even though they are often housed together in the same jail facility.
In the case of someone awaiting trial, jail officials have liability if their negligence in failing to provide adequate health care for inmates causes harm (although some recent cases have cast doubt on this lower standard).
In the case of someone who have been convicted, jail officials have liability if harm is a result of their deliberate indifference for the well-being of the inmate. The leading cases establishing this legal standard are Estelle v. Gamble, 429 U.S. 97 (1976) (deliberate indifference to serious medical needs violates the Eighth Amendment) and Farmer v. Brennan, 511 U.S. 825 (1994) ( there is only liability if a prison official is aware of the facts from which an inference of a substantial risk of serious harm could be drawn, and actually reaches this conclusion).
Deliberate indifference is a very high standard for an inmate to prove. It basically amounts to proving that there was sadistic conduct by the guards, or that the government running the facility left the guards no choice but to act sadistically, knowing that this would happen. Basically, the law has decided that inmate's lives don't matter, and that civil rights are violated only by conduct amounting to torture.
The seriousness of the crime for which an inmate is incarcerated pursuant to a criminal conviction is irrelevant. It doesn't matter if the sentence is being served for shoplifting or mass murder. So, the fact that the offense is a non-violent victimless crime doesn't matter.
These standards are applied without regard to the budget of the jail facility.
If there is a legal duty to provide health care under these legal standards, and it isn't provided, then the jail officials forced to live within this budget may have a legal duty to ask for additional funds that are necessary for them to meet their legal duties, but are not responsible for failing to provide health care that the official don't have the funds to procure.
But, if the government that operates the jail has been told by jail officials (or other people, such as lawyers for the inmate) that additional funds are necessary for the jail to be operated in a way that meets its legal duties, and fails to do so, the government has legal liability for the harm that is caused by the government's failure to spend the money necessary to provide legally required medical care to inmates.
Relief under state law
In some U.S. states, there are also relevant state laws, such as one passed in New York State in the year 2022, which create additional rights to medical treatment not protected by federal law or the United States Constitution.
Remedies
Usually, the remedy if this care is not provided is to bring a lawsuit for money damages under 42 U.S.C. § 1983 (in state and local jails and prisons), or in what is called a Bivens action (in federal prisons). At best, a court might, in very rare cases, order that care be provided at government expense, in an injunction directed at the government operating the facility and the warden of that facility. A civil rights violation is almost never grounds to order than an individual inmate be released early from jail or prison due to their health concerns. If the inmate dies, the lawsuit can be brought by the inmate's next of kin.
Jails and prisons, with narrow exceptions not applicable to the facts described in this question, doesn't get to decide to release an inmate from prison early, although it can transfer an inmate (who remains in custody and often handcuffed to a hospital bed or kept in a locked and guarded hospital room) to a hospital temporarily for treatment. The decision regarding whether a convicted criminal defendant is incarcerated is made by a judge, primarily in a sentencing hearing.
Seeking relief in a sentencing hearing
At the sentencing hearing, the criminal defendant's lawyer can argue that incarceration is an unfair sentence because the defendant's health care needs can't be met there, so an alternative sentence, like community service or house arrest or probation should be imposed instead. But this only sometimes succeeds, and if the judge disagrees, the inmate's only option is to appeal that decision to a higher court for an abuse of discretion. The judge has no liability, and the defendant is incarcerated, often for months, until the appellate court makes a decision. By the time the appellate court rules, the damage will often already be done.
Compassionate release compared
Wardens can sometimes release inmates early in what is known as "compassionate release". But this is usually limited to terminally ill inmates who aren't a threat to the public who are inconvenient for a prison to care for while incarcerated (although, in theory, it can cover some other cases). And, it is usually completely discretionary. There is almost never a legal right to compassionate release.
This standard was temporarily relaxed on a temporary basis during the COVID-19 pandemic, but that one time exception has generally been discontinued.
The most common fact pattern
As a practical matter, the jail budget itself is almost never the problem. Jails and prisons generally outsource medical care for inmates to a third-party provided, usually, but not always, a government owned county hospital, either for a fixed price, or on a fixed fee for service rate that the government operating the facility agrees to pay. Usually, if health care is denied, it is denied because the guards don't take it seriously, or because there is a policy of the government operating the facility against providing a certain kind of care.
In practice, most of the lawsuits involve failure to provide medicine based drug withdrawal treatment (e.g., with methadone) which is not provided due to either guards not taking the withdrawal symptoms seriously, or due to a policy of the government running the facility against providing it as the medicines used are milder forms of controlled substances that are illegal without a prescription. See, e.g., here). See generally, this lengthy ACLU report from 2021 on the issue.
But not treating serious drug withdrawal symptoms with medical treatments such as methadone often leads to prolonged extreme agony often with lifelong disabilities or complications, and not infrequently, to the death of someone with untreated withdrawal symptoms. Many jail and prison guards in the U.S., however, don't receive much training and don't know this fact - and this ignorance can relieve them of liability under the relevant legal standard.