Gray, one of nine new faces in a much-changed Foxes line-up, fluffed the opportunity to open his Premier League goal account for the season from the spot after only 12 minutes.
But the winger made amends after the interval to fire second-placed Leicester to a first league win in four games while inflicting a fourth straight home defeat on woeful West Ham, who have won just twice in 14 matches.
The natives are getting increasingly restless at the London Stadium, and Pellegrini's decision to substitute record signing Sebastien Haller, rather than give him some support in attack, was met with loud jeers.
Pellegrini was, at least, able to welcome back goalkeeper Lukasz Fabianski, whose three-month absence through injury coincided with West Ham's alarming tumble down the table from fifth to the brink of the bottom three.
However, the Poland keeper made an inauspicious start, his first contribution clattering into Kelechi Iheanacho to concede a penalty.
In the absence of Jamie Vardy, who sat out the trip to the capital as his wife Rebekah had given birth to their baby girl, Gray stepped up only to see Fabianski instantly redeem himself, saving the spot-kick low down to his left.
After a VAR check for possible encroachment, the PA announcer confirmed there had been 'no material impact', a curious phrase but one which could have easily applied to the majority of West Ham's players this season.
Yet that appeared to be the spark West Ham needed, with Felipe Anderson suddenly showing urgency to burst down the right.
The £42million Brazilian winger exchanged a one-two with Ryan Fredericks before crossing low for Pablo Fornals to sweep home his first Premier League goal.
But Leicester went ahead again 11 minutes into the second half after Perez's through-ball sliced open the West Ham defence and Gray took a touch before curling the ball inside the near post.
This time West Ham had no answer, and if they put in a display as abject as this at home to Bournemouth on New Year's Day Pellegrini's position could be under serious threat.