What is Copilot Runtime?

Copilot Runtime is the server-side element of CopilotKit. It is responsible for handling requests from your copilot and sending them to the desired LLM. Copilot Runtime can be hosted on your own infrastructure or via Copilot Cloud.

Option 1: Use Copilot Cloud

Copilot Cloud is the easiest way to get started with CopilotKit. It allows you to start building powerful copilots without having to manage your own infrastructure.

Get a free hosted Copilot Runtime via Copilot Cloud

Try Copilot Cloud
  1. First, sign up to Copilot Cloud
  2. Add your OpenAI API Key
  3. Copy your Copilot Cloud Public API Key

Initialize CopilotKit using Copilot Cloud

Using Copilot Cloud is easy. Simply provide your public API key to the <CopilotKit /> component:

<CopilotKit publicApiKey="Your Copilot Cloud API key">
  ...
</CopilotKit>

Option 2: Host your own Copilot Runtime

Install CopilotKit Backend packages

Install the @copilotkit/runtime package:

Set up the Copilot Runtime endpoint

Make sure you have the OPENAI_API_KEY environment variable set up.

Create a new route to handle the /api/copilotkit endpoint:

app/copilotkit/route.ts
import {
  CopilotRuntime,
  OpenAIAdapter,
  copilotRuntimeNextJSAppRouterEndpoint,
} from "@copilotkit/runtime";
import { NextRequest } from "next/server";
import OpenAI from "openai";

const openai = new OpenAI();
const serviceAdapter = new OpenAIAdapter({ openai });

const runtime = new CopilotRuntime();

export const POST = async (req: NextRequest) => {
  const { handleRequest } = copilotRuntimeNextJSAppRouterEndpoint({
    runtime,
    serviceAdapter,
    endpoint: req.nextUrl.pathname,
  });

  return handleRequest(req);
};