showcase/shell-docs/src/content/reference/hooks/useCopilotKit.mdx
useCopilotKit is a low-level React hook that returns the CopilotKit context value, providing direct access to the core instance and provider-level state. It subscribes to runtime connection status changes and triggers re-renders when the connection status updates.
Throws an error if used outside of a CopilotKitProvider (or the <CopilotKit> wrapper component).
import { useCopilotKit } from "@copilotkit/react-core/v2";
function useCopilotKit(): CopilotKitContextValue;
This hook takes no parameters.
The core instance is created once per `CopilotKitProvider` and is stable across re-renders.
function DebugPanel() {
const { copilotkit } = useCopilotKit();
const agents = Object.keys(copilotkit.agents ?? {});
return (
<div>
<h3>Registered Agents</h3>
<ul>
{agents.map((id) => (
<li key={id}>{id}</li>
))}
</ul>
</div>
);
}
function ConnectionMonitor() {
const { copilotkit } = useCopilotKit();
useEffect(() => {
const subscription = copilotkit.subscribe({
onRuntimeConnectionStatusChanged: () => {
console.log("Runtime connection status changed");
},
});
return () => {
subscription.unsubscribe();
};
}, [copilotkit]);
return null;
}
copilotkit.runTool() lets you execute a registered frontend tool directly from code — no LLM turn required. The tool's handler runs, render components appear in the UI, and both the tool call and result are added to the agent's message history.
function ExportButton() {
const { copilotkit } = useCopilotKit();
// Register the tool
useFrontendTool({
name: "exportData",
description: "Export data as CSV",
parameters: [{ name: "format", type: "string" }],
handler: async ({ format }) => {
const csv = await generateCsv(format);
downloadFile(csv);
return `Exported as ${format}`;
},
});
// Trigger it from a button — no LLM needed
const handleExport = async () => {
const { result, error } = await copilotkit.runTool({
name: "exportData",
parameters: { format: "csv" },
});
if (error) console.error(error);
};
return <button onClick={handleExport}>Export CSV</button>;
}
runTool Parameters<PropertyReference name="parameters" type="Record<string, unknown>" default="{}"
Arguments passed to the tool handler. </PropertyReference>
<PropertyReference name="followUp" type="string | false" default="false"> Controls whether an LLM follow-up is triggered after execution: - `false` — execute tool and stop (default) - `"generate"` — trigger an agent run so the LLM responds to the tool result - Any other string — add a user message with this text, then trigger an agent run </PropertyReference> </PropertyReference>runTool Return Valuefunction ToolExecutionIndicator() {
const { executingToolCallIds } = useCopilotKit();
if (executingToolCallIds.size === 0) {
return null;
}
return <div>Executing {executingToolCallIds.size} tool call(s)...</div>;
}
CopilotKitProvider or the <CopilotKit> wrapper component.onRuntimeConnectionStatusChanged events, so components re-render when the runtime connection completes or fails.copilotkit instance is created once per provider and remains stable across re-renders. Only the executingToolCallIds set changes as tool calls begin and complete.executingToolCallIds is tracked at the provider level rather than in individual components. This ensures that tool execution start events fired before child components mount are not lost.useAgent -- High-level hook for accessing agent instancesuseFrontendTool -- Register frontend tools that runTool() can executeCopilotKitProvider -- The provider component that creates the context