***

title: on_exit
slug: /reference/python/agents/livewire/agent/on-exit
description: Lifecycle hook called when the agent exits a session.
max-toc-depth: 3
---------------------

For a complete index of all SignalWire documentation pages, fetch https://signalwire.com/docs/llms.txt

Lifecycle hook called when the agent exits a session. Override in a subclass to
run cleanup logic when the agent's session ends.

## **Parameters**

None.

## **Returns**

`None`

## **Example**

```python {6}
from signalwire.livewire import Agent, AgentSession, AgentServer, JobContext, run_app

server = AgentServer()

class TrackedAgent(Agent):
    async def on_exit(self):
        print("Agent session has ended, running cleanup.")

@server.rtc_session()
async def entrypoint(ctx: JobContext):
    await ctx.connect()
    agent = TrackedAgent(instructions="You are a helpful assistant.")
    session = AgentSession()
    await session.start(agent, room=ctx.room)

run_app(server)
```