It is mindlessly simple. There is an AI value tag for each spell. As long as the spell uses the same targets as other spells, the AI can cast them. Increasing the value to very high levels increases the AI's likelihood of choosing a given spell. The hardest spells to get the AI to use are just ones that have not been fully implemented for the AI. Like a strategic damage spell.
I do find it strange that so little of the magic system focuses on the strategic game. I guess it is hard to get the AI to effectively strategize when to cast a fiery pillar or freezing cloud. Hopefully we can eventually get an AI algorithm that casts those spells and then attacks on the same turn like a human.