The immune system, like the human brain
, like the entire human body
, is built up from systems evolved
over the length of time life has existed.
The part of the immune system that causes allergies has to do with histamine. Anyone who has allergies is familiar with the effects of this substance.
As a system, the histamine system pre-dates the other aspects of the immune system--it is very primitive. Yet it did not evolve without a purpose.
It is believed that the histamine response arose in order to fight large parasites, common many, many, millenia ago, that were a threat to the existence of the organisims that eventually became us.