Get breaking news alerts and special reports. The news and stories that matter, delivered weekday mornings.

Anthony Weiner was sentenced in federal court on Monday morning to 21 months imprisonment.

He got lucky. He could have been sentenced to a lot more time.

The federal sentencing guidelines are harsh in general. Most criminal defense attorneys would rather have a client in state court than in federal court. The stakes are just too high in federal court, where judges routinely hand down sentences of hundreds of months. It’s mostly because of the federal sentencing guidelines. Though they were designed to achieve reasonable uniformity and proportionality in sentencing, the multiple enhancements for even lower-level crimes often result in stratospheric sentences.

The federal sentencing guidelines require the district court to first calculate the “range” on a published chart, which is a function of two factors: the gravity of the crime, and the criminal history of the defendant.

Let our news meet your inbox. The news and stories that matters, delivered weekday mornings. This site is protected by recaptcha

After calculating the guidelines, the court can consider the aggravating and mitigating circumstances, the nature and circumstances of the offense, and the history and characteristics of the defendant. The guidelines require the court to impose a sentence sufficient, but not greater than necessary, to satisfy the purposes of punishment.

Related: Anthony Weiner, Once Promising Politician, Now Faces Jail Time

The guidelines are advisory only. That means a court has to painstakingly calculate them, but doesn’t have to follow them. Rather, that guidelines range still serves as the starting point and the initial benchmark for all sentencing proceedings. If appealed, a “guidelines sentence” will likely be considered reasonable by an appellate court.

A courtroom sketch shows Anthony Weiner during his sentencing. Christine Cornell

In Weiner’s case, his criminal history was as good as it gets: no prior record, which places him in Category I. For this reason alone, his sentencing range should be lower than most. The problem for Weiner is the second factor: the seriousness of the crime.

The guidelines are particularly harsh on child pornography, providing obscene materials to minors, and related crimes. How harsh? Even as a first-time offender who also took responsibility and pleaded guilty, Weiner’s resulting guidelines range would have been 11 years and three months to 14 years.

The statutory maximum for this crime is 10 years in prison.

That’s right: The recommended guidelines would have exceeded the maximum sentence. A formula designed to calculate a reasonable sentence results … in an impermissible sentence.

Fortunately for Weiner, even the prosecutors agreed that a sentence of 21 to 27 months in prison would be fair and appropriate. It’s rare for U.S. attorneys to be so agreeable; it demonstrates that they felt the guidelines resulted in an excessive sentence.

"This kind of leniency on the part of the U.S. attorneys in these cases is not just rare, it's practically unheard of," said veteran criminal defense attorney David Cattie, who has handled a number of federal child pornography cases.

Twenty-one months was a safe sentence for this judge; it’s virtually immune from attack in the appellate courts, and it is within the adjusted range to which the prosecutors stipulated.

How long will Weiner really serve? Pretty much the whole sentence. The abolition of parole in the federal system makes the sentence imposed by the court the sentence the offender will serve, less approximately 15 percent for good behavior.