It seems too complicated to assume that Hamlet is sane, pretending to be insane, going insane, and actually insane. If he is insane, he cannot also be insane. And if he is insane, he cannot be sane. The most plausible explanation of everything he says and does throughout the play is that he is sane, that he sometimes pretends to be insane, and that he sometimes seems to act irrationally because he is overwhelmed with emotions, as is the case when he jumps into Ophelia's grave and grapples with Laertes.
In Act 1, Scene 5, after his meeting with the Ghost, Hamlet forces Horatio and Marcellus to swear that they will keep silent about the visitation.
There are more things in heaven and earth, Horatio,
Than are dreamt of in your philosophy. But come;
Here, as before, never, so help you mercy,
How strange or odd soe'er I bear myself,
As I perchance hereafter shall think meet
To put an antic disposition on,
That you, at such times seeing me, never shall,
With arms encumber'd thus, or this headshake,
Or by pronouncing of some doubtful phrase,
As 'Well, well, we know,' or 'We could, an if we would,'
Or 'If we list to speak,' or 'There be, an if they might,'
Or such ambiguous giving out, to note
That you know aught of me: this not to do,
So grace and mercy at your most need help you, Swear.
Hamlet has already decided, for his own protection, to pretend to be mad. Claudius has been trying to pry into his soul since he returned from Wittenberg. Hamlet has been made to feel like a prisoner who is suspected of plotting to overthrow the king. The Prince knows it will be difficult to hide his thoughts and feelings from the cunning King, now that he has learned such shocking information from the Ghost. He already feels like a different person. He has changed considerably as a result of this meeting with his father's ghost.
Much later, in Act 3, Scene 4, he will have a tumultuous meeting with his mother in her chambers. Here he will give her his assurance that he is not mad but only pretending to be mad.
My pulse, as yours, doth temperately keep time,
And makes as healthful music: it is not madness
That I have utter'd: bring me to the test,
And I the matter will re-word; which madness
Would gambol from. Mother, for love of grace,
Lay not that mattering unction to your soul,
That not your trespass, but my madness speaks:
It will but skin and film the ulcerous place,
Whilst rank corruption, mining all within,
Shakespeare seems to be wanting to assure the audience that Hamlet is sane. If Hamlet were a madman he would surely be talking like a lunatic in this scene with his mother. Yet everything he says sounds completely coherent. His rash action in killing Polonius while the old fool is hidiing behind the tapestry is not an act of madness. Hamlet thinks he has walked into a trap. His mother starts screaming for the guards because she thinks he is going to murder her, and Polonius, who can't see anything, starts shouting for the guards in back of Hamlet. If he is taken off to confinement, he will not be able to carry out his mission to kill the King--and in the meantime Claudius will have him at his mercy and might decide to have someone kill him.
If the guards actually did arrive to apprehend him, his own mother might tell them he tried to kill her; and Polonius, if still alive, would corroborate whatever Gertrude told them.
Hamlet cannot be sane, insane, going insane, and pretending to be insane or pretendinig to be going insane. He is sane.
Hamlet is not insane but he takes the problem impatiently aeems to be insane,but he is not. He looks the best actor.