copilotkit
Build AI copilots, chatbots, and agentic UIs in React and Next.js using CopilotKit. Use this skill when the user wants to add an AI assistant, copilot, chat interface, AI-powered textarea, or agentic UI to their app. Covers setup, hooks (useCopilotAction, useCopilotReadable, useCoAgent, useAgent)...
SKILL.md
Full skill instructions
CopilotKit
Full-stack open-source framework (MIT, v1.51.3, Python SDK v0.1.78) for building agentic applications with AI copilots embedded directly in React and Angular UIs. Angular support via @copilotkitnext/βangular (Angular 18+19). 28k+ GitHub stars.
When to Use This Skill
- User wants to add an AI copilot, assistant, or chatbot to a React/βNext.js app
- User is working with CopilotKit hooks, components, or runtime
- User asks about AI-powered text areas or form completion
- User needs to connect a Python agent (LangGraph/βCrewAI) to a React frontend
- User is implementing human-in-the-loop or generative UI patterns
- User asks about AG-UI protocol or MCP Apps
- User wants to sync state between a UI and an AI agent
Architecture
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β β
β F R O N T E N D @copilotkit/βreact-core + react-ui β
β β
β ββββββββββββββββββββ ββββββββββββββββββββ ββββββββββββββββ β
β β <CopilotPopup> β β <CopilotSidebar> β β <CopilotChat>β β
β β <CopilotTextarea>β β Headless UI β β Custom UI β β
β ββββββββββ¬ββββββββββ ββββββββββ¬ββββββββββ ββββββββ¬ββββββββ β
β ββββββββββββββββ¬βββββββ β β
β βΌ β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β React Hooks β
β β ββ useCopilotAction() β Define callable tools β
β β ββ useCopilotReadable() β Expose app state to LLM β
β β ββ useAgent() β Bidirectional state (v2) β
β β ββ useFrontendTool() β Generative UI rendering β
β β ββ useCopilotChat() β Headless chat control β
β β ββ useLangGraphInterrupt() β Human-in-the-loop β
β βββββββββββββββββββββββββββ¬ββββββββββββββββββββββββββββββββββββ
β β β
βββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββββββ
β
βββββββββββΌββββββββββ
β AG-UI Protocol β
β (HTTP event streamβ
β 17 event types) β
βββββββββββ¬ββββββββββ
β
βββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββββββ
β β β
β B A C K E N D @copilotkit/βruntime β
β βΌ β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β CopilotRuntime β β
β β ββββββββββββββββ βββββββββββββββββ ββββββββββββββββββ β β
β β β LLM Adapters β β Backend β β Thread β β β
β β β OpenAI β β Actions β β Persistence β β β
β β β Anthropic β β (server-side β β InMemory / β β β
β β β Google β β tools) β β SQLite β β β
β β β Groq β β β β β β β
β β ββββββββββββββββ βββββββββββββββββ ββββββββββββββββββ β β
β β ββββββββββββββββββββββββββββββββββββββββββββββββββββ β β
β β β Agent Router β β β
β β β ββ BuiltInAgent (direct LLM + middleware) β β β
β β β ββ BasicAgent (lightweight, no middleware) β β β
β β β ββ CustomHttpAgent (remote Python/βJS agents) βββββΌββββΌββ β
β β ββββββββββββββββββββββββββββββββββββββββββββββββββββ β β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β β
β β β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββΌββ
β
βββββββββββββββββββββ β
β AG-UI / HTTP βββββββββββββββββββββββββ
βββββββββββ¬ββββββββββ
β
βββββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββββββ
β βΌ β
β A G E N T L A Y E R (optional, any AG-UI framework) β
β β
β Python: copilotkit SDK v0.1.78 + FastAPI / Flask β
β β
β ββββββββββββββ ββββββββββββββ ββββββββββββββ ββββββββββββββ β
β β LangGraph β β CrewAI β β Google ADK β β AWS Strandsβ β
β ββββββββββββββ ββββββββββββββ ββββββββββββββ ββββββββββββββ β
β ββββββββββββββ ββββββββββββββ ββββββββββββββ ββββββββββββββ β
β β Mastra β β PydanticAI β β AG2 β β LlamaIndex β β
β ββββββββββββββ ββββββββββββββ ββββββββββββββ ββββββββββββββ β
β β
β Protocols: AG-UI (β user) Β· MCP (β tools) Β· A2A (β agents)β
β β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Quick Start
New project
npx copilotkit@latest create -f next
Existing project
npm install @copilotkit/βreact-core @copilotkit/βreact-ui @copilotkit/βruntime
Environment
# .env.local
OPENAI_API_KEY="sk-..."
# or ANTHROPIC_API_KEY, GOOGLE_API_KEY, GROQ_API_KEY
Backend API route (app/βapi/βcopilotkit/βroute.ts)
import {
CopilotRuntime,
OpenAIAdapter, // or AnthropicAdapter, GoogleGenerativeAIAdapter, GroqAdapter, LangChainAdapter
copilotRuntimeNextJSAppRouterEndpoint,
} from "@copilotkit/βruntime";
import { NextRequest } from "next/βserver";
const serviceAdapter = new OpenAIAdapter({ model: "gpt-4o" });
const runtime = new CopilotRuntime();
export const POST = async (req: NextRequest) => {
const { handleRequest } = copilotRuntimeNextJSAppRouterEndpoint({
runtime,
serviceAdapter,
endpoint: "/βapi/βcopilotkit",
});
return handleRequest(req);
};
Frontend provider (layout.tsx)
import { CopilotKit } from "@copilotkit/βreact-core";
import "@copilotkit/βreact-ui/βstyles.css";
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<html lang="en">
<body>
<CopilotKit runtimeUrl="/βapi/βcopilotkit">
{children}
</βCopilotKit>
</βbody>
</βhtml>
);
}
Chat UI (page.tsx)
import { CopilotPopup } from "@copilotkit/βreact-ui"; // or CopilotSidebar, CopilotChat
export default function Home() {
return (
<>
<YourApp />
<CopilotPopup
instructions="You are an AI assistant for this app."
labels={{ title: "Assistant", initial: "How can I help?" }}
/>
</>
);
}
Core Hooks
useCopilotReadable -- Expose app state to LLM
useCopilotReadable({
description: "Current user profile and preferences",
value: { name: user.name, role: user.role, preferences },
});
useCopilotAction -- Define executable actions
useCopilotAction({
name: "addItem",
description: "Add a new item to the list",
parameters: [
{ name: "title", type: "string", required: true },
{ name: "priority", type: "string", description: "low, medium, or high" },
],
handler: async ({ title, priority = "medium" }) => {
setItems(prev => [...prev, { id: Date.now().toString(), title, priority }]);
return `Added "${title}" with ${priority} priority`;
},
});
useCopilotChat -- Programmatic chat control
const { appendMessage, stopGeneration, reset, reloadMessages } = useCopilotChat();
useCopilotAdditionalInstructions -- Dynamic context-aware prompts
useCopilotAdditionalInstructions({
instructions: "User is on the settings page. Help them configure preferences.",
});
useCopilotChatSuggestions -- Auto-generate suggestions from app state
useCopilotChatSuggestions({
instructions: "Suggest actions based on the current app state.",
});
useAgent -- v2 agent state sync (superset of useCoAgent)
const { state, setState, run, stop } = useAgent({ name: "my_agent" });
useAgent is the v2 replacement for useCoAgent. It includes all useCoAgent functionality plus time-travel debugging and improved state management. Prefer useAgent for new projects.
CopilotTask -- Run one-off programmatic tasks
import { CopilotTask } from "@copilotkit/βreact-core";
const task = new CopilotTask({ instructions: "Summarize the data" });
await task.run(context);
Advanced Patterns
Detailed guides organized by topic -- load only what's needed:
- Generative UI (static AG-UI, declarative A2UI, open-ended MCP Apps): See references/βgenerative-ui.md
- Shared State & CoAgents (useCoAgent, useAgent, bidirectional sync, LangGraph): See references/βcoagents-shared-state.md
- Human-in-the-Loop (buildtime/βruntime HITL, approval flows, agent steering): See references/βhuman-in-the-loop.md
- Runtime & Adapters (all LLM adapters, framework endpoints, backend actions): See references/βruntime-adapters.md
- Python SDK (LangGraphAgent, FastAPI, actions, state, events): See references/βpython-sdk.md
- Styling & Customization (CSS, custom components, headless mode): See references/βstyling-customization.md
- Troubleshooting (common issues, CORS, env vars, Docker): See references/βtroubleshooting.md
- AG-UI Protocol (events, architecture, CLI scaffolding): See references/βag-ui-protocol.md
UI Components
| Component | Import | Use case |
|---|---|---|
CopilotChat | @copilotkit/βreact-ui | Embedded inline chat panel |
CopilotSidebar | @copilotkit/βreact-ui | Collapsible sidebar chat |
CopilotPopup | @copilotkit/βreact-ui | Floating popup chat |
CopilotTextarea | @copilotkit/βreact-ui | AI-powered textarea drop-in |
All accept instructions, labels, suggestions, custom message/βinput components, and observabilityHooks.
CopilotKit Provider Props
| Prop | Type | Purpose |
|---|---|---|
runtimeUrl | string | Self-hosted runtime endpoint |
publicApiKey | string | Copilot Cloud API key |
headers | object | Custom auth headers |
credentials | string | Cookie handling ("include" for cross-origin) |
agent | string | Default agent name |
properties | object | Thread metadata, authorization |
onError | function | Error handler callback |
showDevConsole | boolean | Dev error banners |
enableInspector | boolean | Debugging inspector tool |
renderActivityMessages | array | Custom renderers (A2UI, etc.) |
Agent Protocols
| Protocol | Purpose | Package |
|---|---|---|
| AG-UI | Agent <-> User interaction, event streaming | @ag-ui/βcore, @ag-ui/βclient |
| MCP | Agent <-> External tools | MCP server integration |
| A2A | Agent <-> Agent communication | A2A protocol support |
Supported Agent Frameworks
LangGraph, CrewAI, Google ADK, AWS Strands, Microsoft Agent Framework, Mastra, PydanticAI, AG2, LlamaIndex, Agno, VoltAgent, Blaxel.
LLM Adapters
OpenAIAdapter, AnthropicAdapter, GoogleGenerativeAIAdapter, GroqAdapter, LangChainAdapter, OpenAIAssistantAdapter.
Key Packages
| Package | Purpose |
|---|---|
@copilotkit/βreact-core | Provider + all hooks |
@copilotkit/βreact-ui | Chat UI components + styles |
@copilotkit/βruntime | Backend runtime + LLM adapters + framework endpoints |
copilotkit (Python) | Python SDK for LangGraph/βCrewAI agents |
@copilotkitnext/βangular | Angular SDK (Angular 18+19) |
@ag-ui/βcore | AG-UI protocol types/βevents |
@ag-ui/βclient | AG-UI client implementation |
Error Handling
Frontend onError callback
<CopilotKit
runtimeUrl="/βapi/βcopilotkit"
onError={(error) => {
console.error("CopilotKit error:", error);
toast.error("AI assistant encountered an error");
}}
>
Tool render "failed" status
All render functions receive status === "failed" when a tool errors. Always handle this:
render: ({ status, args, result }) => {
if (status === "failed") return <ErrorCard message="Tool execution failed" />;
// ...
}
Python SDK exception types
| Exception | Meaning |
|---|---|
ActionNotFoundException | Requested action not registered |
AgentNotFoundException | Requested agent not found in endpoint |
Versioning
Current version: v1.51.3 (Python SDK v0.1.78). The v2 runtime interface is available at the /βv2 path. Next-generation packages use the @copilotkitnext/β* namespace (e.g., @copilotkitnext/βangular).
Common Anti-Patterns
| Don't | Do Instead |
|---|---|
| Put API keys in client-side code | Use server-side env vars + runtime endpoint |
| Create a new CopilotRuntime per request | Instantiate once, reuse across requests |
Skip status checks in render functions | Always handle "inProgress", "complete", "failed" |
Use useCoAgent for new projects | Prefer useAgent (v2 superset with time travel) |
| Hardcode instructions in every component | Use useCopilotAdditionalInstructions for page-specific context |
| Forget to import styles | Add import "@copilotkit/βreact-ui/βstyles.css" in layout |
Mix runtimeUrl and publicApiKey without reason | Pick one deployment mode unless you need hybrid |
| Put heavy computation in action handlers | Return data from handlers, compute in render |
Common Patterns Cheat Sheet
| Want to... | Use |
|---|---|
| Show a chat bubble | <CopilotPopup> |
| Give LLM app context | useCopilotReadable() |
| Let LLM call functions | useCopilotAction() |
| Render UI from tool calls | useFrontendTool() or useRenderToolCall() |
| Default fallback tool renderer | useDefaultTool() |
| Sync state bidirectionally | useCoAgent() |
| Use v2 agent state sync | useAgent() (superset of useCoAgent) |
| Show agent progress | useCoAgentStateRender() |
| Ask user for approval | useHumanInTheLoop() |
| Handle LangGraph interrupts | useLangGraphInterrupt() |
| Control chat programmatically | useCopilotChat() |
| Run one-off tasks | CopilotTask |
| Connect Python agent | CopilotKitRemoteEndpoint + LangGraphAgent |
| Scaffold AG-UI app | npx create-ag-ui-app |
| Persist conversations | Thread model + StorageRunners |
