The life histories of many animals are characterized by niche shifts, the timing of which can strongly affect fitness. In the tree frog Agalychnis callidryas, which has arboreal eggs, there is a trade-off between predation risks before and after hatching. When eggs are attacked by snakes, tadpoles escape by hatching rapidly and falling into the water below. Eggs not attacked by snakes hatch later, when newly emerged tadpoles are less vulnerable to aquatic predators. Plasticity in hatching allows embryos to use immediate, local information on risk of mortality to make instantaneous behavioral decisions about hatching and the accompanying shift from arboreal to aquatic habitats.