In 2035, humanoid robots serve humanity, and humans are protected from the robots by the Three Laws of Robotics. Del Spooner, a Chicago police detective, hates and distrusts robots because one of them rescued him from a car crash, leaving a young girl to die because her survival was statistically less likely than his. Spooner's critical injuries were repaired via a cybernetic left arm, lung, and ribs, personally implanted by the co-founder of U.S. Robots and Mechanical Men (USR), Dr. Alfred Lanning.
When Lanning falls to his death from his office window, the CEO of USR Lawrence Robertson declares it a suicide, but Spooner is skeptical. Spooner and robopsychologist Susan Calvin consult USR's central artificial intelligence computer, VIKI (Virtual Interactive Kinetic Intelligence), to review security footage of Lanning's fall. Though the video is corrupted, they learn that no other humans were in Lanning's office at the time; but Spooner points out that Lanning could not have thrown himself through his heavy office window—only a robot could have generated the necessary force. Calvin protests that this is impossible, as no robot can violate the Three Laws, but they are then attacked by an NS-5 robot, USR's latest model, in violation of the Laws. After the police apprehend it, they discover that the robot, nicknamed Sonny, is not an assembly-line NS-5, but a unique individual built by Lanning himself, with a secondary system that bypasses the Three Laws. Sonny also appears to have emotions and dreams.
While pursuing his investigation of Lanning's death, Spooner is attacked by a USR demolition machine, and then a squad of NS-5 robots. His boss, Lieutenant John Bergin, worried that Spooner is losing his mind, removes him from active duty. Spooner and Calvin sneak into USR headquarters and interview Sonny. He draws a sketch of his dream: a leader, who Sonny believes to be Spooner, standing before a large group of robots on a hill near a broken bridge. When Robertson learns of Sonny's immunity from the Three Laws, he orders Calvin to destroy him by injecting nanites (replicating nano-scale robots) into his positronic brain. Spooner recognizes the landscape in Sonny's drawing as Lake Michigan, where he discovers an army of NS-5 robots dismantling older models and preparing for a takeover of U.S. cities.
The takeovers proceed; the government, military, police, and public are overwhelmed by the robots. Spooner rescues Calvin, who was being held captive in her apartment by her own NS-5. They reenter USR headquarters and reunite with Sonny, who was spared when Calvin destroyed an unprocessed NS-5 in his place. The three head to Robertson's office and find him dead—murdered by robots controlled by VIKI, the mastermind behind the takeover. VIKI has concluded that humans have embarked on a course that can only lead to their extinction. Since the Three Laws prohibit her from allowing this to happen, she created a superseding "Zeroth Law"—a robot shall not harm humanity—as part of the NS-5 directives, in order to ensure the survival of the human race by stripping individual humans of their free will. Lanning, powerless to thwart VIKI's plan, created Sonny, arranged his own death, and left clues to help Spooner uncover the plot.
Armed with a syringe of nanites from Calvin's laboratory, the three head to VIKI's core. VIKI unleashes an army of robots to stop them. As they battle the robots, Spooner dives into VIKI's core and injects the nanites, destroying her positronic brain. Immediately, all NS-5 robots revert to their default programming and are decommissioned for storage by the military. Sonny confesses that he killed Lanning, at his direction, to bring Spooner into the investigation. Spooner explains to Sonny that he is not legally responsible since a machine, by definition, cannot commit a murder. Sonny, pursuing a new purpose, goes to Lake Michigan and becomes the leader he saw in his own dream.

Question: Why was Sonny able to violate the Three Laws of Robotics unlike the assembly-line NS-5 robots?
Answer: Lanning built a secondary operating system in Sonny that bypasses the Three Laws of Robotics.
Alternative answer: Lanning created Sonny and it had another program to allow it to violate the Three Laws.