I got a decent iv alakazam but if I use it it gets oneshot. Why is this and what can do about it?
Because it is frail and its defensive stats are dumb. HP : 55 Attack : 50 Defense: 45 Sp. Atk: 135 Sp. Def.: 95 Speed: 120 Base: 500
Probably your opponent was using a Physical attack. Alakazam has pretty low Defense and HP.
What Can I Do?
You can use Focus Sash. So It will survive an attack that would OHKO with one HP at full health. Focus Sash+Counter is a common strategy.
You can teach it Calm Mind. It wiil raise it SpA and SpD by one stage.
Some bad suggestion:
You can breed an Defensive stats incresing nature and defensive IVs Abra
You can teach it barrier, confide, curse, reflect, light screen, double team and other defensive stats increasing moves.
You can Mega evolve it. Mega alakazam has a bit more bulk.
EV train it to give it 252 HP / 128 Def / 128 SpD.
Alakazam is supposed to be a Fast Special Sweeper. Not a Bulky Attacker. Increase its SpA and Speed EVs.
alakazam has terrible defence at base 45. it has ok special defence at a nice 95. if you need a psychic wall, use bronzong, cresselia, jirachi or uxie. alternitavley, you can give it barrier but its a bad option. this question explains why: https://pokemondb.net/pokebase/305431/does-showdown-barrier-usually-useless-alakazams-learnset