Professor Frederick Marius, who invented the first K9 in the year 5000 while working on the asteroid K4067, described him as "my best friend and constant companion." Marius had a dog on Earth, but weight requirements did not allow him to bring his real dog into space, so he built K9. (TV: The Invisible Enemy) He built K9 by using the latest technology. Marius used his own medical computer, a state-of-the-art intraresponsive brain app, protective anti-radiation cladding, probes, a laser scapel, a vision and voice box and two scanning antennas. He put these parts together and created K9. (PROSE: One Man and His Dog) According to one account, the Professor copied the remnants of one of the versions of K9 created by the Doctor and given to Sarah Jane Smith. (PROSE: Tautology)
K9 was not originally intended to be a companion, but producer Graham Williams liked the concept so much that the decision was made to retain him as a regular character, in order to appeal to the younger members of the audience. The original name for the character was "FIDO" — apparently from "Phenomenal [sic] Indication Data Observation" unit — but it was eventually named K9.
Damaged by the time winds, K9 Mark II could not cross back into N-Space with the Doctor. He decided to stay behind at the Gateway with Romana II to help her free the remaining Tharils from slavery. (TV: Warriors' Gate) Eventually, Romana II and the presumably repaired K9 returned to their own universe and K9 Mark II came to live on Gallifrey. (PROSE: Lungbarrow, WC: Shada, AUDIO: Zagreus)
After Mark I sacrificed himself to destroy the Jixen warriors, Starkey retrieved his regeneration unit, which quickly activated. In a blast of white light, K9 was thus reborn as a much more advanced model, though this K9 Mark Z could not access the memories of his previous self, which had been "scrambled". He stayed at the house of Alistair Gryffen, who ran tests on K9, but did not tell the Department. K9 tried to kill Starkey, as he was sprayed with Jixen genetic matter and so registered as a Jixen. Starkey convinced him that he was not an enemy and K9 gave him his dog whistle to summon him when needed. (TV: Regeneration) K9 and the team tracked down the surviving Jixen to Dauntless Prison and killed it. They also faced a Meron and freed the alien prisoners. (TV: Liberation) K9 travelled with Starkey when he was on the run from the Department and rescued Gryffen (who was kidnapped by a Korven). Gryffen later welcomed him and Starkey into his home. (TV: The Korven)
K9 Mark III first appeared in "A Girl's Best Friend", the 1981 pilot for a series, K-9 and Company, which never materialized beyond the first episode. In this, the character is presented to the Doctor's former companion Sarah Jane Smith (Elisabeth Sladen); together, the two embark on what a number of adventures. Only the first of these is depicted on-screen; several are shown in comic book and audio play adaptations, and others are alluded to by the television series. Mark III made a brief appearance alongside Sarah Jane in the Doctor Who 20th anniversary ninety-minute special The Five Doctors (1983), before appearing properly and for the final time in the revived series' episode "School Reunion" in 2006. By the time of that story, Mark III has fallen into disrepair and eventually sacrifices himself to stop a plot by the alien Krillitane and defeat their leader, Lucas Finch (Anthony Head). In the episode's conclusion, the Doctor presents Sarah Jane with a new K9 to encourage her to continue investigating alien activity; the Doctor "rebuilt" him after the Mark III's sacrifice, implying that he had the same mind and memories as his predecessor while still being a "brand new model".