CopilotKitCore
Framework-agnostic client that connects to a CopilotRuntime and manages agents, tools, context, and suggestions.
Overview
CopilotKitCore is the main entry point of @copilotkit/core. It connects to a CopilotRuntime over the AG-UI protocol, exposes the available agents, holds the registry of frontend tools, shared context, and suggestions, and notifies subscribers when any of that state changes. The React, Angular, and vanilla bindings all wrap a single instance of this class — reach for it directly when you need an agent client outside of those frameworks.
Import
import { CopilotKitCore } from "@copilotkit/core";Constructor
new CopilotKitCore(config: CopilotKitCoreConfig)Configuration object. See CopilotKitCoreConfig for the full type.
Endpoint of the CopilotRuntime that requests are sent to.
Transport style for runtime endpoints. "auto" probes REST and falls back to single-route.
Headers appended to every HTTP request made by the core.
Credentials mode for fetch requests (e.g. "include" for HTTP-only cookies).
Values sent as forwardedProps to the AG-UI agent.
Frontend tools available to the core. See FrontendTool.
Suggestion configurations registered at construction. See SuggestionsConfig.
Local agent instances keyed by name. Development only — production requires a CopilotRuntime.
Enables debug logging for the client-side event pipeline.
Methods
Agents
Returns the agent registered under id, or undefined if none exists.
Runs an agent against the runtime, streaming events and executing any frontend tool calls it produces.
Connects to an agent to receive its current state and message stream without starting a new run.
Aborts the current run for the given agent.
Registers a ProxiedCopilotRuntimeAgent under a local agentId that routes outbound requests to runtimeAgentId. Returns the proxy and an unregister cleanup handle. Throws if agentId is already taken.
Adds a local agent instance. Development only.
Removes a local agent by id. Development only.
Replaces the full set of local agents. Development only.
Tools
Registers a frontend tool the agent can call. If a tool with the same name and agentId is already registered, the call is a no-op (a warning is logged). See FrontendTool.
Removes a registered tool by name. With no agentId, removes only the global (unscoped) tool of that name; with agentId, removes only the tool scoped to that agent.
Returns a registered tool, or undefined if not found.
Replaces the entire tool registry.
Executes a registered frontend tool directly, without an LLM turn. The handler runs, render components appear, and both the tool call and result messages are appended to agent.messages.
Context
Adds a context entry shared with the agent and returns its generated id.
Removes a context entry by id.
Suggestions
Registers a suggestion configuration and returns its generated id.
Removes a suggestion configuration by id.
Returns the current suggestions and loading state for an agent.
Re-generates suggestions for an agent.
Clears the current suggestions for an agent.
Subscriptions
Subscribes to core lifecycle events (agents changed, context changed, errors, runtime connection status, and more). Returns a subscription with an unsubscribe() method. See CopilotKitCoreSubscriber.
Subscribes to a single agent's message, state, and run-lifecycle callbacks with optional throttling. Every callback is wrapped with error protection. Use agent.subscribe() directly when you need AG-UI event handlers or per-item notifications.
Configuration
Updates the runtime endpoint.
Updates the transport style.
Replaces the request headers and re-applies them to existing agents.
Replaces the fetch credentials mode and re-applies it to existing agents.
Replaces the forwarded properties sent to agents.
Toggles debug logging for the client-side event pipeline.
Sets the default throttle interval (ms) used by subscribeToAgentWithOptions. Invalid values are ignored.
State queries
Returns the agent state snapshot recorded for a specific run.
Returns the run id that produced a given message.
Returns the ordered run ids recorded for a thread.
Properties (read-only)
The currently registered agents, keyed by id.
The currently registered frontend tools.
The current shared context entries, keyed by id.
The configured runtime endpoint.
The current runtime connection status. See CopilotKitCoreRuntimeConnectionStatus.
Snapshots of the configured headers, credentials mode, and forwarded properties.
Usage
import { CopilotKitCore } from "@copilotkit/core";
const copilotkit = new CopilotKitCore({
runtimeUrl: "/api/copilotkit",
tools: [
{
name: "showAlert",
handler: ({ message }: { message: string }) => window.alert(message),
},
],
});
const subscription = copilotkit.subscribe({
onRuntimeConnectionStatusChanged: ({ status }) => console.log(status),
});
// `agents` populates after the runtime responds — `onAgentsChanged` (above)
// fires when they're ready; this guarded lookup is for illustration.
const agent = copilotkit.getAgent("default");
if (agent) {
await copilotkit.runAgent({ agent });
}
subscription.unsubscribe();Behavior
- Asynchronous agent population —
agentsis populated after the runtime responds, so it may be empty immediately after construction. Usesubscribe({ onAgentsChanged })to react when agents become available. - Error surfacing — failures (runtime fetch, agent run, tool execution, subscriber callbacks) are delivered through the
onErrorsubscriber callback with a CopilotKitCoreErrorCode; they are not thrown from the originating method — exceptrunTool(), which also throws when the named tool or its resolved agent cannot be found. - Subscriber isolation — a throw in one subscriber callback is caught and logged; it does not stall notifications to other subscribers.
__unsafe_dev_onlyAPIs — the*Agent__unsafe_dev_onlymethods register local in-memory agents for development. Production setups must go through a CopilotRuntime.