CopilotKit Runtime
@copilotkit/runtime is the server half of CopilotKit: it accepts AG-UI protocol
requests, dispatches them to an AbstractAgent (built-in or external), runs the
stream through an AgentRunner, and responds as Server-Sent Events.
This SKILL.md is the index. Read the reference under references/ that matches
your task — do not try to absorb the whole package from this file.
Mental Model — the three dictionaries you hand to CopilotRuntime
new CopilotRuntime({
agents, // Record<string, AbstractAgent> — see wiring-external-agents or built-in-agent
runner, // AgentRunner (optional) — see agent-runners
intelligence, // CopilotKitIntelligence (optional) — see intelligence-mode (auto-wires runner)
mcpApps, // McpAppsConfig (optional) — see wiring-mcp-apps-middleware
a2ui, // A2UIConfig (optional) — see packages/a2ui-renderer skill
hooks, // { onRequest, onBeforeHandler } — see middleware
beforeRequestMiddleware,
afterRequestMiddleware, // legacy — see middleware
transcription, // TranscriptionService (optional) — see transcription
});
You then mount it:
import { createCopilotRuntimeHandler } from "@copilotkit/runtime/v2";
const handler = createCopilotRuntimeHandler({
runtime,
basePath: "/api/copilotkit",
});
export default { fetch: handler };
When to load which reference
| Task |
Reference |
| Mounting on any fetch-native server (Cloudflare Workers, Bun, Deno, Vercel Edge, Next.js App Router, React Router v7, TanStack Start) or delegating from Express/Node |
references/setup-endpoint.md |
Auth / logging / rate-limit / request-scoped guards via hooks.onRequest / hooks.onBeforeHandler (preferred) or legacy beforeRequestMiddleware / afterRequestMiddleware |
references/middleware.md |
Choosing between InMemoryAgentRunner, SqliteAgentRunner, or a custom subclass — including thread-locking semantics and the runner/Intelligence mutual exclusion |
references/agent-runners.md (+ -in-memory.md, -sqlite.md, -custom.md for backend-specific detail) |
| Enabling durable threads + realtime websocket via CopilotKit Intelligence (a managed service, not self-hostable) |
references/intelligence-mode.md |
Voice transcription — implementing a TranscriptionService subclass for the /transcribe endpoint |
references/transcription.md |
Instantiating BuiltInAgent — Simple Mode (classic) or Factory Mode with TanStack AI (preferred AG-UI-compliant default), AI SDK, or custom factory |
references/built-in-agent.md (+ -factory-modes.md, -helper-utilities.md, -model-identifiers.md) |
Defining server-side tools via defineTool for BuiltInAgent.config.tools (Simple Mode only) |
references/server-side-tools.md |
Wiring an external agent framework into CopilotRuntime({ agents }) |
references/wiring-external-agents.md (index) + per-framework refs (wiring-mastra.md, wiring-langgraph.md, wiring-crewai-crews.md, wiring-crewai-flows.md, wiring-pydantic-ai.md, wiring-adk.md, wiring-llamaindex.md, wiring-agno.md, wiring-aws-strands.md, wiring-ms-agent-framework.md, wiring-ag2.md, wiring-a2a.md) |
| Wiring MCP Apps (runtime-level middleware, not an agent) |
references/wiring-mcp-apps-middleware.md |
Invariants and gotchas (load-once, before any reference)
createCopilotRuntimeHandler is the canonical primitive. createCopilotExpressHandler / createCopilotHonoHandler exist but are avoid at all costs — delegate from Express/Hono routes to the fetch primitive instead.
publicLicenseKey is the canonical provider-side field. publicApiKey is a deprecated alias — expect to see it in legacy code, emit the canonical name in new code.
- Intelligence mode auto-wires
IntelligenceAgentRunner. Passing both runner and intelligence to CopilotRuntime is rejected at construction.
- Intelligence mode targets the managed CopilotKit Intelligence service (
api.cloud.copilotkit.ai) and is not self-hostable.
hooks.onRequest runs before beforeRequestMiddleware (hook-based middleware wins for Response short-circuits). beforeRequestMiddleware runs after hooks.onRequest (see fetch-handler.ts:136-147).
identifyUser (Intelligence) does not forward thrown Response objects — convert to 500. Gate auth rejection in hooks.onRequest, which does forward Responses.
agents__unsafe_dev_only and selfManagedAgents are dev-only aliases of each other; do not reach for them in production. Either signals that the SPA is in dev mode.
Reading order for a first-time reader
setup-endpoint — the primitive.
built-in-agent or pick one from wiring-external-agents — the agent.
agent-runners — production persistence choice.
- Optional:
middleware, intelligence-mode, server-side-tools, transcription.
1---
2name: runtime
3description: >
4 @copilotkit/runtime — mount a fetch-native CopilotRuntime on any JS server, wire
5 middleware, pick an AgentRunner, instantiate BuiltInAgent (Factory Mode with TanStack AI
6 is the preferred default) or plug in any of 12 external agent frameworks (Mastra,
7 LangGraph, CrewAI Crews/Flows, PydanticAI, ADK, LlamaIndex, Agno, AWS Strands, MS Agent
8 Framework, AG2, A2A), enable Intelligence mode for durable threads + websocket,
9 register server-side tools via defineTool, and wire voice transcription. Uses the
10 fetch-based createCopilotRuntimeHandler primitive — the Express/Hono adapters are
11 discouraged. Load the reference under references/ that matches your task.
12type: core
13library: copilotkit
14library_version: "1.56.2"
15requires: []
16sources:
17 - "CopilotKit/CopilotKit:packages/runtime/src/v2/runtime/core/fetch-handler.ts"
18 - "CopilotKit/CopilotKit:packages/runtime/src/v2/runtime/core/runtime.ts"
19 - "CopilotKit/CopilotKit:packages/runtime/src/v2/runtime/core/hooks.ts"
20 - "CopilotKit/CopilotKit:packages/runtime/src/v2/runtime/core/middleware.ts"
21 - "CopilotKit/CopilotKit:packages/runtime/src/v2/runtime/runner/agent-runner.ts"
22 - "CopilotKit/CopilotKit:packages/runtime/src/v2/runtime/runner/in-memory.ts"
23 - "CopilotKit/CopilotKit:packages/runtime/src/v2/runtime/runner/intelligence.ts"
24 - "CopilotKit/CopilotKit:packages/runtime/src/v2/runtime/intelligence-platform/client.ts"
25 - "CopilotKit/CopilotKit:packages/runtime/src/v2/runtime/transcription-service/transcription-service.ts"
26 - "CopilotKit/CopilotKit:packages/runtime/src/v2/runtime/handlers/handle-transcribe.ts"
27 - "CopilotKit/CopilotKit:packages/runtime/src/agent/index.ts"
28 - "CopilotKit/CopilotKit:packages/runtime/src/agent/converters/tanstack.ts"
29 - "CopilotKit/CopilotKit:packages/sqlite-runner/src/sqlite-runner.ts"
30 - "CopilotKit/CopilotKit:packages/shared/src/transcription-errors.ts"
31metadata:
32 source: https://skillmd.com/skills/om-scogo/runtime
33---
34
35# CopilotKit Runtime
36
37`@copilotkit/runtime` is the server half of CopilotKit: it accepts AG-UI protocol
38requests, dispatches them to an `AbstractAgent` (built-in or external), runs the
39stream through an `AgentRunner`, and responds as Server-Sent Events.
40
41This SKILL.md is the **index**. Read the reference under `references/` that matches
42your task — do not try to absorb the whole package from this file.
43
44## Mental Model — the three dictionaries you hand to `CopilotRuntime`
45
46```ts
47new CopilotRuntime({
48 agents, // Record<string, AbstractAgent> — see wiring-external-agents or built-in-agent
49 runner, // AgentRunner (optional) — see agent-runners
50 intelligence, // CopilotKitIntelligence (optional) — see intelligence-mode (auto-wires runner)
51 mcpApps, // McpAppsConfig (optional) — see wiring-mcp-apps-middleware
52 a2ui, // A2UIConfig (optional) — see packages/a2ui-renderer skill
53 hooks, // { onRequest, onBeforeHandler } — see middleware
54 beforeRequestMiddleware,
55 afterRequestMiddleware, // legacy — see middleware
56 transcription, // TranscriptionService (optional) — see transcription
57});
58```
59
60You then mount it:
61
62```ts
63import { createCopilotRuntimeHandler } from "@copilotkit/runtime/v2";
64const handler = createCopilotRuntimeHandler({
65 runtime,
66 basePath: "/api/copilotkit",
67});
68export default { fetch: handler };
69```
70
71## When to load which reference
72
73| Task | Reference |
74| ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
75| Mounting on any fetch-native server (Cloudflare Workers, Bun, Deno, Vercel Edge, Next.js App Router, React Router v7, TanStack Start) or delegating from Express/Node | `references/setup-endpoint.md` |
76| Auth / logging / rate-limit / request-scoped guards via `hooks.onRequest` / `hooks.onBeforeHandler` (preferred) or legacy `beforeRequestMiddleware` / `afterRequestMiddleware` | `references/middleware.md` |
77| Choosing between `InMemoryAgentRunner`, `SqliteAgentRunner`, or a custom subclass — including thread-locking semantics and the runner/Intelligence mutual exclusion | `references/agent-runners.md` (+ `-in-memory.md`, `-sqlite.md`, `-custom.md` for backend-specific detail) |
78| Enabling durable threads + realtime websocket via CopilotKit Intelligence (a **managed service**, not self-hostable) | `references/intelligence-mode.md` |
79| Voice transcription — implementing a `TranscriptionService` subclass for the `/transcribe` endpoint | `references/transcription.md` |
80| Instantiating `BuiltInAgent` — Simple Mode (classic) or Factory Mode with TanStack AI (preferred AG-UI-compliant default), AI SDK, or custom factory | `references/built-in-agent.md` (+ `-factory-modes.md`, `-helper-utilities.md`, `-model-identifiers.md`) |
81| Defining server-side tools via `defineTool` for `BuiltInAgent.config.tools` (Simple Mode only) | `references/server-side-tools.md` |
82| Wiring an external agent framework into `CopilotRuntime({ agents })` | `references/wiring-external-agents.md` (index) + per-framework refs (`wiring-mastra.md`, `wiring-langgraph.md`, `wiring-crewai-crews.md`, `wiring-crewai-flows.md`, `wiring-pydantic-ai.md`, `wiring-adk.md`, `wiring-llamaindex.md`, `wiring-agno.md`, `wiring-aws-strands.md`, `wiring-ms-agent-framework.md`, `wiring-ag2.md`, `wiring-a2a.md`) |
83| Wiring MCP Apps (runtime-level middleware, not an agent) | `references/wiring-mcp-apps-middleware.md` |
84
85## Invariants and gotchas (load-once, before any reference)
86
87- `createCopilotRuntimeHandler` is the canonical primitive. `createCopilotExpressHandler` / `createCopilotHonoHandler` exist but are **avoid at all costs** — delegate from Express/Hono routes to the fetch primitive instead.
88- `publicLicenseKey` is the canonical provider-side field. `publicApiKey` is a **deprecated alias** — expect to see it in legacy code, emit the canonical name in new code.
89- Intelligence mode auto-wires `IntelligenceAgentRunner`. Passing both `runner` and `intelligence` to `CopilotRuntime` is rejected at construction.
90- Intelligence mode targets the managed CopilotKit Intelligence service (`api.cloud.copilotkit.ai`) and is **not self-hostable**.
91- `hooks.onRequest` runs **before** `beforeRequestMiddleware` (hook-based middleware wins for Response short-circuits). `beforeRequestMiddleware` runs **after** `hooks.onRequest` (see `fetch-handler.ts:136-147`).
92- `identifyUser` (Intelligence) does **not** forward thrown `Response` objects — convert to 500. Gate auth rejection in `hooks.onRequest`, which does forward Responses.
93- `agents__unsafe_dev_only` and `selfManagedAgents` are dev-only aliases of each other; do not reach for them in production. Either signals that the SPA is in dev mode.
94
95## Reading order for a first-time reader
96
971. `setup-endpoint` — the primitive.
982. `built-in-agent` **or** pick one from `wiring-external-agents` — the agent.
993. `agent-runners` — production persistence choice.
1004. Optional: `middleware`, `intelligence-mode`, `server-side-tools`, `transcription`.