Showing status while a method runs
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?
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
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 guide.
If you do not have an agent yet, the quickstart shows how to create one.
Install the packages
pip install ag-ui-crewai litellmEmit 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.
import asyncio
from crewai.flow.flow import Flow, start
from litellm import acompletion
from ag_ui_crewai import CopilotKitState, copilotkit_emit_state, copilotkit_stream
class AgentState(CopilotKitState):
status: str = ""
class StatusFlow(Flow[AgentState]):
@start()
async def chat(self) -> None:
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 = ""Show the status in your UI
Read status from the agent state with useAgent. Use the agent ID that you registered in your runtime.
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>;
}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.
How is this guide?