docs/content/docs/integrations/langgraph/coagent-troubleshooting/error-debugging.mdx
CopilotKit provides comprehensive error handling capabilities to help you debug issues and monitor your application's behavior. Whether you're developing locally or running in production, CopilotKit offers tools to capture, understand, and resolve errors effectively.
For local development, enable the dev console to see errors directly in your UI:
import { CopilotKit } from "@copilotkit/react-core";
export default function App() {
return (
<CopilotKit
runtimeUrl="<your-runtime-url>"
showDevConsole={true} // [!code highlight]
>
</CopilotKit>
);
}
For production applications, use error observability hooks to send errors to monitoring services (requires publicApiKey):
import { CopilotKit } from "@copilotkit/react-core";
export default function App() {
return (
<CopilotKit
runtimeUrl="<your-runtime-url>"
publicApiKey="ck_pub_your_key" // [!code highlight]
onError={(errorEvent) => {
// [!code highlight]
// Send to your monitoring service
console.error("CopilotKit Error:", errorEvent);
// Example: Send to analytics
analytics.track("copilotkit_error", {
type: errorEvent.type,
source: errorEvent.context.source,
timestamp: errorEvent.timestamp,
});
}} // [!code highlight]
showDevConsole={false} // Hide dev console in production
>
</CopilotKit>
);
}
showDevConsole)The dev console provides immediate visual feedback during development:
<CopilotKit runtimeUrl="<your-runtime-url>" showDevConsole={true}>
</CopilotKit>
Features:
Best for:
onError)The error observability hooks provide programmatic access to detailed error information for monitoring and analytics:
import { CopilotErrorEvent } from "@copilotkit/shared";
<CopilotKit
publicApiKey="ck_pub_your_key"
onError={(errorEvent: CopilotErrorEvent) => {
// Send error data to monitoring services
switch (errorEvent.type) {
case "error":
logToService("Critical error", errorEvent);
break;
case "request":
logToService("Request started", errorEvent);
break;
case "response":
logToService("Response received", errorEvent);
break;
case "agent_state":
logToService("Agent state change", errorEvent);
break;
}
}}
>
</CopilotKit>;
Features:
Requirements:
publicApiKey from Copilot CloudThe onError handler receives detailed error events with rich context:
interface CopilotErrorEvent {
type:
| "error"
| "request"
| "response"
| "agent_state"
| "action"
| "message"
| "performance";
timestamp: number;
context: {
source: "ui" | "runtime" | "agent";
request?: {
operation: string;
method?: string;
url?: string;
startTime: number;
};
response?: {
endTime: number;
latency: number;
};
agent?: {
name: string;
nodeName?: string;
};
messages?: {
input: any[];
messageCount: number;
};
technical?: {
environment: string;
stackTrace?: string;
};
};
error?: any; // Present for error events
}
<CopilotKit
publicApiKey="ck_pub_your_key"
onError={(errorEvent) => {
console.error("[CopilotKit Error]", {
type: errorEvent.type,
timestamp: new Date(errorEvent.timestamp).toISOString(),
context: errorEvent.context,
error: errorEvent.error,
});
}}
>
</CopilotKit>
// Example with Sentry
import * as Sentry from "@sentry/react";
<CopilotKit
publicApiKey="ck_pub_your_key"
onError={(errorEvent) => {
if (errorEvent.type === "error") {
Sentry.captureException(errorEvent.error, {
tags: {
source: errorEvent.context.source,
operation: errorEvent.context.request?.operation,
},
extra: {
context: errorEvent.context,
timestamp: errorEvent.timestamp,
},
});
}
}}
>
</CopilotKit>;
<CopilotKit
publicApiKey="ck_pub_your_key"
onError={(errorEvent) => {
// Track different error types
analytics.track("copilotkit_event", {
event_type: errorEvent.type,
source: errorEvent.context.source,
agent_name: errorEvent.context.agent?.name,
latency: errorEvent.context.response?.latency,
error_message: errorEvent.error?.message,
timestamp: errorEvent.timestamp,
});
}}
>
</CopilotKit>
<CopilotKit
runtimeUrl="http://localhost:3000/api/copilotkit"
showDevConsole={true} // Show visual errors
onError={(errorEvent) => {
// Simple console logging for development
console.log("CopilotKit Event:", errorEvent);
}}
>
</CopilotKit>
<CopilotKit
runtimeUrl="https://your-app.com/api/copilotkit"
publicApiKey={process.env.NEXT_PUBLIC_COPILOTKIT_API_KEY}
showDevConsole={false} // Hide from users
onError={(errorEvent) => {
// Production error observability
if (errorEvent.type === "error") {
// Log critical errors
logger.error("CopilotKit Error", {
error: errorEvent.error,
context: errorEvent.context,
timestamp: errorEvent.timestamp,
});
// Send to monitoring service
monitoring.captureError(errorEvent.error, {
extra: errorEvent.context,
});
}
}}
>
</CopilotKit>
To use error observability hooks, you'll need a Copilot Cloud account:
NEXT_PUBLIC_COPILOTKIT_API_KEY=ck_pub_your_key_here
<CopilotKit publicApiKey={process.env.NEXT_PUBLIC_COPILOTKIT_API_KEY}>
</CopilotKit>
showDevConsole={true} during development for immediate feedbackshowDevConsole={false} in production to hide errors from usersonError hook for monitoringIf your onError hook isn't being called:
ck_pub_showDevConsole={true} to see if errors are occurringIf the dev console isn't displaying errors:
true