On Costa Rica, inside a wheel of sticky silk, an orb weaver spider is doomed to die. She is a formidable predator, but this Leucauge argyra has fallen victim to something even worse. A white grub is stuck to her abdomen, and it's growing bigger while the spider shrinks.

One day, the spider stops building her usual orb web. Instead, she keeps repeating the first few steps, creating a "cocoon web": a bag, strong enough to withstand Costa Rica's heavy tropical rains.

The large grub now turns ruthless. It sucks out the spider's remaining life juices and throws out the skinny remains. Then it spins a cocoon for itself, supported by the cocoon web. A few weeks later a wasp emerges, with a slender waist and metallic sheen. The grub has grown up.

The ghoulish wasp is called Hymenoepimecis argyraphaga. Its larva injected a chemical into the spider to control its actions, making it a slave.

The wasp's way of life may seem like a pretty raw deal for the spider. But in fact this sort of behaviour is astonishingly common. The wasps that lay their eggs on the bodies of other animals may well be the most diverse group of animals on Earth. Nature, it seems, loves animals that play dirty.

These wasps lay eggs onto other animals, which their larvae then feed on. In many cases, the hosts are still alive when their insides are being eaten. This ensures a living larder chock-full of fresh food.

When the larvae are ready to pupate, they normally kill their host. Until this point, they are completely dependent on their hosts for nourishment and protection.

This lifestyle has an unusual name. The wasps are not strictly parasites like tapeworms, as parasites don't as a rule kill their hosts. But they also don't kill their hosts as quickly and surely as a predator like a lion would. They are somewhere in between: "parasitoids".

They infest a host of small critters, from social insects like ants and bees to flies and caterpillars.

There are probably more species of them than any other kind of animal on Earth

All parasitoids specialise in attacking one host species, usually during a particular stage of its life cycle. This specialisation has given rise to a huge diversity of parasitoids.

Estimates about the number of parasitoid species vary widely: most are tiny, so there are many undiscovered species. Some put the cap at 2 million.

"There are probably more species of them than any other kind of animal on Earth," says Andrew Forbes of the University of Iowa in Iowa City. "If you stop and think about the sheer number of animals on Earth that make a living by laying eggs in other animals and doing all sorts of horrible things to them before consuming them alive, it can give one pause."

The ancestral parasitoid wasp was probably similar to modern sawflies, which feed on dead wood that has been digested by symbiotic fungi. It may be that one species lost the fungi, so started killing other species that did.

The first order of business for an adult parasitoid is to find a suitable host. She first identifies its preferred habitat, which has a distinct look and smell. Then it's a matter of laying an egg appropriately.

The wasp may stab the caterpillar in this way a dozen times

To pull this off, most parasitoid wasps have a needle-like organ with which to stab their hosts. For instance, Iseropus wasps thrust their eggs into the larvae of Hemerocampa moths. In a 1929 lecture, the entomologist Robert Cushman described the attack in detail.

Iseropus attacks the Hemerocampa caterpillar late in its life, when it has already built its cocoon in an elm tree. The wasp lands on the tree and feels around for the cocoon.

"Eagerly she tests it all over until, apparently satisfied that it suits her purpose, she takes a firm hold with her feet, arches her abdomen until the stiletto points perpendicularly toward the cocoon, then thrusts it through the meshes," according to Cushman. The wasp may stab the caterpillar in this way a dozen times.

Other species have to work far harder. Their hosts can fight back.

The startling, metallic Lasiochalcidia pubescens (sometimes L. igiliensis) lays her eggs on the larvae of antlions. That sounds like a monumentally bad idea, because antlion larvae are ferocious predators.

She provokes the antlion to attack her legs

They dig small pits into sand, then bury themselves at the bottom. When a luckless ant enters the pit it slides down the side, and the antlion grabs it in its powerful jaws.

Despite the risks, L. pubescens has evolved to lay her eggs in the antlion's throat.

She provokes the antlion to attack her legs with its mandibles. When the antlion grabs on, she holds its jaws apart using her muscular legs, and carefully injects an egg into the membrane of the antlion's exposed throat.

Other parasitoids are far more cautious. They don't even approach their hosts, and instead leave eggs for them to find.

One family, the Eucharitids, use ant larvae as hosts. They lay their eggs on plants near ant nests. When the wasp larvae emerge, they sit around until they encounter an ant heading back to its nest.

Other parasitoids must venture into truly dangerous territory

Once inside the ant nest, the parasitoid larva attaches to an ant larva. It emits a chemical bouquet so similar to its ant hosts that the ants accept its presence.

Even when it becomes an adult wasp, the ants treat it as one of their own, grooming and feeding it. Before the ant-mimicking odour wears off, the wasp emerges from the nest and leaves.

Other parasitoids must venture into truly dangerous territory to find their hosts. One Japanese species has learned to scuba-dive.

Agriotypus gracilis lays her eggs in the pupae of a caddisfly called Goera japonica. Like all caddisflies, these larvae build protective cases out of silk and sand grains. They also live 6-15 inches underwater.

Larvae that develop inside a host's body need to obtain some air

To reach one, the female wasp crawls down a plant stem or the side of an exposed stone. She can survive about 14 minutes underwater. Her thick hair forms a sort of bubble that allows her to breathe.

After laying an egg, the female floats to the surface to look for another pupa. Once the larvae hatch, they usually eat up their host.

Larvae like these have it easy. They are outside the host's body, so can breathe normally. However, larvae that develop inside a host's body need to obtain some air.

Encyrtus infidus is a parasitoid on the scale insect Lecanium kunoensis (sometimes called Eulecanium kunoense). Many parasitoid larvae develop on one scale larva, and use it as a buffet.

They turn their hosts into their personal bodyguards

At first, the larvae remain attached to the egg from which they hatched by a stalk. This helps them get their air. Later, the scale's innards get crowded, the larvae start competing for space, and the stalk gets cut. But the wasp larva has a solution.

The scale larva has a network of tubes that supply air throughout its body, called trachea. Each trachea ends in an opening called a spiracle, through which the scale larva exchanges air with its surroundings.

When the parasitoid larva loses connection with its egg, it fuses its spiracles with the tracheal system of the scale and "steals" air until it pupates.

Other parasitoids do more than steal their hosts' air. They turn their hosts into their personal bodyguards.

Among the leafy undergrowth of Brazil, the parasitoid Glyptapanteles seeks out caterpillars of the moth Thyrinteina leucoceraea and deposits up to 80 eggs. The host caterpillar continues feeding even after the larvae hatch from the eggs.

It swings its head violently from side to side to keep predators at bay

The parasitoids feed on the caterpillar's insides until they are ready to pupate. Then, almost all of them eat their way out of the still-living caterpillar, and spin a cocoon on a nearby twig or leaf.

However, a few of them stay inside the caterpillar. Their job is to control the caterpillar and make it guard their pupating brothers and sisters.

The beleaguered caterpillar stops eating. It uses its body, which by this point is riddled with holes, as a tent to protect the pupae. It also swings its head violently from side to side to keep predators at bay. Once the wasps emerge, the caterpillar dies.

After pupation, the adult has to emerge from its host's body. This is the particularly gruesome bit, and resembles nothing so much as the famous chest-burster scene from Alien.

The wasp comes forth very largely covered with body fluids and fragments of tissue of the host

Writing in 1932, Curtis Clausen explained that the adult wasp "must first effect a break in the puparium which surrounds it and then scrape or bite away a varying amount of host viscera or tissue, and finally cut a hole in the heavily chitinised integument…"

All this biting and cutting creates an almighty mess, and "the wasp comes forth very largely covered with body fluids and fragments of tissue of the host." While the wasp "is readily and quickly cleaned", the luckless host "dies immediately as a result of this gross mutilation".

Once this is over with, the adult wasps have one task left in order to complete the cycle. They must mate.

The males do nothing towards the care of the eggs, so their only task is to fertilise females.

In some parasitoids, males fly around searching for chemical signals secreted by receptive females. But sometimes the roles are reversed. In some species of the genus Melittobia, which infects the larvae of solitary bees and wasps, males produce odours that attract females in droves.

Melittobia lays eggs in its hosts just before they pupate. The female stings the host into submission, then lays a cluster of eggs on the outer surface. This simple act launches a bizarre sexual drama.

The larvae feed through the host's skin, pupate and become wasps. Almost all of them are female. If the host is large enough, the eggs develop rapidly into short-winged females. These lay even more eggs, draining the host completely.

Brothers fight one another for access to their emerging sisters

Eggs laid later develop into long-winged females, which chew through the host cocoon and fly out to find more victims.

Meanwhile, within the cocoon the few blind males begin courting the females. They raise and lower their legs, stroke the females with their legs and antennae, and flutter their wings, according to a 2008 study by Robert Matthews of the University of Georgia in Athens.

Competition among the males is fierce. "Brothers fight one another for access to their emerging sisters," according to Matthews. Sometimes, all the males wind up dead.

The females are then left without mates. So they make some more.

A female wasp finds a new host and lays a few eggs, usually less than ten. All these eggs develop into males.

She stays around, stroking her sons with her antennae, watching them grow into pupae and then emerge as adults. While most female parasitoids abandon their offspring as soon as the legs are laid, Melittobia females can be positively maternal.

When the first adult male emerges, the female mates with him. Having been fertilised by her own son, she lays a full clutch of eggs on the same host.

If that strikes you as a rather peculiar way to reproduce, brace yourself, because Copidosoma floridanum has gone one better. "These wasps invented genetic cloning long before humans even had an inkling of the idea," says John Werren of the University of Rochester in New York.

A female C. floridanum lays a single egg in the egg of a noctuid moth. The host egg hatches and the host larva grows, evidently unaware of the 1500-2000 parasitoid larvae inside it. Once the host reaches its final larval stage, the parasitoid larvae start eating it and develop rapidly.

By killing their half-brothers, they make food for their identical sisters

"As the embryos begin to develop, they divide, and divide, each egg making identical genetic copies of itself," says Werren. "This is twinning gone berserk."

Among the genetically identical daughters, a subset become "soldiers". They develop faster than their sisters and defend the host. If another wasp approaches, they attack her and eat up any eggs she may manage to deposit.

The soldier females also do something that seems counter-productive: they seek out their brothers and kill them. They do it because they are genetically identical to their sisters, but only share half their DNA with their brothers. By killing their half-brothers, they make food for their identical sisters.

A few males escape the slaughter. They eventually mate with the fertile females that emerge later, and the cycle goes on.

You may feel that parasitoids are irredeemably horrible. But the odd thing is, they are rather useful, largely because each parasitoid is so specific in its choice of host.

Each year, one-fifth of the world's crops are eaten by insect pests: for instance, about 25% of India's rice is lost to insects. Parasitoids can control these pests, without the need for damaging insecticides.

We often don't understand the intricate relationships between parasitoids and their hosts

Back in 1888, the cottony cushion scale insect was decimating the citrus fields of California. So farmers brought in a "now legendary predator", the Australian parasitoid vedalia beetle. It quickly suppressed the cushion scales, which even now are still kept in check.

By the end of the 20th century, the world had seen more than 3600 purposeful introductions of parasitoids against more than 500 pests in almost 200 countries and islands.

But many haven't been as successful as the vedalia beetle. Only 30% of the parasitoids introduced were able to establish themselves successfully, and of these, only 36% have completely controlled their target pests.

This isn't a failure on their part, but rather on ours. In spite of more than a century of work, we often don't understand the intricate relationships between parasitoids and their hosts. The wild diversity of these insects, it seems, is still beyond our grasp.