Hooks Overview
Hooks enable automated workflows triggered by specific events during GitHub Copilot coding agent sessions, such as session start, session end, user prompts, and tool usage.What Are Hooks?
Hooks are event-driven automations that execute custom scripts or commands during GitHub Copilot coding agent sessions. They follow the GitHub Copilot hooks specification and provide a powerful way to extend your development workflow.Session Tracking
Log session starts and ends for audit trails and usage analytics
Auto-Commit
Automatically commit and push changes when sessions end
Governance
Scan prompts for security threats and compliance violations
Custom Workflows
Integrate with external tools and services
Hook Structure
Each hook is a folder containing:Available Hook Events
Hooks can be triggered by the following events during a Copilot coding agent session:sessionStart
sessionStart
Triggered when a new Copilot coding agent session begins.Common Use Cases:
- Initialize session logging
- Set up environment variables
- Load project-specific configurations
- Start time tracking
sessionEnd
sessionEnd
Triggered when a Copilot coding agent session ends.Common Use Cases:
- Auto-commit and push changes
- Generate session reports
- Clean up temporary files
- Log session duration and statistics
userPromptSubmitted
userPromptSubmitted
Triggered each time the user submits a prompt to the agent.Common Use Cases:
- Log user prompts for audit
- Scan prompts for security threats
- Track prompt patterns and analytics
- Filter or validate prompts
preToolUse
preToolUse
Triggered before the agent uses a tool.Common Use Cases:
- Validate tool parameters
- Log tool usage for compliance
- Set up tool-specific environments
- Implement custom authorization
postToolUse
postToolUse
Triggered after the agent uses a tool.Common Use Cases:
- Log tool results
- Clean up tool artifacts
- Track tool performance
- Trigger follow-up actions
errorOccurred
errorOccurred
Triggered when an error occurs during the session.Common Use Cases:
- Log errors for debugging
- Send error notifications
- Trigger recovery procedures
- Collect diagnostic information
Hook Configuration
Thehooks.json file defines which commands run on which events. Here’s a complete example:
hooks.json
Configuration Fields
| Field | Type | Description |
|---|---|---|
version | number | Hook specification version (currently 1) |
type | string | Command type (currently only "command" supported) |
bash | string | Path to the script to execute |
cwd | string | Working directory for the command (. for repository root) |
env | object | Environment variables to pass to the script |
timeoutSec | number | Maximum execution time in seconds |
Installation
To install a hook in your repository:Best Practices
Keep Scripts Fast
Use short timeouts and optimize scripts to avoid blocking the agent
Handle Errors Gracefully
Always include error handling - hooks should never crash the session
Respect Privacy
Don’t log sensitive data like full prompts or credentials
Make Hooks Optional
Support environment variables to disable hooks when needed
Security Considerations
- Validate inputs: Never trust data from hooks without validation
- Use least privilege: Run scripts with minimal permissions
- Avoid external calls: Minimize network requests in hooks
- Protect logs: Add log directories to
.gitignore - Review scripts: Audit hook scripts before installing them
When to Use Hooks
Hooks are ideal for:- Automated session logging and audit trails
- Auto-committing changes at session end
- Tracking usage analytics for teams
- Integrating with external tools (Slack, monitoring systems)
- Enforcing governance policies on prompts and tool usage
- Custom session workflows specific to your project
Next Steps
Browse Hook Catalog
Explore available hooks and installation examples
GitHub Copilot Hooks Spec
Read the official hooks specification