Brazilians are world famous for their friendliness and so the handshakes, hugs and high fives were not unexpected. But they were not much compensation for the fact that I was locked in a room full of Command Vermelho (Red Command) prisoners – one of the deadliest armed groups in the world today – and the only guard on duty was down the other end of the corridor behind a bolted metal grill.

I was being shown around a police lock-up in Rio de Janeiro by activists from Rio da Paz (Rio of Peace) who greeted the prisoners with equal enthusiasm. There were 600 people crammed into a two-wing block, each of which contained five cells with space for about 12 bunks in each. The heat was incredible and bodies were sprawled everywhere as people took it in turn to get some rest.

Despite the friendliness, the atmosphere of underlying violence was extremely apparent. Rio is an undeclared war zone. A few weeks ago 12 people died in a single day in gun battles between Comando Vermelho and Amigos dos Amigos (Friends of Friends) during which a police helicopter was shot down. The police killed 1,137 people in Rio last year, down from their record of 1,330 the previous year, but still comparable with the total killed in Gaza during Israel's last offensive or the annual total in Darfur in recent years.

Brazil's criminal gangs recruit most of their members in prison and organise many of their activities from there. Comando Vermelho controlled one of the wings that we were in while Amigos dos Amigos and Terceiro Commando (Third Force) were on the other. "They would not last five minutes if they found themselves on the wrong wing," I was told. "What if someone is not in a gang when they are arrested?" I asked. "The authorities just assign them to one in that case."

Viewed from this perspective the 200% increase in the number of pre-trial detainees being held in Brazil that has occurred in the last 10 years is quite alarming. Many of the people being held have only been charged with extremely minor offences – such as shoplifting – but administrative inefficiencies in the conduct of trials means that it is not uncommon for them to spend longer on remand than their final sentence. Many should not even be there at all. The Brazilian judiciary have recently reopened the files in a number of states and found that around 20% of the people currently in prison should be released and a further 30% moved to lower security.

Locking up petty thieves with hardened killers also provides the gangs with a steady stream of new recruits. Their leaders are responsible for the day to day administration of many prisons, controlling the distribution of food, medicine, and hygiene kits and enforcing whatever internal discipline exists. Two and half years ago the Primeiro Comando da Capital (PCC), São Paulo's most powerful crime gang, launched a series of co-ordinated attacks against police officers and prison staff in a protest over prison conditions, which resulted in around 450 killings. The PCC was initially formed by a group of prisoners to "avenge the death of 111 prisoners" who were killed during the suppression of a prison protest in 1992.

Brazil is a good country to be a criminal because police detection rates for crime are very low and the legal system is so convoluted that trials can be drawn out for years. Its laws and constitution provide a whole range of rights and privileges to those who can hire private lawyers to claim them, but its legal aid system is weak to nonexistent and around 80% of its prison inmates are too poor to afford a lawyer.

Documenting what is wrong with the current system is comparatively easy – although there are surprisingly few recent reports on the situation from groups like Amnesty International – it is knowing what to do about it that is the hard part. For years, human rights groups have called for new laws and institutions – which the Brazilian government is actually quite happy to introduce – but have largely ignored the dysfunctionality of the system as a whole. Brazil has entrenched the full range of human rights into its constitution and has some of the most liberal penal laws in the world. The problem is that – faced with one of the world's highest rates of violent crime – the people responsible for enforcing these laws are conspiring together to ensure that they don't work.

Groups such as Rio da Paz and the Catholic church's Pastoral Carceraria, who are prepared to visit prisoners and provide them with basic assistance, are very few and far between, but with the state almost completely abdicating its responsibilities for penal management they are the only chance that most prisoners have got. Prison numbers are increasing across the country at a rate of 3,000 a month, so the pressure is now building at an alarming rate.

It costs money to keep people locked up and money spent on prisons cannot be spent on developing alternative programmes. Imprisoning people for relatively minor offences has also been shown to be counterproductive and more likely to turn them into repeat offenders than a non-custodial sentence. There are, therefore, strong utilitarian reasons for trying to reduce the number of people that are sent to prison in Brazil and to use imprisonment only as a last resort. However, Brazil is not the only country in the world to be pursuing the opposite policy. Prison numbers are going up across the world and Brazil offers a frightening glimpse into what the future consequences of this could be.