A butterfly or moth larvae, also known as a caterpillar, goes through five stages of growth. Each time the caterpillar grows, it molts and sheds its skin. After the fifth growth stage, the caterpillar is ready to pupate, its next stage of growth. While butterfly caterpillars molt one last time, forming the skin of its chrysalis under its current skin, moth caterpillars spin silk from an gland in their mouths to build cocoons where they pupate. Moth caterpillars develop into moths during their time in their cocoons.
Moth caterpillars species that spin cocoons attach themselves to a leaf or stem prior to the formation of their covering. Some moth caterpillars spin their cocoons with bits of leaves and other debris, while others form their entire outer coating from silk. Their silk is somewhat different from spider silk in that it shrinks as it dries, pulling the cocoon tightly around their bodies. While inside its cocoon, the caterpillar molts for the last time, emerging in its moth form.
The cocoon is a covering, like a sleeping bag for the caterpillar as it goes through its pupa stage. Pupating is the third stage of life for a moth, following the egg and larvae stages. The moth emerges from its silk cocoon in the fourth and final stage as an adult. The cocoon is about the same size as the caterpillar that made it, since the silk dries tightly around the larvae.
A caterpillar's main form of protection, prior to building its cocoon is spines along its back. These spines make it hard to eat. Some types of caterpillars taste bitter to predators, due to their diet. These are brightly colored, announcing to birds that might want to eat them that they don't taste good. In their early stages, caterpillars look like bird droppings on leaves, which is another form of protection. While some caterpillars spin cocoons, others pupate underground in a layer of dirt or leaf mulch.