How to stop IK bone flipping?

The green bone is moving as desired, the tail of the yellow bone is correct, it’s the bits in between that go wrong. How can I constrain it so that there isn’t this sudden change in angle?