Leodis McKelvin has new life as a starting-caliber cornerback, and the Buffalo Bills gained some security in the defensive secondary Saturday.

McKelvin agreed to terms on a four-year contract worth just over $20 million, according to league sources. McKelvin was due to become an unrestricted free agent Tuesday.

The 28-year-old McKelvin has had an up-and-down tenure as a coverage man over the last five seasons for the Bills but also been a sensational kick returner. He led the NFL in punt returns last year with an 18.7-yard average. He took two punt returns back for touchdowns, giving him four kick-return TDs for his career.

His new $5 million-a-year contract, which includes $7.5 million in guaranteed money, signals the Bills’ regard for him as a worthy starter in the secondary opposite cornerback Stephon Gilmore.

Bills General Manager Buddy Nix said as much last month.

“Leodis has got to have some help, but he’s a big-time returner,” said Nix. “I think Leodis will be a candidate for that spot.”

McKelvin had a pretty good year as a first-time starter at cornerback in 2010. The next season he had two poor games early in the year (against Oakland and Cincinnati) and the Bills pulled him from the starting lineup after five games.

He entered the 2012 season working mostly as a slot cornerback. Former defensive coordinator Dave Wannstedt thought the inside defensive position would allow McKelvin to be more instinctive without worrying so much about deep sideline balls. However, the Bills’ No. 2 corner, Aaron Williams, struggled early, allowing five TD passes in the Bills’ first five games.

McKelvin took over for Williams midway through the eighth game of the season and then started four more games before going out with a groin injury. McKelvin didn’t allow any significant completions in those starts.

Whether Williams will get a chance to compete at corner is uncertain. Nix raised the prospect of shifting him to safety.

McKelvin was the 11th overall pick in the 2008 draft. He has 33 starts in 64 career games.

McKelvin set a Bills single-season record with 1,468 kickoff-return yards as a rookie, and he ranked third in the league with a 28.3-yard average. He averaged 19.5 yards on eight punt returns in 2011 before taking over full-time duty on punt returns last season.