# 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).


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://bootcamp.openguild.wtf/building-a-blockchain-with-polkadot-sdk/polkadot-sdk/substrate/adding-a-custom-logic-to-runtime/hooks.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
