The deal is for $130 million and features an opt-out for the pitcher after the second year, sources said.
The Giants, who previously signed free agent Jeff Samardzija to a five-year, $90 million contract in early December, have further strengthened a rotation that already included Madison Bumgarner, Matt Cain and Jake Peavy. Cueto was the top starter left on the free-agent market this winter.
"We're going to very much need Cain and Peavy at their best," Giants general manager Bobby Evans said. "Hopefully with these two significant additions it gives us a balance of five guys that can give us the starts and innings we need to protect our bullpen and give us the best chance within our division to compete for the division. That's the goal."
Cueto, 29, is 96-70 with a 3.30 ERA and one All-Star appearance over parts of eight seasons with the Cincinnati Reds and Kansas City Royals. The Royals acquired Cueto from the Reds in a July non-waiver deadline trade, and he recovered from a disappointing regular season in Kansas City (4-7 with a 4.76 ERA in 13 starts) to make a significant contribution to the Royals' World Series run.
"Johnny has always been, and rightly so, very high on our list ... high on our short list," Evans said. "He's somebody we've admired for a long time."
Since 2010, Cueto's aggregate 2.87 ERA ranks third among MLB starters (minimum 900 innings pitched) behind Clayton Kershaw and Adam Wainwright. His 455 2/3 innings over the past two seasons is third most in the majors behind David Price and Corey Kluber.
Over the past five seasons, Cueto ranks in the top five among all qualified pitchers in ERA, opponent OPS and WHIP.
"He's not a guy that you relish facing," Evans said. "You have a guy who has a chance to be an elite presence in your rotation if he's healthy and we can play good defense behind him and use our ballpark to our advantage. Johnny has upside beyond his 2015 final numbers."
Cueto turned down a reported six-year, $120-126 million offer in November from the Arizona Diamondbacks, who quickly shifted course and signed marquee right-hander Zack Greinke to a six-year, $206.5 million contract. The Los Angeles Dodgers and St. Louis Cardinals were among the clubs that expressed interest in Cueto, but the momentum gradually appeared to shift toward San Francisco in recent days.
Big league clubs have gone on a spending rampage on free-agent pitchers this offseason. With Price (Boston), Greinke, Cueto and Jordan Zimmermann (Detroit) leading the way, teams have already invested about $890 million in starters this winter.
The Giants probably aren't done yet. Although they've prioritized pitching this offseason, they have also been in the market for an outfield bat. But the $220 million already spent on Cueto and Samardzija could make it a challenge for the Giants to afford a top tier outfielder in the Yoenis Cespedes-Justin Upton-Alex Gordon group, and they might have to pursue someone from the more budget-friendly Dexter Fowler-Gerardo Parra-Denard Span-Austin Jackson tier of free agents.