After 13 months in a series of stinking Peruvian jails and $18,000 in payoffs and bribes; James Gordon Weinnig made his escape. One night four weeks ago, he broke away from his guards, dodged bullets and finally crossed the Peru-Ecuador border dressed as a woman.

Two weeks ago, Weinnig stepped off a plane at Washington National Airport, a free man, and home at last.

"It was a matter of life and death, right?" he said earlier this week, sitting in his Arlington apartment with his girlfriend, who helped arrange the escape. "I had to do it. I just couldn't stay there any longer."

Weinnig's story, which is confirmed by a Virginia congressman, the State Department and the Drug Enforcement Administration, began in March 1979 when he went to Peru to smuggle 25 ounces of nearly pure cocaine -- for which he paid $5,000 -- back to the United States.

It was, he says today, the stupidest thing he has done in his life.

Weinnig had already picked up the cocaine, but was still nervous on the morning of March 22, as he walked back to his hotel room in Cuzco, an acient city of winding streets in the middle of the Andes Mountains in southern Peru.

He had just picked up his plane ticket and was returning to gather up the rugs he had bought for resale in the United States and the cocaine -- hidden in the false bottom of one of his suitcases -- to leave for the airport and home.

As he walked into the hotel courtyard, his mind whirling with details, a dozen men leaped at him, screaming for him to stop and to put his hands in the air. Some grabbed him. Others, he says, began to kick and beat him. Even though he spoke no Spanish, Weinnig knew immediately what had happened.

He had been caught.

Abruptly, Weinnig went from the successful operator of a vending machine business in Washington to a statistic: he became one of more than 700 Americans imprisoned on drug charges around the world.

The men pushed and dragged him up to his room, which they ransacked until at last they found the cocaine.Present during the arrest and interrogation, he said, was an American agent of the Drug Enforcement Administration, a charge the DEA headquarters here denies.

"When they found the drugs, they all started snorting it. They gave me some and for an hour we had a good time."

Then the men took him to the jail in Cuzco the first of several in which he would spend time, Weinnig said.

Over the next two weeks, Weinnig says he was kicked, beaten and tortured by police who were intent on winning a confession from him, allegations that cannot be confirmed. The state Department has received statements attesting to similar treatment in Peruvian prisons.

Eventually he was transferred to a jail in Lima. There, he said, he gave in and signed a confession that states that he had 664 grams of cocaine on him at the time he was arrested. "I actually bought 740, but the Peruvian agents must taken the rest," he said.

To celebrate, the police called in the Peruvian press and announced that they had broken a major drug ring. The police dubbed Weinnig "Bello Antonio" ("Beautiful Anthony"). It had a nice Italian ring to it, Weinnig said, and police were making strong hints that Weinnig's actions were all part of a Mafia operation. In addition, news accounts of the press conference quote police as saying Weinnig had 20 kilos of cocaine -- or more than 20 times as much as what he had confessed to having.

Meanwhile, almost two weeks after he had been arrested, the State Department notified his girlfriend, Carol Lee, that Weinnig had been arrested. Frantically, she began telephoning anyone who could tell her more. But because Weinnig was being held incommunicado, there was no additional information.

After his confession, Weinnig was transferred to a small jail for a week and then to the main Peruvian prison, Lurigancho, on the outskirts of Lima. The prison, modeled after American prisons, was designed for 1,500 people, Weinnig said. But there were 7,000 inmates, and he swiftly learned that, to survive, you needed money.

"I had no money for two months, even though Carol sent me $150 right away," he said. "Those were the two worst months of my life."

Without money, a prisoner could not sleep on a bunk, he said. A $20 bribe to the prison guards was necessary to get a bunk. A straw mattress cost $1, Weinnig said. To walk from one part of the prison to another -- even on official business -- prisoners had to pay guards to open the gates.

The prison food consisted of a piece of bread in the morning, and a handful of rice and beans at lunch and at dinner -- if you were lucky enough to be at the head of the line Weinnig said. There was never enough food to go around, so as the quantities began to run low, the prison guards would back off and the prisoners would have daily fights over what was left.

Water was turned on 15 minutes a day, and it was thick with worms and amoebas. Before it could be drunk it has to be boiled, Weinnig said.

Eventually, his girlfriend was able to come to Peru to visit him. By the time she arrived, he had figured out a way to make the money they would need to finance his escape.

"I was always thinking about escape," he said. "Always."

The plan was this: the Indians in the prison made rugs for sale to the outside. Many were beautiful, and in the United States they would sell for as much as 10 times what he would have to pay his fellow prisoners for them.

Over the next 10 months, Lee visited him three times in Peru, staying for a month or two at a time, and then returning to the United States with rugs to sell. It was a profitable business, and eventually it would provide the cash he needed to bribe and pay his way to freedom.

By the end of 10 months, Weinnig had met enough people and become respected enough within the prison to develop the kind of Peruvian underworld contacts that would enable him to plan an escape.

"The key," he said, "is never to pay money up front. If you do, it's a rip. You're never going to get out. You get them to do it, and then you pay them."

To succeed, Weinnig's plan called for him to be able to visit a court for a hearing. That way he would be on the outside. The problem was that, in 10 months, he had yet to have a hearing on his case. It would cost him $1,000 in bribes to arrange a hearing, he learned.

Finally, in April, after being transferred to a smaller jail in Lima, everything was perfect. Weinnig bribed two guards to drive him to his hearing in a taxi instead of the prisoner van, and from there to a small restaurant. There, he told the guards, he would treat them to dinner and drinks and then they could take him back to the prison.

It was the evening of April 14, 1980. Weinnig, who for months had been jumping rope religiously to get in shape for his freedom run, sat picking nervously at his ceviche -- fish marinated in lemon juice -- while his guards chatted gaily over wine and beer.

Weinnig kept looking out the window as nonchalantly as he could. At last, in the fading light that bathed Lima in red, he caught the signal he had been waiting for: a motorcyclist a few hundred feet away was wagging his headlight to and fro.

Then the two guards, unknowing, led Weinnig to the street and the waiting cab. As he was about to step inside, he shoved one of the guards out of the way and ran down the street. Behind him, the guards screamed for him to stop and then began shooting. Bullets ricocheted off the concrete at his feet, spewing sparks.

Like a scene out of a Western where the cowboy leaps onto a running horse, Weinnig scrambled aboard the racing motorcycle. Within minutes he was whisked to a safe house.

Over the next two weeks, the final plans were laid for his escape over the border. Finally, on April 27, after dressing as a Peruvian and riding public transportation to the Ecuadorian border, he changed his clothes to those of a woman and crossed a little-guarded section of the frontier into Ecuador.

Today, Weinnig, who lost 30 pounds during his prison stay, says he has been told by the State Department that he will not be extradited.

A State Department spokeswoman said yesterday that persons charged with drug offenses are, in principle, extraditable, but there was no one available late yesterday familiar with the status of extradition commitments between this country and Peru.

"It was the first time I ever did anything like that," Weinnig said. "And you can bet it will be the last time."