Planetary defenses, while not quite effective at stopping the AI, do wonders to halt most pirate raids. Plop down two repair stations, two or three fighter bays, and a bunch of stationary guns and you should have yourself a nice little pirate roadblock. Of course, any ships in the vicinity will help and get xp along the way.