Using hooks with GitHub Copilot agents
Extend and customize GitHub Copilot agent behavior by executing custom shell commands at key points during agent execution.
Hooks allow you to extend and customize the behavior of GitHub Copilot agents by executing custom shell commands at key points during agent execution. For a conceptual overview of hooks—including details of the available hook triggers—see About hooks.
Creating a hook in a repository on GitHub
Create a new hooks.json file with the name of your choice in the .github/hooks/ folder of your repository. The hooks configuration file must be present on your repository's default branch to be used by Copilot coding agent. For GitHub Copilot CLI, hooks are loaded from your current working directory.
In your text editor, copy and paste the following hook template. Remove any hooks you don't plan on using from the hooks array.
{
"version": 1,
"hooks": {
"sessionStart": [...],
"sessionEnd": [...],
"userPromptSubmitted": [...],
"preToolUse": [...],
"postToolUse": [...],
"errorOccurred": [...]
}
}
Configure your hook syntax under the bash or powershell keys, or directly reference script files you have created.
This example runs a script that outputs the start date of the session to a log file using the sessionStart hook:
"sessionStart": [
{
"type": "command",
"bash": "echo \"Session started: $(date)\" >> logs/session.log",
"powershell": "Add-Content -Path logs/session.log -Value \"Session started: $(Get-Date)\"",
"cwd": ".",
"timeoutSec": 10
}
],
This example calls out to an external log-prompt script:
"userPromptSubmitted": [
{
"type": "command",
"bash": "./scripts/log-prompt.sh",
"powershell": "./scripts/log-prompt.ps1",
"cwd": "scripts",
"env": {
"LOG_LEVEL": "INFO"
}
}
],
For a full reference on the input JSON from agent sessions along with sample scripts, see Hooks configuration.
Commit the file to the repository and merge it into the default branch. Your hooks will now run during agent sessions.
Troubleshooting
If you run into problems using hooks, use the following table to troubleshoot.
| Issue |
Action |
| Hooks are not executing |
Verify the JSON file is in the .github/hooks/ directory.Check for valid JSON syntax (for example, jq . hooks.json).Ensure version: 1 is specified in your hooks.json file.Verify the script you are calling from your hook is executable (chmod +x script.sh)Check that the script has a proper shebang (for example, #!/bin/bash) |
| Hooks are timing out |
The default timeout is 30 seconds. Increase timeoutSec in the configuration if needed.Optimize script performance by avoiding unnecessary operations. |
| Invalid JSON output |
Ensure the output is on a single line.On Unix, use jq -c to compact and validate the JSON output.On Windows, use the ConvertTo-Json -Compress command in PowerShell to do the same. |
Debugging
You can debug hooks using the following methods:
Enable verbose logging in the script to inspect the input data and trace script execution.
#!/bin/bash
set -x # Enable bash debug mode
INPUT=$(cat)
echo "DEBUG: Received input" >&2
echo "$INPUT" >&2
# ... rest of script
Test hooks locally by piping test input into your hook to validate its behavior:
# Create test input
echo '{"timestamp":1704614400000,"cwd":"/tmp","toolName":"bash","toolArgs":"{\"command\":\"ls\"}"}' | ./my-hook.sh
# Check exit code
echo $?
# Validate output is valid JSON
./my-hook.sh | jq .
Further reading
1---2name: using-hooks-with-github-copilot-agents3description: Extend and customize GitHub Copilot agent behavior by executing custom shell commands at key points during agent execution.4---5# Using hooks with GitHub Copilot agents67Extend and customize GitHub Copilot agent behavior by executing custom shell commands at key points during agent execution.89Hooks allow you to extend and customize the behavior of GitHub Copilot agents by executing custom shell commands at key points during agent execution. For a conceptual overview of hooks—including details of the available hook triggers—see [About hooks](/en/copilot/concepts/agents/coding-agent/about-hooks).1011## Creating a hook in a repository on GitHub12131. Create a new `hooks.json` file with the name of your choice in the `.github/hooks/` folder of your repository. The hooks configuration file **must be present** on your repository's default branch to be used by Copilot coding agent. For GitHub Copilot CLI, hooks are loaded from your current working directory.14152. In your text editor, copy and paste the following hook template. Remove any hooks you don't plan on using from the `hooks` array.1617 ```json copy18 {19 "version": 1,20 "hooks": {21 "sessionStart": [...],22 "sessionEnd": [...],23 "userPromptSubmitted": [...],24 "preToolUse": [...],25 "postToolUse": [...],26 "errorOccurred": [...]27 }28 }29 ```30313. Configure your hook syntax under the `bash` or `powershell` keys, or directly reference script files you have created.3233 * This example runs a script that outputs the start date of the session to a log file using the `sessionStart` hook:3435 ```json copy36 "sessionStart": [37 {38 "type": "command",39 "bash": "echo \"Session started: $(date)\" >> logs/session.log",40 "powershell": "Add-Content -Path logs/session.log -Value \"Session started: $(Get-Date)\"",41 "cwd": ".",42 "timeoutSec": 1043 }44 ],45 ```4647 * This example calls out to an external `log-prompt` script:4849 ```json copy50 "userPromptSubmitted": [51 {52 "type": "command",53 "bash": "./scripts/log-prompt.sh",54 "powershell": "./scripts/log-prompt.ps1",55 "cwd": "scripts",56 "env": {57 "LOG_LEVEL": "INFO"58 }59 }60 ],61 ```6263 For a full reference on the input JSON from agent sessions along with sample scripts, see [Hooks configuration](/en/copilot/reference/hooks-configuration).64654. Commit the file to the repository and merge it into the default branch. Your hooks will now run during agent sessions.6667## Troubleshooting6869If you run into problems using hooks, use the following table to troubleshoot.7071| Issue | Action |72| ----------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |73| Hooks are not executing | <ul><li>Verify the JSON file is in the `.github/hooks/` directory.</li><li>Check for valid JSON syntax (for example, `jq . hooks.json`).</li><li>Ensure `version: 1` is specified in your `hooks.json` file.</li><li>Verify the script you are calling from your hook is executable (`chmod +x script.sh`)</li><li>Check that the script has a proper shebang (for example, `#!/bin/bash`)</li></ul> |74| Hooks are timing out | <ul><li>The default timeout is 30 seconds. Increase `timeoutSec` in the configuration if needed.</li><li>Optimize script performance by avoiding unnecessary operations.</li></ul> |75| Invalid JSON output | <ul><li>Ensure the output is on a single line.</li><li>On Unix, use `jq -c` to compact and validate the JSON output.</li><li>On Windows, use the `ConvertTo-Json -Compress` command in PowerShell to do the same.</li></ul> |7677## Debugging7879You can debug hooks using the following methods:8081* **Enable verbose logging** in the script to inspect the input data and trace script execution.8283 ```shell copy84 #!/bin/bash85 set -x # Enable bash debug mode86 INPUT=$(cat)87 echo "DEBUG: Received input" >&288 echo "$INPUT" >&289 # ... rest of script90 ```9192* **Test hooks locally** by piping test input into your hook to validate its behavior:9394 ```shell copy95 # Create test input96 echo '{"timestamp":1704614400000,"cwd":"/tmp","toolName":"bash","toolArgs":"{\"command\":\"ls\"}"}' | ./my-hook.sh9798 # Check exit code99 echo $?100101 # Validate output is valid JSON102 ./my-hook.sh | jq .103 ```104105## Further reading106107* For more information about configuring hooks, see [Hooks configuration](/en/copilot/reference/hooks-configuration)108* For more information about Copilot coding agent, see [About GitHub Copilot coding agent](/en/copilot/concepts/agents/coding-agent/about-coding-agent)109* For more information about GitHub Copilot CLI, see [About GitHub Copilot CLI](/en/copilot/concepts/agents/about-copilot-cli)110* For information about customizing the agent environment, see [Customizing the development environment for GitHub Copilot coding agent](/en/copilot/how-tos/use-copilot-agents/coding-agent/customize-the-agent-environment)