The 16-year-old went to Michael Harrington's Grangetown home, in Cardiff, after suspecting him of sexually assaulting a schoolgirl, and fatally stabbed the 67-year-old.
He was convicted of manslaughter after being cleared of murder at Cardiff Crown Court.
Mrs Justice Nicola Davies said the case was a "tragedy".
There was never a formal complaint made against Mr Harrington, the court heard.
Sentencing the boy on Friday, the judge told him: "This was a tragedy for you and a tragedy for your family and is something you will live with for the rest of your life.
"You are of positive good character and you genuinely believed that this man had sexually abused (the schoolgirl).
"But you took the law into your own hands and no one has the authority to do that.
"The offence is so serious that only a custodial sentence is justified."
The teenager, who cannot be named, went to Mr Harrington's home on 3 August, stabbing him in his neck and ear with a steak knife.
Mr Harrington died four days later.
The boy claimed he never meant to kill the man, but stabbed him during a struggle with the knife.
Despite his denials, he was found guilty of manslaughter by a majority verdict of 11-1 by the jury.

Guidolin is under pressure with the Swans only one point above the relegation zone.
Swansea's American owners, Steve Kaplan and Jason Levien, have yet to decide on Guidolin's future.
However, they are compiling a shortlist of potential successors to the Italian.
The owners will be in attendance at the Liberty on Saturday.
Chairman Huw Jenkins has met former United States coach Bob Bradley, while former Wales and Manchester United captain Ryan Giggs would be interested in the manager's role if it was to become vacant.
Bradley and Giggs are not the only options, with Swansea's hierarchy thought to be considering several other possible candidates in the event of Guidolin's departure.
Any decision on Guidolin's future will be taken jointly between Kaplan, Levien and Jenkins.
They have become concerned in recent weeks, with Swansea failing to win in the Premier League since their opening-day triumph at Burnley.
It is understood the nature of the Swans' performances in their next few games - a trip to Arsenal follows the Liverpool fixture - will have as much of a bearing on Guidolin's future as the results.
The 60-year-old succeeded Garry Monk as Swansea boss in January and, after guiding the Welsh club to Premier League safety last season, the former Udinese manager was rewarded with a two-year contract in May.
Guidolin insists he still has the support of the players and he has not sought assurances from Jenkins about his future.
"I spoke with the chairman last week," he said.
"But at the moment the most important thing is for me to work with my players and with my team to prepare for the next game, to be focussed and with concentration."
Subscribe to the BBC Sport newsletter to get our pick of news, features and video sent to your inbox.
A schoolboy who killed a pensioner for allegedly abusing a girl has been given a three-year custodial sentence.
[NEXT_CONCEPT]
Francesco Guidolin's future as Swansea City head coach will not necessarily rest on Saturday's Premier League match against Liverpool - even though the Italian admitted it could be his last.