The second-tier visitors took the lead with a penalty before the break then held on for dear life after Kevin De Bruyne leveled for the hosts.
But a last-gasp goal from Sergio Aguero gave City a 2-1 advantage heading into the return game in two weeks' time.
The late strike was a gut-wrenching blow for visiting manager Lee Johnson, whose team had already dumped four top-flight sides out of the competition, but Guardiola had words of consolation at full-time.
"Pep Guardiola said to me that we played better than most of the Premier League teams who come to Man City," Johnson said.
"We'll take an awful lot from it and learn a lot. It's not over ... The lads have to go home and think we can kick on from this performance."
Manchester City, who are unbeaten and 15 points clear in the Premier League, fell behind when defender John Stones dived into a rash challenge on Bobby Reid, who converted the penalty.
The lead lasted until the 55th minute, and after De Bruyne's leveler it was one-way traffic. Bristol were made to defend bravely in the face of a City onslaught.
"We try until the end," Guardiola said. "We win many games in the last minutes because we don't give up. A semi-final is always complicated.
"I said congratulations to Lee Johnson on full time. They are a fantastic team for many reasons. They can play, they are fast, they know exactly what to do. It will be tough in Bristol."
In a Spanish cup competition, Atletico Madrid booked a place in the last eight of the King's Cup after seeing off third-tier Lleida 3-0 for a 7-0 aggregate win.
And a hat-trick from new signing Luciano Vietto fired Valencia past Las Palmas 4-0 after the sides drew 1-1 in the first leg.
Elsewhere, Thomas Lemar scored as Monaco beat Cote d'Azur rivals Nice 2-1 at the Allianz Riviera to reach the French League Cup semi-finals.
Lemar, who has been linked with a move to England, nodded Monaco into an early lead, but Alassane Plea equalized after a video review. Monaco moved back in front before the break through 21-year-old Adama Diakhaby.