Extreme IDE lag and task hanging during AI-driven operations

When executing long-running AI tasks (like generating code, refactoring, or handling complex prompts), the IDE becomes extremely sluggish. What is the problem and what is solve ?