What You Can Build
- Stop operations before they cause problems (like creating
.jsfiles in a TypeScript project) - Run linters or custom validators before files get saved
- Prevent operations that violate security policies
- Track everything for analytics or compliance
- Trigger external tools or services at the right moments
- Add context to the conversation based on what Cline is doing
Hook Types
Cline supports 8 hook types that run at different points in the task lifecycle:| Hook Type | When It Runs |
|---|---|
| TaskStart | When you start a new task |
| TaskResume | When you resume an interrupted task |
| TaskCancel | When you cancel a running task |
| TaskComplete | When a task finishes successfully |
| PreToolUse | Before Cline executes a tool (read_file, write_to_file, etc.) |
| PostToolUse | After a tool execution completes |
| UserPromptSubmit | When you submit a message to Cline |
| PreCompact | Before Cline truncates conversation history to free up context |
Hook Lifecycle
The diagram shows the complete hook lifecycle:- Entry: When you start a task, either TaskStart (new task) or TaskResume (interrupted task) runs first
- Conversation Cycle: Each time you send a message, UserPromptSubmit runs, then Cline processes your request
- Tool Execution: When Cline decides to use a tool, PreToolUse runs first-if allowed, the tool executes, then PostToolUse runs
- Context Management: If the conversation approaches context limits, PreCompact runs before truncation
- Exit: The task ends with either TaskComplete (success) or TaskCancel (user cancellation)
Hook Locations
Hooks can be stored globally or in a project workspace. See Storage Locations for guidance on when to use each.- Global hooks:
~/Documents/Cline/Hooks/ - Project hooks:
.clinerules/hooks/in your repo (can be committed to version control)
cancel: true, the operation stops.
Creating a Hook
Open the Hooks tab
Click the scale icon at the bottom of the Cline panel, to the left of the model selector. Switch to the Hooks tab.
Review the hook's code
Click the pencil icon to open and edit the hook script. Cline generates a template with examples.
Quick Start: Your First Hook
Let’s create a simple hook that logs every file Cline reads or writes. You’ll see results in seconds.The Hook
Create a file calledfile-logger in your hooks directory with this content:
Setup
Create the hook file
Save the script above as
~/Documents/Cline/Hooks/file-logger (macOS/Linux) or create it through the Hooks UI.Test It
Ask Cline to read any file in your project: “What’s in package.json?” Then check the log:Customize It
Try modifying the hook to:- Filter specific file types (only log
.tsfiles) - Add the task ID to each log entry
- Send notifications for write operations
- Block operations on certain paths
How Hooks Work
Hooks are executable scripts that receive JSON input via stdin and return JSON output via stdout.Input Structure
Every hook receives a JSON object with common fields plus hook-specific data:taskStart,taskResume,taskCancel,taskCompletecontain{ task: string }preToolUsecontains{ tool: string, parameters: object }postToolUsecontains{ tool: string, parameters: object, result: string, success: boolean, durationMs: number }userPromptSubmitcontains{ prompt: string }preCompactcontains{ conversationLength: number, estimatedTokens: number }
Output Structure
Hooks return a JSON object to stdout:| Field | Type | Description |
|---|---|---|
cancel | boolean | If true, stops the operation (blocks the tool, cancels the task start, etc.) |
contextModification | string | Optional text that gets injected into the conversation as context for Cline |
errorMessage | string | Shown to the user if cancel is true |
Context Modification
ThecontextModification field lets hooks inject information into the conversation. This is useful for:
- Adding project-specific context when a task starts
- Providing validation results that Cline should consider
- Injecting environment information before tool execution
"Note: This file is auto-generated. Edits may be overwritten."
Hook Reference
Task Lifecycle Hooks
TaskStart
Runs when you start a new task. Use it to:- Log task start time for analytics
- Add project context to the conversation
- Check prerequisites before work begins
- Notify external systems (Slack, issue trackers)
TaskResume
Runs when you resume an interrupted task (instead of TaskStart). Use it to:- Check for changes since the task was paused
- Refresh context with latest project state
- Notify that work is resuming
TaskCancel
Runs when you cancel a running task. Use it to:- Clean up temporary files or resources
- Notify external systems about cancellation
- Log cancellation for analytics
TaskComplete
Runs when a task completes successfully. Use it to:- Run tests or validation after changes
- Generate reports or summaries
- Notify stakeholders
- Trigger CI/CD pipelines
Tool Hooks
PreToolUse
Runs before any tool executes. This is the most powerful hook for validation and safety. Use it to:- Block dangerous operations
- Validate parameters before execution
- Add context about the file or resource being accessed
- Log tool usage
.js files in a TypeScript project:
PostToolUse
Runs after a tool completes (success or failure). Use it to:- Audit tool usage
- Validate results
- Trigger follow-up actions
- Monitor performance
PostToolUse hooks can return
cancel: true to stop the task, but they cannot undo the tool execution that already happened.Other Hooks
UserPromptSubmit
Runs when you send a message to Cline. Use it to:- Log prompts for analytics
- Add context based on prompt content
- Validate or sanitize prompts
PreCompact
Runs before Cline truncates conversation history to stay within context limits. Use it to:- Archive important conversation parts before they’re removed
- Log compaction events
- Add a summary of what’s being removed
Examples
TypeScript Enforcement
Block creation of.js files in a TypeScript project:
Tool Usage Logging
Log all tool executions to a file:Add Project Context on Task Start
Inject project-specific information when a task begins:CLI Support
Hooks are available in the Cline CLI:CLI hooks are only supported on macOS and Linux.
Troubleshooting
Hook not running?- Check that the file is executable (
chmod +x hookname) - Verify the hook is enabled (toggle is on in the Hooks tab)
- Check that Hooks are enabled globally in Settings
- Ensure output is valid JSON on a single line to stdout
- Use stderr (
>&2) for debug logging, not stdout - Check for trailing characters or newlines before the JSON
- Review the hook’s logic and test with sample input
- Check both global and workspace hooks (both run if they exist)
Related Features
- Rules define high-level guidance that hooks can enforce programmatically
- Checkpoints let you roll back if a hook didn’t catch an issue
- Auto-Approve works well with hooks as safety nets

