Answer
It prevents predation, protecting the plant.
Work Step by Step
Since nicotine is poisonous to most animals that might try to eat the tobacco plant, its production is worth the energy investment as it increases the plant's odds of survival.