Skip to content

varienos/cc-openai-prompt-rewriter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

OpenAI Prompt Rewriter Hook for Claude Code

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.

Features

  • 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 UserPromptSubmit hook within the Claude Code environment.

Prerequisites

  • Python 3.10+
  • OpenAI API Key

Installation

  1. Clone the repository:

    git clone <repository-url>
    cd cc-openai-prompt-rewriter
  2. Create and activate a virtual environment:

    python3 -m venv .venv
    source .venv/bin/activate
  3. Install dependencies:

    pip install -r requirements.txt
  4. Configure Environment Variables: Create a .env file in the project root and add your OpenAI API key:

    echo "OPENAI_API_KEY=sk-your-api-key-here" > .env

Configuration

Hook Registration

The hook is located at .claude/hooks/cc-openai-prompt-rewriter.py. To register it with Claude Code:

  1. Copy the example settings file:

    cp .claude/settings.local.json.example .claude/settings.local.json
  2. 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.

Changing the Model

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=[...],
    # ...
)

Usage

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.

Manual Testing

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.py

Logging

Logs 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.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Releases

No releases published

Packages

No packages published

Languages