---
title: "useCopilotKit"
description: "Low-level React hook for accessing the CopilotKit context in React Native"
---
## Overview
`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.
Re-exported from `@copilotkit/react-core/v2`, identical to the [React (V2) `useCopilotKit`](/reference/v2/hooks/useCopilotKit). The only difference is the import path.
`useCopilotKit` is a low-level hook. Most applications should use higher-level
hooks like `useAgent` or `useFrontendTool` instead.
**Throws an error** if used outside of a `CopilotKitProvider`.
## Signature
```tsx
import { useCopilotKit } from "@copilotkit/react-native";
function useCopilotKit(): CopilotKitContextValue;
```
## Parameters
This hook takes no parameters.
## Return Value
The context object containing the core instance and provider-level state.
The live CopilotKit core instance. This is the central coordinator that manages agents, tools, suggestions, and runtime communication. It exposes methods for:
- Agent management (`agents`, `getAgent`)
- Tool registration and execution
- Suggestion configuration and retrieval
- Runtime connection management
- Event subscription via `subscribe()`
The core instance is created once per `CopilotKitProvider` and is stable across re-renders.
Set of tool call IDs currently being executed. This is tracked at the provider level to ensure tool execution events are captured even before child components mount. This is important for scenarios like human-in-the-loop reconnection, where pending tool calls may already exist when the component tree mounts.
## Usage
### Accessing the Core Instance
```tsx
import { Text, View } from "react-native";
import { useCopilotKit } from "@copilotkit/react-native";
function DebugPanel() {
const { copilotkit } = useCopilotKit();
const agents = Object.keys(copilotkit.agents ?? {});
return (
Registered Agents
{agents.map((id) => (
{id}
))}
);
}
```
### Running an Agent
`copilotkit.runAgent()` triggers an agent run directly from code. This is the validated React Native pattern: add a message to the agent, then run it.
```tsx
import { useCallback, useState } from "react";
import { Text, TextInput, TouchableOpacity, View } from "react-native";
import { useAgent, useCopilotKit } from "@copilotkit/react-native";
function Composer() {
const [inputText, setInputText] = useState("");
const { copilotkit } = useCopilotKit();
const { agent } = useAgent({ agentId: "default" });
const handleSend = useCallback(async () => {
const text = inputText.trim();
if (!text || !agent) return;
setInputText("");
agent.addMessage({
id: `user-${Date.now()}`,
role: "user",
content: text,
});
try {
await copilotkit.runAgent({ agent });
} catch (error) {
console.error("CopilotKit runAgent failed:", error);
}
}, [inputText, agent, copilotkit]);
return (
Send
);
}
```
### Subscribing to Core Events
```tsx
import { useEffect } from "react";
import { useCopilotKit } from "@copilotkit/react-native";
function ConnectionMonitor() {
const { copilotkit } = useCopilotKit();
useEffect(() => {
const subscription = copilotkit.subscribe({
onRuntimeConnectionStatusChanged: () => {
console.log("Runtime connection status changed");
},
});
return () => {
subscription.unsubscribe();
};
}, [copilotkit]);
return null;
}
```
### Running a Tool Programmatically
`copilotkit.runTool()` lets you execute a registered frontend tool directly from code, with 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.
```tsx
import { Text, TouchableOpacity } from "react-native";
import { useCopilotKit, useFrontendTool } from "@copilotkit/react-native";
import { z } from "zod";
function ExportButton() {
const { copilotkit } = useCopilotKit();
// Register the tool
useFrontendTool({
name: "exportData",
description: "Export data as CSV",
parameters: z.object({ format: z.string() }),
handler: async ({ format }) => {
const csv = await generateCsv(format);
await saveFile(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 (
Export CSV
);
}
```
#### `runTool` Parameters
Name of the registered frontend tool to execute.
Agent ID for tool and message scoping. Defaults to `"default"`.
Arguments passed to the tool handler.
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
#### `runTool` Return Value
Unique ID of the tool call, matching the message added to history.
Stringified return value from the tool handler (empty string for render-only
tools).
Error message if the handler threw. The tool result message will contain `"Error: ..."`.
### Checking Tool Execution State
```tsx
import { Text } from "react-native";
import { useCopilotKit } from "@copilotkit/react-native";
function ToolExecutionIndicator() {
const { executingToolCallIds } = useCopilotKit();
if (executingToolCallIds.size === 0) {
return null;
}
return Executing {executingToolCallIds.size} tool call(s)...;
}
```
## Behavior
- **Error on Missing Provider**: Throws an error if the hook is used outside of `CopilotKitProvider`.
- **Runtime Status Subscription**: The hook subscribes to `onRuntimeConnectionStatusChanged` events, so components re-render when the runtime connection completes or fails.
- **Stable Core Reference**: The `copilotkit` instance is created once per provider and remains stable across re-renders. Only the `executingToolCallIds` set changes as tool calls begin and complete.
- **Provider-Level Tool Tracking**: `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.
## Related
- [`useAgent`](/reference/react-native/hooks/useAgent): High-level hook for accessing agent instances
- [`useFrontendTool`](/reference/react-native/hooks/useFrontendTool): Register frontend tools that `runTool()` can execute
- [`CopilotKitProvider`](/reference/react-native/components/CopilotKitProvider): The provider component that creates the context
- [React (V2) reference](/reference/v2/hooks/useCopilotKit): The web equivalent this page mirrors