Scout more often and keep an eye out for "back door" attacks?
Maybe you went after the wrong opponent?
If you're playing Entrenchment or Diplomacy, perhaps strong starbases supported by a small fleet would help?
The other thing I wonder, is, whether you're playing Unlocked Teams or with Locked Teams. With Unlocked Teams the AI will eventually ally with one another and gang up on you, often making it impossible tow in. With Locked teams, the AIs will always be hostile to one another. (Maybe it works a little differently in the Diplomacy expansion.)
If you ever venture online and play PvP against human opponents, most games are played with locked teams and a 1-to-1 ally-to-enemy ratio, so it's harder to get doubled than if you're fighting 9 AIs alone. (It can still happen, and often does happen depending on starting positions, but not as much as if you're alone fighting 9 AIs.)
<Edit> Since you're dealing with AI, it might be possible to distract the second AI with a small marauding fleet. Scout the enemy on your back side and if you see it gearing up to attack you, have the marauding fleet start attacking its planets and maybe you can pull its attack fleet back and trick it into chasing your small fleet around.