Prepared and Spontaneous Spells
For a creature that can cast as many spells as a PC spellcaster, the highest spell rank the creature can cast is half its level rounded up. It gets five cantrips. If the creature's level is odd, it gets two spell slots of the highest spell rank (plus three spell slots of each lower rank), or three spell slots of that rank (plus four spell slots of each lower level). If its level is even, it gets three spell slots of the highest spell rank (plus three spell slots of each lower rank), or four spell slots of that rank (plus four spell slots of each lower rank). You can base the number of spells on the class you're trying to emulate or choose more spells if the creature doesn't have many other abilities.
Because creatures tend to be “on stage” for only a short time, you usually don't need to fill every spell slot. You can often fill just the top three ranks of spells, pick cantrips, and slot in a few thematic backup spells in the fourth rank down. For a recurring foe, you might give it a full complement of spells.