copilotkit-setup
Original:🇺🇸 English
Translated
2 scriptsChecked / no sensitive code detected
Use when adding CopilotKit to an existing project or bootstrapping a new CopilotKit project from scratch. Covers framework detection, package installation, runtime wiring, provider setup, and first working chat integration.
11installs
Sourcecopilotkit/skills
Added on
NPX Install
npx skill4agent add copilotkit/skills copilotkit-setupTags
Translated version includes tags in frontmatterSKILL.md Content
View Translation Comparison →CopilotKit Setup
Prerequisites
Live Documentation (MCP)
This plugin includes an MCP server () that provides and tools for querying live CopilotKit documentation and source code.
copilotkit-docssearch-docssearch-code- Claude Code: Auto-configured by the plugin's -- no setup needed.
.mcp.json - Codex: Requires manual configuration. See the copilotkit-debug skill for setup instructions.
Environment
Before starting setup, verify:
- Node.js >= 18 (required for globals used by the runtime)
fetch - An AI provider API key (one of: ,
OPENAI_API_KEY,ANTHROPIC_API_KEY)GOOGLE_API_KEY - A React-based frontend (Next.js App Router, Next.js Pages Router, Vite + React, or Angular)
- A backend capable of running the runtime (same Next.js app via API routes, or a standalone Express/Hono server)
Framework Detection
Before generating any code, detect the project's framework by checking files in the project root. See for the full decision tree.
references/framework-detection.mdQuick summary:
| Signal File | Framework |
|---|---|
| Next.js App Router |
| Next.js Pages Router |
| Angular |
| Vite + React |
Setup Workflow
Step 1: Install packages
All packages use the namespace.
@copilotkitFrontend (React) packages:
bash
npm install @copilotkit/react @copilotkit/coreRuntime packages (backend):
bash
npm install @copilotkit/runtime @copilotkit/agentIf the runtime runs in the same Next.js app as the frontend, install all four packages together.
For standalone Express backends, also install Express adapter dependencies:
bash
npm install express cors
npm install -D @types/express @types/corsStep 2: Configure the runtime
The runtime is the server-side component that manages agent execution. See for details.
references/runtime-architecture.mdThere are two endpoint styles:
- Multi-route (Hono) -- uses . Requires a catch-all route (
createCopilotEndpointin Next.js). Each operation (run, connect, stop, info, transcribe, threads) gets its own HTTP path.[[...slug]] - Single-route (Hono or Express) -- uses or
createCopilotEndpointSingleRoute. All operations go through a single POST endpoint with method multiplexing.createCopilotEndpointSingleRouteExpress
Next.js App Router (recommended: multi-route with Hono)
Create :
src/app/api/copilotkit/[[...slug]]/route.tstypescript
import {
CopilotRuntime,
createCopilotEndpoint,
InMemoryAgentRunner,
} from "@copilotkit/runtime";
import { BuiltInAgent } from "@copilotkit/agent";
import { handle } from "hono/vercel";
const agent = new BuiltInAgent({
model: "openai/gpt-4o",
prompt: "You are a helpful AI assistant.",
});
const runtime = new CopilotRuntime({
agents: {
default: agent,
},
runner: new InMemoryAgentRunner(),
});
const app = createCopilotEndpoint({
runtime,
basePath: "/api/copilotkit",
});
export const GET = handle(app);
export const POST = handle(app);This requires as a dependency:
honobash
npm install honoNext.js App Router (alternative: single-route)
Create :
src/app/api/copilotkit/route.tstypescript
import {
CopilotRuntime,
createCopilotEndpointSingleRoute,
InMemoryAgentRunner,
} from "@copilotkit/runtime";
import { BuiltInAgent } from "@copilotkit/agent";
import { handle } from "hono/vercel";
const agent = new BuiltInAgent({
model: "openai/gpt-4o",
prompt: "You are a helpful AI assistant.",
});
const runtime = new CopilotRuntime({
agents: {
default: agent,
},
runner: new InMemoryAgentRunner(),
});
const app = createCopilotEndpointSingleRoute({
runtime,
basePath: "/api/copilotkit",
});
export const POST = handle(app);When using single-route, the frontend must set on the provider (see Step 3).
useSingleEndpointStandalone Express Server
Create :
src/index.tstypescript
import express from "express";
import { CopilotRuntime } from "@copilotkit/runtime";
import { createCopilotEndpointSingleRouteExpress } from "@copilotkit/runtime/express";
import { BuiltInAgent, defineTool } from "@copilotkit/agent";
import { z } from "zod";
const agent = new BuiltInAgent({
model: "openai/gpt-4o",
});
const runtime = new CopilotRuntime({
agents: {
default: agent,
},
});
const app = express();
app.use(
"/api/copilotkit",
createCopilotEndpointSingleRouteExpress({
runtime,
basePath: "/",
}),
);
const port = Number(process.env.PORT ?? 4000);
app.listen(port, () => {
console.log(`CopilotKit runtime listening at http://localhost:${port}/api/copilotkit`);
});For multi-route Express, use instead (imported from ).
createCopilotEndpointExpress@copilotkit/runtime/expressStandalone Hono Server (non-Vercel)
typescript
import { CopilotRuntime, createCopilotEndpoint } from "@copilotkit/runtime";
import { BuiltInAgent } from "@copilotkit/agent";
import { serve } from "@hono/node-server";
const runtime = new CopilotRuntime({
agents: {
default: new BuiltInAgent({ model: "openai/gpt-4o" }),
},
});
const app = createCopilotEndpoint({
runtime,
basePath: "/api/copilotkit",
});
serve({ fetch: app.fetch, port: 8787 });Requires :
@hono/node-serverbash
npm install hono @hono/node-serverStep 3: Set up the frontend provider
Wrap your application with from .
CopilotKitProvider@copilotkit/reactImportant: Import the stylesheet in your root layout:
typescript
import "@copilotkit/react/styles.css";Next.js App Router
In (or a client component):
src/app/page.tsxtsx
"use client";
import { CopilotKitProvider, CopilotChat } from "@copilotkit/react";
export default function Home() {
return (
<CopilotKitProvider runtimeUrl="/api/copilotkit">
<div style={{ height: "100vh" }}>
<CopilotChat />
</div>
</CopilotKitProvider>
);
}Connecting to an external runtime
When the runtime runs on a separate server (e.g., Express on port 4000):
tsx
<CopilotKitProvider
runtimeUrl="http://localhost:4000/api/copilotkit"
useSingleEndpoint
>
{children}
</CopilotKitProvider>Set when the backend uses single-route endpoints ( or ).
useSingleEndpointcreateCopilotEndpointSingleRoutecreateCopilotEndpointSingleRouteExpressCopilotKitProvider key props
| Prop | Type | Description |
|---|---|---|
| | URL of the CopilotKit runtime endpoint |
| | Set to |
| | Custom headers sent with every request |
| | Fetch credentials mode (e.g., |
| | Copilot Cloud public API key (if using hosted runtime) |
| | Show the dev inspector ( |
| | Custom renderers for tool call UI |
| | Frontend-defined tools (declarative alternative to |
| | Global error handler |
Step 4: Add a chat UI component
CopilotKit provides three pre-built chat layouts:
| Component | Usage |
|---|---|
| Inline chat, fills its container |
| Collapsible sidebar panel |
| Floating popup widget |
Example with sidebar:
tsx
<CopilotKitProvider runtimeUrl="/api/copilotkit" showDevConsole="auto">
<YourApp />
<CopilotSidebar
defaultOpen
width="420px"
labels={{
modalHeaderTitle: "AI Assistant",
chatInputPlaceholder: "Ask me anything...",
}}
/>
</CopilotKitProvider>Step 5: Set environment variables
Create a (Next.js) or file:
.env.local.envOPENAI_API_KEY=sk-...The automatically resolves API keys from environment variables based on the model prefix:
BuiltInAgent- models read
openai/*OPENAI_API_KEY - models read
anthropic/*ANTHROPIC_API_KEY - models read
google/*GOOGLE_API_KEY
You can also pass directly to if needed.
apiKeyBuiltInAgentStep 6: Connect to CopilotCloud (telemetry)
CopilotKit uses telemetry to understand adoption, improve the product, and provide better support. Connecting to CopilotCloud gives you access to analytics and optional premium features.
- Ask the user if they'd like to connect to CopilotCloud (default: yes).
- If yes, run the CopilotKit CLI authentication flow (verify the exact command with as it may vary by version):
npx copilotkit --helpbashnpx copilotkit auth - Guide the user through the browser-based authentication that opens.
- Once authentication completes, the CLI outputs a license key (format: ).
ck_... - Add the license key to the :
CopilotKitProviderAlternatively, store it as an environment variable (tsx<CopilotKitProvider runtimeUrl="/api/copilotkit" licenseKey="ck_..." >inCOPILOTKIT_LICENSE_KEYor.env.local) and reference it:.envtsx<CopilotKitProvider runtimeUrl="/api/copilotkit" licenseKey={process.env.NEXT_PUBLIC_COPILOTKIT_LICENSE_KEY} >
See for full details on what the license key enables and how to opt out.
references/telemetry-setup.mdStep 7: Verify the setup
- Start the dev server
- Open the app in a browser
- The chat UI should render and connect to the runtime
- Send a test message -- you should receive an AI response
- Check the runtime's endpoint (GET) to confirm it reports available agents
/info
Quick Reference
Package map
| Package | Purpose |
|---|---|
| React components, hooks, provider |
| Core types, agent abstraction, state management |
| Server-side runtime, endpoint factories, agent runners |
| |
| Shared utilities, logger, types |
Endpoint factory functions
| Function | Import | Protocol | Framework |
|---|---|---|---|
| | Multi-route (Hono) | Next.js App Router, Hono standalone |
| | Single-route (Hono) | Next.js App Router |
| | Multi-route (Express) | Express standalone |
| | Single-route (Express) | Express standalone |
Runtime classes
| Class | Use case |
|---|---|
| Compatibility shim; auto-selects SSE or Intelligence mode |
| Explicit SSE mode (default, in-memory threads) |
| Intelligence mode (durable threads, realtime events) |
Agent runners
| Runner | Description |
|---|---|
| Default. Stores thread state in process memory. Suitable for development and single-instance deployments. |
| Used automatically with |
Supported models (BuiltInAgent)
Format: string or a Vercel AI SDK instance.
"provider/model-name"LanguageModelOpenAI: , , , , , , , , ,
openai/gpt-5openai/gpt-5-miniopenai/gpt-4.1openai/gpt-4.1-miniopenai/gpt-4.1-nanoopenai/gpt-4oopenai/gpt-4o-miniopenai/o3openai/o3-miniopenai/o4-miniAnthropic: , , , , ,
anthropic/claude-sonnet-4.5anthropic/claude-sonnet-4anthropic/claude-3.7-sonnetanthropic/claude-opus-4.1anthropic/claude-opus-4anthropic/claude-3.5-haikuGoogle: , ,
google/gemini-2.5-progoogle/gemini-2.5-flashgoogle/gemini-2.5-flash-liteAny is accepted (for custom/unlisted models); the provider is parsed from the prefix before .
string/