Do I need a medium or large cage rear derailleur?
You have it right. Between medium and long cage, medium is the one to get here.

As you say, you only need a capacity of 23, which is short cage. SRAM is still listing a short cage X7 on their site, but I couldn't find anyone selling or distributing it. If you can track one down and if you're staying with this gearing, that would be more ideal than medium cage. But the medium X5 is fine too. Sadly the short cage 10-speed GX with a clutch is not compatible.