Kai Havertzâs early tap-in had Chelsea in control until Dan Gosling headed home with two minutes on the clock before Barkley struck.
City were in danger of blowing it as Matty Cash and former Reds star Philippe Coutinho struck at the Etihad Stadium.
The Hammers conceded three second-half goals as they had to settle for seventh place.