This project implements a custom hook for Claude Code that intercepts user prompts and rewrites them using OpenAI's GPT-4.1-mini model. It is designed to transform unstructured, emotional, or unclear user input into clear, technical, and actionable instructions for the AI agent.
- Prompt Rewriting: Uses OpenAI's API to analyze and rewrite user prompts.
- Sanitization: Removes emotional language and profanity, focusing solely on the technical intent.
- Logging: Maintains a local JSONL log of all original and rewritten prompts for auditing and debugging purposes.
- Seamless Integration: Designed to work as a
UserPromptSubmithook within the Claude Code environment.
- Python 3.10+
- OpenAI API Key
-
Clone the repository:
git clone <repository-url> cd cc-openai-prompt-rewriter
-
Create and activate a virtual environment:
python3 -m venv .venv source .venv/bin/activate -
Install dependencies:
pip install -r requirements.txt
-
Configure Environment Variables: Create a
.envfile in the project root and add your OpenAI API key:echo "OPENAI_API_KEY=sk-your-api-key-here" > .env
The hook is located at .claude/hooks/cc-openai-prompt-rewriter.py. To register it with Claude Code:
-
Copy the example settings file:
cp .claude/settings.local.json.example .claude/settings.local.json
-
Make the hook executable:
chmod +x .claude/hooks/cc-openai-prompt-rewriter.py
The settings.local.json file configures Claude Code to run this hook on every prompt submission. This file is gitignored to allow personal customization.
The script uses gpt-4.1-mini by default. To change the model (e.g., to gpt-4 or gpt-3.5-turbo), edit the model parameter in .claude/hooks/cc-openai-prompt-rewriter.py:
completion = client.chat.completions.create(
model="gpt-4", # <--- Change this value
messages=[...],
# ...
)Once configured, the hook runs automatically whenever you submit a prompt in Claude Code. It intercepts the input, processes it via OpenAI, and passes the rewritten prompt to Claude Code.
You can test the hook manually by piping JSON input to it:
echo '{"prompt": "fix this broken code immediately!"}' | python .claude/hooks/cc-openai-prompt-rewriter.pyLogs are stored in ~/.claude/logs/ in JSONL format. Each entry contains:
- Timestamp
- Session ID
- Original Input
- Rewritten Output
Note: Logs are excluded from version control via .gitignore to protect sensitive data.
This project is licensed under the MIT License - see the LICENSE file for details.