As many as you think is necessary.
You just have to follows the same limitations as PCs for spell slots or spells known, the NPC's spellbook may contain any amount of spells you want.
The guidelines to Creating NPCs suggests this:
When it comes to spells, determine how many spell selections you need to make for each level. Choose a variety of spells for the highest two levels of spells possessed by the NPC. For all other levels, stick to a few basic spells, prepared multiple times (if possible).
If this NPC is slated to appear in only one encounter (such as a combat), leaving off lower-level spells entirely is an acceptable way to speed up generation, especially if the NPC is unlikely to cast those spells. You can always choose a few during play if they are needed.
So, the NPC surely has all spell levels and spell slots of a normal PC of that class, but since neither a PC nor an NPC of that class will certainly spend all his spells in a single combat, you can pick some spells ahead of time, write those down and leave some slots open, which you can use for other spells as they come to mind and does make sense that the NPC would have those prepared/known.
Just dont assign spells based on the situation to counter your PCs, like mysteriously have the exact same spells for counterspelling, or the challenge rating of your NPC will suddenly skyrocket.
If i might give you a suggestion, plan ahead based on wether or not this final boss knows the PCs and their abilities, if he has scrying at his disposal and if he was warned or not of their presence before the battle starts.
This will help you to pick the first 5 or so combat rounds and leave his strategy planned out, his buffs already cast, specially those with hour/level durations.