# Exiting the agent loop

After your agent has finished a workflow, you can signal the end of that workflow by calling the `copilotkit_exit()` method in your Python code.

The exit call sends an AG-UI custom event named `Exit` to the frontend. CopilotKit does not act on this event. To react to it in your app, subscribe to the agent with an `onCustomEvent` handler and check for the name `Exit`.

In this example, the `send_email` method adds an assistant message, then exits:

<Steps>
    <Step>
        ### Install the CopilotKit SDK [#install-the-copilotkit-sdk]
        ```bash
        pip install ag-ui-crewai
        ```
    </Step>

    <Step>
        ### Exit the agent loop [#exit-the-agent-loop]
        Call `copilotkit_exit()` at the end of the method. It sends the `Exit` event to the frontend.

        <Tabs groupId="language_crewai-flows_agent" items={['Python']} default="Python" persist>
            <Tab value="Python">
                ```python
                from litellm import completion
                from crewai.flow.flow import start
                from ag_ui_crewai import copilotkit_exit
                # ...
                @start()
                async def send_email(self):
                    """Send an email."""


                    # get the last message and cast to ToolMessage
                    last_message = self.state.messages[-1]
                    if last_message["content"] == "CANCEL":
                        text_message = "❌ Cancelled sending email."
                    else:
                        text_message = "✅ Sent email."
                    self.state.messages.append({"role": "assistant", "content": text_message, "id": str(uuid.uuid4())})
                    # Exit the agent loop after processing
                    await copilotkit_exit() # [!code highlight]
                ```
            </Tab>
        </Tabs>
    </Step>

</Steps>
