Skip to main content

Use hooks

Customize the behavior of Copilot SDK sessions at key points in the conversation lifecycle using hooks.

Quickstart for hooks

Get started with hooks in Copilot SDK to control tool execution, transform results, add context, handle errors, and audit interactions.

Pre-tool use hook

Use the onPreToolUse hook to control tool execution, modify arguments, and add context before a tool runs in Copilot SDK.

Post-tool use hook

Use the onPostToolUse hook to transform tool results, log tool execution, and add context after a tool runs in Copilot SDK.

User prompt submitted hook

Use the onUserPromptSubmitted hook to modify prompts, add context, and filter user input in Copilot SDK.

Session lifecycle hooks

Use the onSessionStart and onSessionEnd hooks to initialize context, clean up resources, and track session metrics in Copilot SDK.

Error handling hook

Use the onErrorOccurred hook to implement custom error logging, track error patterns, and provide user-friendly error messages in Copilot SDK.