Prerequisites
Before you begin, make sure you have:- Node.js 18 or later installed
- An OpenAI API key (or other LLM provider API key)
- Basic knowledge of React and Next.js
Quick Start with CLI
The fastest way to get started is using the CopilotKit CLI:Create a new project
For a brand new project, run:For an existing project, navigate to your project directory and run:
Set up environment variables
Create a
.env.local file in your project root and add your API key:.env.local
CopilotKit supports multiple LLM providers including OpenAI, Anthropic, and Google. You can configure your preferred provider in the runtime setup.
Start your development server
Manual Setup
If you prefer to set up CopilotKit manually, follow these steps:Set up the runtime endpoint
Create an API route at
app/api/copilotkit/route.ts:app/api/copilotkit/route.ts
The
BuiltInAgent is CopilotKit’s built-in agent that works out of the box. You can also integrate with LangGraph, CrewAI, or create custom agents.Add the provider to your layout
Wrap your app with the CopilotKit provider in
app/layout.tsx:app/layout.tsx
Understanding the Setup
Frontend Components
CopilotKit provides three main UI components:CopilotSidebar
A slide-out sidebar chat interface that sits alongside your content
CopilotPopup
A popup chat bubble that floats over your content
CopilotChat
A full-featured chat component you can embed anywhere
Runtime Setup
The runtime is the server-side component that:- Connects to your LLM provider (OpenAI, Anthropic, Google, etc.)
- Manages agent execution and state
- Handles tool calls and generative UI rendering
- Provides real-time streaming over Server-Sent Events (SSE)
Frontend Actions
Frontend actions let your agent interact with your UI. In the example above, thesetThemeColor action allows the AI to change the page’s theme color. The agent automatically learns about available actions and when to use them.
Next Steps
Now that you have a basic CopilotKit app running, explore these key features:Add Backend Tools
Define tools that let your agent fetch data, call APIs, or perform calculations
Implement Generative UI
Let your agent render custom UI components dynamically
Use Shared State
Synchronize state between your agent and UI in real-time
Add Human-in-the-Loop
Request user confirmation or input during agent execution
Examples
Check out complete working examples in our repository:- Next.js with Built-in Agent - Full-featured demo with multiple patterns
- LangGraph Integration - Human-in-the-loop with LangGraph
- Express Backend - Node.js/Express server setup
Troubleshooting
Runtime connection errors
Runtime connection errors
Make sure:
- Your runtime endpoint is running and accessible
- The
runtimeUrlin CopilotKit provider matches your API route - CORS is properly configured if your frontend and backend are on different domains
Agent not responding
Agent not responding
Check that:
- Your API key is correctly set in environment variables
- The API key has sufficient credits/permissions
- The model name is correct (e.g., “openai/gpt-4o-mini”)
Actions not being called
Actions not being called
Verify that:
- Your action descriptions are clear and specific
- Action names follow camelCase convention
- Parameters are properly typed with descriptive descriptions
Need more help? Join our Discord community or check out the documentation.
