When a manager has to take the ball from his starting pitcher with one out in the fourth inning, that can be a problem. But it was a part of Joe Girardi’s plan Saturday.

Girardi removed starter Caleb Smith with the Yankees trailing by a run after the lefthander issued his third walk. Then five relievers allowed only a pair of solo homers and struck out six in the final 5 2⁄3 innings as the Yankees earned a 5-4 walk-off victory over the Rays at Yankee Stadium.

“I knew I had a loaded bullpen is what I did,” Girardi said, “so I took advantage of it.”

Perhaps nobody appreciated that more than Smith, who understood the less-than-ideal situation faced by the bullpen.

“It’s huge,” said Smith, who allowed two runs and struck out four. “Everybody out of the pen is just really good. They’re lights-out. I just wish I could have helped them out a little bit today. Only going 3 1⁄3 is a pretty big workload for the bullpen.”

After Smith, the Yankees used Adam Warren, Dellin Betances, Tommy Kahnle, David Robertson and Aroldis Chapman, who picked up the victory when Brett Gardner singled home the winning run with none out in the bottom of the ninth.

Betances, who usually pitches the eighth, faced the fourth through seventh batters in the Rays’ lineup and threw a scoreless sixth.

advertisement | advertise on newsday

“I told Joe if he needs to use me when the middle is coming [up], whenever he wants, I’m fine with that,” said Betances, who walked Lucas Duda before getting the next three batters. “I’m just trying to do the best I can to help the team win and get myself right at the same time.”

It’s a sentiment echoed throughout the bullpen.

“We’re all ready to go whenever,” said Kahnle, who pitched a scoreless seventh and lowered his ERA to 2.18. “They asked us earlier and we told them, ‘Whenever you guys need us, we’re going to be available and we’re going to try to get the job done and hopefully get this team a win.’ ”

Kahnle surrendered back-to-back one-out singles by Mallex Smith and Logan Morrison to put runners on first and third but struck out Steven Souza Jr. and got Evan Longoria to foul out to catcher Gary Sanchez to keep the Yankees ahead 4-3.

“I knew he had the ability to get out of it and he was facing two really tough hitters,” Girardi said. “But his stuff is outstanding. He’s a strikeout guy.”

Yankees videos

Robertson surrendered a long home run by Lucas Duda — his second in as many days since being acquired by the Mets — that tied the score at 4 in the eighth. But just as a blown save by Robertson allowed Derek Jeter to pick up a walk-off single in the final home game of his career in 2014, this blown save by Robertson set up Gardner’s second walk-off hit in three days.

The recent acquisition of Robertson and Kahnle from the White Sox has solidified a bullpen that has helped the Yankees win eight of their last nine games.

“Once we made the trade,” Betances said, “it just changes everything.”

Said Girardi, “Our bullpen is really, really strong, and it’s what we can do on certain days.”