Auto-translated
Spells have an AI Value parameter. The AI searches for any of the spells that it considers to be the best among those it doesn't yet have in its cities. If it doesn't need resources for other buildings, etc., it will continue researching until it can acquire better spells than the ones it currently has.