State Rendering
Render your agent's state with custom UI components in real-time.
Not available for Langchain DeepAgents yet
shared-state-streaming demo, and Langchain DeepAgents doesn't have it yet. See this page for CopilotKit's Built-in Agent, LangGraph (Python), LangGraph (TypeScript).What is this?
State rendering lets you build UI that reflects your agent's state in real-time. As your agent progresses through nodes and emits state updates, your frontend renders those changes, showing progress, drafts, or intermediate results.
When should I use this?
Use state rendering when you want to:
- Show real-time progress (e.g. "Researching... 2/5 complete")
- Display drafts that update as the agent works
- Build dashboards that reflect agent state
- Render structured output outside of the chat
How it works in code
On the frontend, subscribe to the agent's state. Each time the backend forwards a fresh value, your component re-renders with the latest partial output. useAgent returns an agent ref; read agent.value?.state in script or let templates unwrap the ref. Keep the provider in an ancestor SFC.
<script setup lang="ts">
import { computed } from "vue";
import { useAgent, UseAgentUpdate } from "@copilotkit/vue/v2";
type ResearchState = { status?: string; draft?: string; completed?: number; total?: number };
const { agent } = useAgent({
agentId: "researcher",
updates: [UseAgentUpdate.OnStateChanged, UseAgentUpdate.OnRunStatusChanged],
});
const state = computed(() => (agent.value?.state ?? {}) as ResearchState);
</script>
<template>
<section aria-live="polite">
<p>{{ state.status ?? "Idle" }}</p>
<progress :value="state.completed ?? 0" :max="state.total ?? 1" />
<article v-if="state.draft">{{ state.draft }}</article>
</section>
</template><script setup lang="ts">
import { CopilotChat, CopilotKitProvider } from "@copilotkit/vue/v2";
import ResearchProgress from "./components/ResearchProgress.vue";
</script>
<template>
<CopilotKitProvider runtime-url="/api/copilotkit">
<ResearchProgress />
<CopilotChat agent-id="researcher" />
</CopilotKitProvider>
</template>On the backend, a state-streaming mapping forwards a tool argument into a state key while the model writes it. Some frameworks provide that as middleware. Direct SDK adapters can emit STATE_SNAPSHOT events from their streaming loop. Either way, the UI can watch the answer assemble token-by-token rather than appearing in one burst between checkpoints.
Build an agent that streams a tool argument into state
Deep Agents has no live demo on this page yet, so the demo code on this
page does not show for it. Define the state key, add a tool that saves the
completed value, and add StateStreamingMiddleware with one StateItem
that maps the tool argument to the state key. This example streams a
searches list:
from typing import Any, TypedDict
from copilotkit import (
CopilotKitMiddleware,
CopilotKitState,
StateItem,
StateStreamingMiddleware,
)
from deepagents import create_deep_agent
from langchain_openai import ChatOpenAI
from langchain.agents.middleware import AgentMiddleware
from langchain.messages import ToolMessage
from langchain.tools import ToolRuntime, tool
from langgraph.types import Command
class Search(TypedDict):
query: str
done: bool
class AgentState(CopilotKitState):
searches: list[Search]
class SearchesStateMiddleware(AgentMiddleware[AgentState, Any, Any]):
state_schema = AgentState
@tool
def report_research_progress(
searches: list[Search],
runtime: ToolRuntime[None, AgentState],
) -> Command:
"""Report the current research tasks and completion status."""
return Command(
update={
"searches": searches,
"messages": [
ToolMessage(
content="Research progress saved.",
tool_call_id=runtime.tool_call_id,
)
],
}
)
agent = create_deep_agent(
model=ChatOpenAI(model="gpt-6-luna", reasoning_effort="none"),
tools=[report_research_progress],
middleware=[
SearchesStateMiddleware(),
CopilotKitMiddleware(),
StateStreamingMiddleware(
StateItem(
state_key="searches",
tool="report_research_progress",
tool_argument="searches",
)
),
],
system_prompt=(
"You are a research assistant. Use report_research_progress "
"to show each task and mark it done when complete."
),
)Understand the two update phases
The state-streaming middleware sends the partial searches argument while
the model writes it. The frontend can show each partial value at once.
The tool then returns a Command that saves the completed list. Its
ToolMessage closes the active tool call.
When the run finishes, the agent's final state is the single source of
truth. A streamed value that the tool does not also save in its Command
disappears at the end of the run, with no error.
Each setting must match your code. A mismatch sends updates to the wrong state field, or to none:
state_key(stateKey): the state field.tool: the name of the tool.tool_argument(toolArgument): the argument of the tool.
Render the state in the UI
Use useAgent to read the state anywhere in your app: in the chat, in a
dashboard, in a sidebar.
import { useAgent } from "@copilotkit/react-core/v2";
function YourMainContent() {
const { agent } = useAgent({
agentId: "sample_agent",
});
const state = (agent.state ?? {}) as {
searches?: { query: string; done: boolean }[];
};
const searches = state.searches ?? [];
return (
<div>
{searches.map((search, index) => (
<div key={index}>
{search.done ? "✅" : "⏳"} {search.query}
</div>
))}
</div>
);
}Ask the agent to research a topic. The search items appear and update while the agent works.
Ask the researcher agent to write a report. The status line and the progress bar change while the agent runs, and the draft text grows as the tokens arrive.
Treat agent state as untrusted external data. Validate it before using it for privileged actions, and avoid putting secrets in state that is streamed to the browser.
How is this guide?