Insanity defense

In criminal trials, the insanity defense is the claim that the defendant is not responsible for his or her actions due to a mental illness. People who have been determined to be insane have been exempt from full criminal punishment since the Code of Hammurabi. There are different definitions of legal insanity in different jurisdictions. A finding of insanity usually results in the defendant being confined in a mental health facility instead of a prison. The first to use this defense was Daniel Sickles when he killed his wife's lover Francis Barton Key (son of Francis Scott Key) in 1859.
Application.
In the United Kingdom, Ireland, and the United States, use of the insanity defense is rare. However, insanity pleas have steadily increased in the UK. Mitigating factors, including things not eligible for the insanity defense like intoxication (or, more frequently, diminished capacity), may lead to reduced charges or reduced sentences. 
The question of who bears the burden of proof is an issue in the United States. Before the John Hinckley, Jr. trial the burden of proof rested with the government in most states. Afterwards many of these states required the defense to prove the defendant was legally insane. Where the state still bears the burden of proof, the standard for the prosecution is beyond a reasonable doubt. Where the defense bears the burden, the standard is preponderance of the evidence (a lower standard).
Expert testimony.
The insanity defense is based on evaluations by forensic mental health professionals with the appropriate test according to the jurisdiction. Their testimony guides the jury (or judge in a bench trial). But they may not testify to the defendant's criminal responsibility. This is for the jury or judge to decide. Mental health experts may testify as to whether at the time of the crime the defendant understood what he did was wrong. If the defendant were acting under a delusion at the time and could still determine right from wrong, he is not insane and may be punished.