You’re correct that spellcasting does not trigger an attack of opportunity. Spells that involve ranged spell attack rolls (such as fire bolt or eldritch blast) get disadvantage if the caster is engaged in melee when casting them, just the same as ranged weapon attacks.
Do immediate actions provoke attacks of opportunity?
Regardless of the action, if you move out of a threatened square, you usually provoke an attack of opportunity. This column indicates whether the action itself, not moving, provokes an attack of opportunity. If you have a base attack bonus of +1 or higher, you can combine one of these actions with a regular move.
Do you get attack of opportunity when you cast a spell?
First of all, if you cast a spell without casting defensively, you get one attack of opportunity from each single enemy that has you in its threat range, unless they’ve already done an AoO in that round. This means several different things.
What happens when you cast a spell while next to a creature?
Casting a spell while next to a creature doesn’t provoke an attack of opportunity. Also, getting hit (let’s say the enemy Readies an action to hit you when you cast) doesn’t somehow fizzle your spells, unless your spell is a concentration spell and you lose concentration by failing your constitution saving throw to maintain it.
When does the attack of opportunity take place?
Second, yes, the attack happens before you complete your action. It means the attack can disrupt your casting by damaging you while you’re casting a spell, just like if the enemy prepared against you casting a spell but is a free action (and thus a gain in the action economy). This does not mean the enemy goes first in the initiative count.
How to avoid AOO when casting a spell?
There’s another way to avoid AoO for spellcasting and it’s called casting defensively. You just declare you’re casting defensively and you don’t provoke AoOs anymore. You need to pass a concentration check to successfully cast the spell. The DC is manageable and your modifiers should be high so this is the safe and intended way to do it.