> For the complete documentation index, see [llms.txt](https://bootcamp.openguild.wtf/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://bootcamp.openguild.wtf/building-a-blockchain-with-polkadot-sdk/polkadot-sdk/substrate/adding-a-custom-logic-to-runtime/hooks.md).

# Hooks

{% embed url="<https://paritytech.github.io/polkadot-sdk/master/frame_support/traits/trait.Hooks.html>" %}
Documentation about Pallet hooks
{% endembed %}

<figure><img src="/files/O5vqqDSYpnMmkj59jjUH" alt=""><figcaption></figcaption></figure>

* [**OnInitialize**](https://paritytech.github.io/polkadot-sdk/master/frame_support/traits/trait.Hooks.html#method.on_initialize) hooks are only executed when a code change is detected.
* [`OnRuntimeUpgrade`](hooks::OnRuntimeUpgrade) hooks are mandatorily executed at the very beginning of the block body, before any extrinsics are processed.
* [`Inherents`](https://paritytech.github.io/polkadot-sdk/master/sp_inherents/index.html) are always executed before any other other signed or unsigned extrinsics.
* [`OnIdle`](hooks::OnIdle) hooks are executed after extrinsics if there is weight remaining in the block.
* [`OnFinalize`](hooks::OnFinalize) hooks are mandatorily executed after [`OnIdle`](hooks::OnIdle).
