From the bedroom of his family's cramped apartment in the suburbs of Baltimore, Khalid, while still in high school, fomented violent jihadists across the globe with online screeds, translated recruitment propaganda for al-Qaeda, and conspired over the Internet with an accused terrorist in Ireland to kill a Swedish artist whose work offended some Muslims, prosecutors said.
But as U.S. District Judge Petrese B. Tucker weighed his fate, she wrestled to square the image of the jihadist firebrand who daydreamed in one online chat about "doing martyrdom operations . . . in my school" with the gawky and gaunt young man before her Thursday.
For his part, Khalid struggled through silent sobs to express his remorse to the judge.
"The upheavals of my life were distorted into a force of hate so strong that it wrapped me in its claws," he said. "All of my life, whatever is left of it, was wasted in a few years."
The shame of the situation, said Khalid's attorney, Jeffrey Lindy, was that one could easily imagine a far different fate for his client had different choices been made.
After emigrating from Pakistan in 2007, his parents moved Khalid and his three siblings into a two-bedroom apartment in Ellicott City, Md. His father scraped together rent money as a pizza deliveryman, while his mother, a homemaker, sold perfume at flea markets on the weekend to help make ends meet - all to further their goal of sending their children to the region's best schools.
And by all appearances, Khalid seemed to be holding up his end of that bargain. He aced his SATs, earned early admittance to Hopkins, and won a scholarship worth $54,000 a year.
But for all his parents' attempts to elevate their family's station, Khalid found himself isolated, friendless, and largely ignored by his peers - a condition made only worse by undiagnosed Asperger's syndrome, Lindy said Thursday.
Khalid found solace chatting with strangers online, often spending more than 40 hours a week alone in his room on the computer. His parents worried that their son was addicted to online pornography and turned to a local Muslim scholar for help, according to court filings.
It was only when the FBI showed up on their doorstep in 2009 that they realized the company their son had been keeping.
As prosecutors described it Thursday, Khalid had earned a reputation among online jihadists for his "eloquent and brilliant" translations of jihadist doctrine. He sought to recruit other westerners to their cause and raise money to support training camps overseas.
After his arrest, Khalid's cooperation with FBI agents led them to previously undiscovered evidence of online jihadist networks and made "a big difference" in at least three other U.S. terrorism cases, said Assistant U.S. Attorney Jennifer Arbittier Williams.
"Mr. Khalid was proud of the fact that he was young and all these adults had confidence in him," she said. "The world of violent jihad relies on finding new recruits, and they need people like Mr. Khalid that make the doctrine accessible, especially in the West."
Eventually, that reputation brought him in contact with Ali Charaf Damache, an Algerian terrorism suspect living in Ireland, and LaRose, a Texas transplant who gained her own online following for the jihadist screeds she posted on YouTube under the screen name "JihadJane."
In 2009, LaRose and a Colorado woman, Jamie Paulin Ramirez, flew to Ireland to join Damache in a plot to kill the Swedish artist Lars Vilks for his depictions of Muhammad as a dog.
Khalid stayed behind, but not before LaRose sent him a passport she had stolen from her then-boyfriend in hope of helping to carry out some future terrorist attack.
LaRose was sentenced to 10 years in prison and Ramirez to eight this year for their roles in the failed assassination attempt. Damache remains in Ireland, fighting extradition to the United States.
But, Williams argued Thursday, Khalid, despite his youth, caused more damage than many of his codefendants.
"Mr. Khalid had more of an impact on global jihad," she said. "His translations are still out there. His efforts to recruit jihadists are still playing out. And that passport is still missing."
Lindy pleaded with the court for leniency, describing his young client's extremism as in the past, and the plot in which he was accused as "half-baked."
"I feel like I'm taking crazy pills - like we're talking about a different case," he said. "Colleen LaRose had as much chance of killing Lars Vilks as I did - which is, no chance."
In the five-year sentence she handed down Thursday, Tucker seemed to split the difference. It was far less than the maximum 15 years Khalid could have faced.
"This was a serious offense," she said. "It requires punishment."
But with the three years he has spent behind bars and time off for good behavior, he could be released within 14 months.
Still, his family's dreams for Khalid are likely dashed forever, Lindy said. Hopkins has withdrawn his four-year scholarship offer. Few other colleges are likely to admit a convicted terrorist.
Because Khalid is not a U.S. citizen, he will almost certainly be deported to Pakistan upon his release from prison, his lawyer said.
Yet despite that grim outlook, Khalid managed - even behind bars - to achieve an American graduation of sorts.
Though he earned his high school diploma before his arrest, he took the GED in prison so he could tutor his fellow inmates for the test.
When the results of the exam were announced, in a small prison ceremony in 2012, Khalid was named valedictorian.