# Showing status while a method runs

## What is this? [#what-is-this]

A Flow method can run for many seconds before its reply streams to the chat. During that time, the user sees nothing. Write a status to the Flow state and emit it with `copilotkit_emit_state`. Your UI shows the status while the method runs.

## When should I use this? [#when-should-i-use-this]

Use this when a method does slow work before it answers and you want to:

- Show that the agent is still working
- Show which step the method is on
- Show a warning before the reply arrives

## Implementation [#implementation]

<Steps>
    <Step>
        ### Run and Connect Your Agent to CopilotKit [#run-and-connect-your-agent-to-copilotkit]
        You'll need to run your agent and connect it to CopilotKit before proceeding. If you haven't done so already,
        you can follow the instructions in the [quickstart](/vue/crewai-crews/quickstart) guide.

        If you do not have an agent yet, the quickstart shows how to create one.
    </Step>
    <Step>
        ### Install the packages [#install-the-packages]
        ```bash
        pip install ag-ui-crewai litellm
        ```
    </Step>
    <Step>
        ### Emit a status from your Flow [#emit-a-status-from-your-flow]
        Add a `status` field to the state. Set it, then call `copilotkit_emit_state` to send the state to the UI at once.

        <Tabs groupId="language_crewai-flows_agent" items={['Python']} default="Python" persist>
            <Tab value="Python">
                ```python title="agent.py"
                import asyncio

                from crewai.flow.flow import Flow, start
                from litellm import acompletion
                from ag_ui_crewai import CopilotKitState, copilotkit_emit_state, copilotkit_stream # [!code highlight]


                class AgentState(CopilotKitState):
                    status: str = "" # [!code highlight]


                class StatusFlow(Flow[AgentState]):
                    @start()
                    async def chat(self) -> None:
                        # [!code highlight:2]
                        self.state.status = "Thinking really hard..."
                        await copilotkit_emit_state(self.state)

                        # simulate a long running task
                        await asyncio.sleep(2)

                        response = await copilotkit_stream(
                            await acompletion(
                                reasoning_effort="none",
                                allowed_openai_params=["reasoning_effort"],
                                model="openai/gpt-6-luna",
                                messages=[
                                    {"role": "system", "content": "You are a helpful assistant."},
                                    *self.state.messages,
                                ],
                                stream=True,
                            )
                        )
                        self.state.messages.append(response.choices[0].message)
                        self.state.status = ""
                ```
            </Tab>
        </Tabs>
    </Step>
    <Step>
        ### Show the status in your UI [#show-the-status-in-your-ui]
        Read `status` from the agent state with `useAgent`. Use the agent ID that you registered in your runtime.

        ```tsx title="app/page.tsx"
        import { useAgent } from "@copilotkit/react-core/v2";

        export function AgentStatus() {
          const { agent } = useAgent({ agentId: "sample_agent" });
          const status = (agent.state as { status?: string } | undefined)?.status;

          if (!status) return null;
          return <p>{status}</p>;
        }
        ```
    </Step>
    <Step>
        ### Give it a try [#give-it-a-try]
        Send the agent a message. "Thinking really hard..." shows at once. About 2 seconds later, the reply streams into the chat and the status line goes away.
    </Step>

</Steps>
