The Ravens and head coach John Harbaugh have an agreement in principle on a contract extension with "some lawyer stuff left to do," a source told ESPN's Chris Mortensen.

Harbaugh's extension will keep the most successful coach in franchise history in place for the foreseeable future. This agreement ends speculation of the possible trading of Harbaugh and keeps him from being a coaching free agent at the end of the 2019 season.

Harbaugh's back was against the wall in the middle of November, when the Ravens reached the bye with a 4-5 record and on a three-game losing streak. Along with Lamar Jackson taking over for injured starting quarterback Joe Flacco, Harbaugh inspired one of the bigger turnarounds in the NFL this season, guiding the Ravens (10-6) to the AFC North title and their first playoff berth since 2014.

Harbaugh, 56, is the NFL's fifth-winningest active coach with a 114-77 record (.597), taking Baltimore to the postseason in seven of his 11 seasons and leading the Ravens to a Super Bowl title in 2012. His 10 playoff wins since 2008 rank only behind Bill Belichick.

This deal doesn't come as a surprise because the Ravens announced on Dec. 21 that Harbaugh would return in 2019 and the sides would work on an extension. After the 23-17 wild-card playoff loss to the Chargers on Jan. 6, Harbaugh said he didn't feel there was any uncertainty about his future.

"I have every expectation, every plan, to be here as long as they want me here, and I believe I'll be here," Harbaugh said. "I think that's been made clear by them, to me, over the last few weeks. Like I said a couple weeks ago -- or last week -- I love everybody in the organization; they're great people. I expect to go forward with that as long as that's what they want to do. I do believe that's what they want to do. Let's roll."

Harbaugh has received criticism for his team's run of mediocrity in recent years. Since winning the Super Bowl, Harbaugh had gone 40-40 and made the postseason only once from 2013 to 2017.

What often went overlooked was how the Ravens ranked near the top of the league in each of those seasons with players on injured reserve and how the talent on the roster became depleted. In those five years, Baltimore only drafted two Pro Bowl players (linebacker C.J. Mosley and fullback Kyle Juszczyk).

Still, the Ravens have never unraveled completely. Baltimore has only one losing season under Harbaugh (5-11 in 2015).

Well-respected around the league, Harbaugh would've likely been the top candidate for many of the head-coaching vacancies this offseason if he was available. The Ravens are one of six teams to reach the postseason at least seven times in the past 11 years (New England, Green Bay, Indianapolis, Pittsburgh and Seattle are the others).

Another strong factor in Harbaugh's return was the fact he never lost the respect of the locker room.

"This team believes in him," safety Eric Weddle said in November. "We have his back."

The Ravens remain one of the most stable franchises in the NFL. Harbaugh is only the third coach in the team's 23-year history.

Among active NFL coaches, Harbaugh is the fourth-longest tenured with the same team. Only Belichick, Sean Payton and Mike Tomlin have been with their teams longer than Harbaugh.