Sanity can be a difficult character's trait to figure out but even harder to determine his sanity. Throughout the story Hamlet presented himself as insane yet he was sane the whole time considering the fact of what happened after Hamlet's first encounter with the ghost, plotting the mousetrap play on Claudius, and knowing what Claudius' cunning plan was.
The first point to agreeing on Hamlet's sanity was what happened after he encountered the ghost. In Act 1, scene 5 Horatio and Marcellus are with Hamlet while he is talking to the ghost which is confirmed to be Hamlet's dad considering he was "wearing his exact armor" and he "frowned just like the king did". The ghost tells Hamlet how his father died by saying in Act 1, Scene 5, and Line 39 "The serpent that did sting thy father's life, Now wears the crown" but what happens after that is how he proves that he is actually sane and he is just pretending he is insane. After Hamlet, Horatio and Marcellus see the ghost; Hamlet tells Horatio that he is going to start acting strange. Giving Horatio a heads up that if he notices Hamlet is acting insane at times it is just because he is putting on an act. He says it in act 1 scene 5 line 171 "How strange or odd soe'er I bear myselfThat you, at such times seeing me, never shall- With arms encumbered thus, or this head shake such ambiguously giving out- to note, that you know aught of me." This means that from this point on Hamlet might act weird but to ignore his acts of insanity because they are just acts. By saying this Hamlet is insuring to Horatio that he will always be completely sane in the future. Hamlet is letting Horatio know that for when everyone thinks Hamlet is insane Hamlet will know Horatio knows all of what Hamlet does is just an act for the appropriate scene. Which only a sane person could create a plan like this where he is only acting insane to convince others he is yet he actually has complete control of his mind.