Manually emitting messages
While most agent interactions happen automatically through shared state updates as the agent runs, you can also manually send messages from within your agent code to provide immediate feedback to users.
This video shows the result of npx copilotkit@latest init with the
implementation section applied to it!
What is this?#
In LangGraph, messages are only emitted when a node is completed. CopilotKit allows you to manually emit messages in the middle of a node's execution to provide immediate feedback to the user.
When should I use this?#
Manually emitted messages are great for when you don't want to wait for the node to complete and you:
- Have a long running task that you want to provide feedback on
- Want to provide a status update to the user
- Want to provide a warning or error message
Implementation#
Run and connect your agent#
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 Getting Started guide.
If you don't already have an agent, you can use the coagent starter as a starting point as this guide uses it as a starting point.
Install the CopilotKit SDK#
Any LangGraph agent can be used with CopilotKit. However, creating deep agentic experiences with CopilotKit requires our LangGraph SDK.
uv add copilotkitpoetry add copilotkitpip install copilotkit --extra-index-url https://copilotkit.gateway.scarf.sh/simple/conda install copilotkit -c copilotkit-channelnpm npm install @copilotkit/sdk-js
Manually emit a message#
The copilotkit_emit_message method allows you to emit messages early in a node's execution to communicate status updates to the user. This is particularly useful for long running tasks.
from langchain_core.messages import SystemMessage, AIMessage
from langchain_openai import ChatOpenAI
from langchain_core.runnables import RunnableConfig
from copilotkit.langgraph import copilotkit_emit_message
# ...
async def chat_node(state: AgentState, config: RunnableConfig):
model = ChatOpenAI(model="gpt-5.4")
intermediate_message = "Thinking really hard..."
await copilotkit_emit_message(config, intermediate_message)
# simulate a long running task
await asyncio.sleep(2)
response = await model.ainvoke([
SystemMessage(content="You are a helpful assistant."),
*state["messages"]
], config)
return Command(
goto=END,
update={
# Make sure to include the emitted message in the messages history
"messages": [AIMessage(content=intermediate_message), response]
}
)// ...
async function chat_node(state: AgentState, config: RunnableConfig) {
const model = new ChatOpenAI({ model: "gpt-5.4" });
const intermediateMessage = "Thinking really hard...";
await copilotkitEmitMessage(config, intermediateMessage);
// simulate a long-running task
await new Promise(resolve => setTimeout(resolve, 2000));
const response = await model.invoke([
new SystemMessage({content: "You are a helpful assistant."}),
...state.messages
], config);
return {
// Make sure to include the emitted message in the messages history
messages: [new AIMessage(intermediateMessage), response],
};
}Give it a try!#
Now when you talk to your agent you'll notice that it immediately responds with the message "Thinking really hard..." before giving you a response 2 seconds later.
