A Sydney man who stabbed his son to death was in the grips of a psychotic episode and thought the five-year-old was the devil, a court has heard.

Key points: The judge said the man's mental illness meant he didn't know what he was doing was wrong

The judge said the man's mental illness meant he didn't know what he was doing was wrong The court heard he was prescribed anti-psychotic medication in April 2018

The court heard he was prescribed anti-psychotic medication in April 2018 He will remain in custody indefinitely for treatment

The 38-year-old, who cannot be named for legal reasons, was today found not guilty on the basis of mental illness after being diagnosed with schizophrenia in 2003.

He told police he thought he could "save the world" by ending the boy's life and feared the child was "trying to tear my soul apart".

Family members shrieked and cried from the public gallery of the New South Wales Supreme Court at Darlinghurst when Acting Justice Peter Hidden revealed the number of times the boy was stabbed.

Distraught, the man stood in the dock and faced them as his sister repeatedly reassured him "it's not your fault".

The boy's grandmother had expressed concern for his safety. ( Facebook )

Acting Justice Hidden, who then agreed not to read out the graphic details of the death, said there was no question the man killed the child or that mental illness played a role.

The court heard from mid-April 2018 the man expressed to mental health staff a pressure to "save the world" and was prescribed anti-psychotic medication.

Two days before the death, the man's partner and mother sought his voluntary admission to a Hornsby mental health facility but were told there were no beds available and he could wait in emergency.

The grandmother told mental health workers she worried she would wake up at night and the child may be dead because her son had "snapped".

The next day, the man attended the centre and declared his son was safe.

'I just murdered my son'

On June 8, when the grandmother and partner were out of the house, the father murdered the boy with a brown knife.

The grandmother came home to find him lying face down and tried to take him to safety in a car, but he died in The Children's Hospital at Westmead.

The court heard the man told police: "I just murdered my son. I feel sick."

Acting Justice Hidden said two "persuasive" reports from psychiatrists found the motivation for the killing was "entirely psychotic".

He said it was more likely than not that the man was suffering from a mental illness such that he did not know what he was doing was wrong.

The man will remain in custody indefinitely for treatment.

In a statement, the man's family said they were relieved about the finding after a "tragic and difficult" period.

"There are still other facts and events that show systemic failure within the mental health system," the statement said.

"These still need to be examined and addressed and we hope this will occur as quickly as possible, so that another family is not put through something so horrific and tragic again."