BOSTON – In late July, the Red Sox and Dodgers came very close to a trade that would have sent Josh Beckett to Los Angeles.

Three-and-a-half weeks later, the clubs made that deal happen by expanding it into about the biggest deal in baseball history. Not just Beckett but Adrian Gonzalez, Carl Crawford and Nick Punto also went to the Dodgers, with the Red Sox receiving more than $270 million in salary relief and five Dodgers players/prospects.

Here’s an account, based on baseball sources familiar with the deal, on how it took shape and finally got done.

The Dodgers had been scouting Beckett intensively in late July. On July 31, however, Beckett had to leave his start with back spasms — an injury that put the kibosh on that deal but set the stage for what was to follow.

In the middle of August, the 30 MLB owners were meeting in Denver when Dodgers president Stan Kasten approached Red Sox principal owner John Henry and began talking about the failed Beckett deal. Kasten expressed his team’s continued interest in Beckett, but that the Red Sox player whom the Dodgers were really interested in was first baseman Adrian Gonzalez.

Gonzalez was not available, Kasten was told.

Kasten pressed on, inquiring if the Red Sox’ stance would change if the Dodgers were to take on the remaining contracts of both John Lackey and Beckett.

The answer was still no at that point. Then Kasten added the name of Carl Crawford, still owed $102.5 million beginning with next year through 2017, to that list.

The Red Sox were caught by surprise, to say the least, to hear that Crawford, who just underwent Tommy John surgery on his elbow, was of interest.

“As soon as Crawford entered the conversation, then there was something to talk about,” said one baseball source.

Talks began.

Before long, Lackey’s name left the list and the clubs began to talk about other names. Punto entered the conversation. The Red Sox, still well aware they had shipped out three prized prospects — Casey Kelly, Anthony Rizzo and Reymond Fuentes — to San Diego in the original Gonzalez trade, were intent on getting quality prospects in return.

They are believed to have started with the Dodgers’ top pitching prospect, Zach Lee, but finally settled on the second best, Allen Webster.

The package — Webster, Ivan De Jesus Jr. and the two players to be named later, Rubby de la Rosa and Jerry Sands — was settled upon but the deal was far from complete.

Both the Red Sox and Dodgers were worried that Gonzalez in particular would not be able to clear waivers, with one insider stating each club felt there was only a 50-50 shot that he would clear, even with the $127 million remaining on his deal.

Gonzalez cleared, however, along with everyone else involved who was on each team’s 40-man roster.

That left a couple more hurdles.

The Red Sox understood that the Dodgers wanted Beckett, but handing over his medical records was not done cavalierly or with a sense that it would be a breeze. Los Angeles did not find enough to be concerned about, however. After Beckett and Crawford, who each had veto power over the deal, signed off on it, and the Dodgers and Red Sox concluded negotiations about how much money would change hands — the Red Sox will make a one-time payment of $12 million to the Dodgers next year while the Dodgers took on more than 95 percent of the remaining money owed Beckett, Crawford, Gonzalez and Punto — the deal drew to a close.

For the Red Sox, the talks escalated once Crawford’s name entered into them, but that also coincided with another stretch of lousy play during the club’s ill-fated trip to Cleveland, Baltimore and New York.

The Red Sox understood that any hope for a playoff run this season was likely over and the situation warranted a bold trade, or else it would be difficult to make this type of wholesale change, both in personnel and the gained financial flexibility, in the offseason.

The Dodgers, whose payroll actually had room to absorb the Red Sox’ excess, got two players, Gonzalez and Beckett, they really wanted — and a splash that their new ownership really thought they needed.

“Well, the Dodgers had an opportunity, they’re in a pennant race and motivated to add talent, so they made a move obviously they felt was in their best interest,” said Red Sox GM Ben Cherington Saturday.

There is no pennant race in sight for the Red Sox. But by doing this deal, but they created financial wiggle room to create a long-term vision that is credible and believable.