CopilotKit
Use OP Inference as the model behind a CopilotKit runtime.
Point CopilotKit's open-source runtime at our base URL. Tested with @copilotkit/runtime 1.79.1
in a Next.js App Router route.
npm install @copilotkit/runtime @copilotkit/react-core @copilotkit/react-ui @ai-sdk/openai// app/api/copilotkit/route.ts
import { createOpenAI } from "@ai-sdk/openai";
import {
CopilotRuntime,
OpenAIAdapter,
copilotRuntimeNextJSAppRouterEndpoint,
} from "@copilotkit/runtime";
// Unset, createOpenAI would fall back to OPENAI_API_KEY and send that key here.
const apiKey = process.env.OP_API_KEY;
if (!apiKey) throw new Error("OP_API_KEY is not set");
// OpenAIAdapter calls the Responses API, which is not served here.
// This override makes it call chat completions instead.
class OPInferenceAdapter extends OpenAIAdapter {
override getLanguageModel() {
return createOpenAI({
baseURL: "https://api.opinference.com/v1",
apiKey,
}).chat(this.model);
}
}
export async function POST(req: Request) {
const { handleRequest } = copilotRuntimeNextJSAppRouterEndpoint({
runtime: new CopilotRuntime(),
serviceAdapter: new OPInferenceAdapter({ model: "openai/gpt-oss-120b" }),
endpoint: "/api/copilotkit",
});
return handleRequest(req);
}On the page, wrap your app in <CopilotKit runtimeUrl="/api/copilotkit">. Frontend actions you
declare with useCopilotAction reach the model as tools, so pick a model that supports tool
calling.
Do not use OpenAIAdapter without the override, or the built-in agent's OPENAI_BASE_URL
switch. The first calls /v1/responses. The second strips the openai/ prefix from the model
id, so the model is not found.
What it costs
The runtime adds two tools of its own, AGUISendStateSnapshot and AGUISendStateDelta, to every
request. Their definitions count as prompt tokens. A tool call takes two requests: one that returns
the call, and one that sends the result back.
Your route spends whatever key it holds, for anyone who can reach it. Authenticate the route before you deploy it, and set a spend cap on the key. See Key restrictions and spend caps.