> ## Documentation Index
> Fetch the complete documentation index at: https://docs.chainlit.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Chat Life Cycle

Whenever a user connects to your Chainlit app, a new chat session is created. A chat session goes through a life cycle of events, which you can respond to by defining hooks.

## On Chat Start

The [on\_chat\_start](/api-reference/lifecycle-hooks/on-chat-start) decorator is used to define a hook that is called when a new chat session is created.

```python theme={null}
@cl.on_chat_start
def on_chat_start():
    print("A new chat session has started!")
```

## On Message

The [on\_message](/api-reference/lifecycle-hooks/on-message) decorator is used to define a hook that is called when a new message is received from the user.

```python theme={null}
@cl.on_message
def on_message(msg: cl.Message):
    print("The user sent: ", msg.content)
```

## On Stop

The `on_stop` decorator is used to define a hook that is called when the user clicks the stop button while a task was running.

```python theme={null}
@cl.on_stop
def on_stop():
    print("The user wants to stop the task!")
```

## On Chat End

The [on\_chat\_end](/api-reference/lifecycle-hooks/on-chat-end) decorator is used to define a hook that is called when the chat session ends either because the user disconnected or started a new chat session.

```python theme={null}
@cl.on_chat_end
def on_chat_end():
    print("The user disconnected!")
```

## On Chat Resume

The [on\_chat\_resume](/api-reference/lifecycle-hooks/on-chat-resume) decorator is used to define a hook that is called when a user resumes a chat session that was previously disconnected. This can only happen if [authentication](/authentication) and [data persistence](/data-persistence) are enabled.

```python theme={null}
from chainlit.types import ThreadDict

@cl.on_chat_resume
async def on_chat_resume(thread: ThreadDict):
    print("The user resumed a previous chat session!")
```
