All Toolsโ€บAI Code Optimizer
๐Ÿ”ง AI-Powered Code ToolsMarch 31, 2026โœ… Tests passing

AI Code Optimizer

A CLI tool that uses GPT and Claude to analyze Python scripts and suggest optimizations for performance, readability, or maintainability. Developers working with AI pipelines or large codebases can use this to automate code reviews.

What It Does

  • Analyze Python scripts for performance, readability, and maintainability improvements.
  • Get suggestions from both GPT and Claude models.
  • Easy-to-use command-line interface.

Installation

  • Python 3.7+
  • openai Python package
  • anthropic Python package
  • click Python package
  • pytest for testing

Usage

Run the CLI tool with the following command:

python ai_code_optimizer.py --file <path_to_python_script> --gpt-key <your_openai_api_key> --claude-key <your_anthropic_api_key>

Example

python ai_code_optimizer.py --file example.py --gpt-key sk-12345 --claude-key sk-67890

Source Code

import os
import click
import openai
import anthropic

def analyze_code_with_gpt(code, api_key):
    """Analyze code using OpenAI's GPT model."""
    openai.api_key = api_key
    try:
        response = openai.Completion.create(
            engine="text-davinci-003",
            prompt=f"Analyze the following Python code for performance, readability, and maintainability improvements:\n\n{code}\n\nProvide detailed suggestions:",
            max_tokens=500
        )
        return response.choices[0].text.strip()
    except openai.error.OpenAIError as e:
        return f"Error analyzing code with GPT: {e}"

def analyze_code_with_claude(code, api_key):
    """Analyze code using Anthropic's Claude model."""
    client = anthropic.Client(api_key)
    try:
        response = client.completion(
            prompt=f"\n\nHuman: Analyze the following Python code for performance, readability, and maintainability improvements:\n\n{code}\n\nProvide detailed suggestions.\n\nAssistant:",
            model="claude-v1",
            max_tokens_to_sample=500
        )
        return response["completion"].strip()
    except Exception as e:
        return f"Error analyzing code with Claude: {e}"

def annotate_code(file_path, gpt_key, claude_key):
    """Read a Python script, analyze it with GPT and Claude, and return annotated suggestions."""
    if not os.path.exists(file_path):
        raise FileNotFoundError(f"File not found: {file_path}")

    with open(file_path, "r") as f:
        code = f.read()

    if not code.strip():
        return "The file is empty. No code to analyze."

    gpt_suggestions = analyze_code_with_gpt(code, gpt_key)
    claude_suggestions = analyze_code_with_claude(code, claude_key)

    return f"GPT Suggestions:\n{gpt_suggestions}\n\nClaude Suggestions:\n{claude_suggestions}"

@click.command()
@click.option('--file', 'file_path', required=True, type=click.Path(exists=True), help='Path to the Python script file to analyze.')
@click.option('--gpt-key', required=True, help='OpenAI API key for GPT.')
@click.option('--claude-key', required=True, help='Anthropic API key for Claude.')
def main(file_path, gpt_key, claude_key):
    """AI Code Optimizer CLI entry point."""
    try:
        result = annotate_code(file_path, gpt_key, claude_key)
        click.echo(result)
    except Exception as e:
        click.echo(f"Error: {e}")

if __name__ == "__main__":
    main()

Community

Downloads

ยทยทยท

Rate this tool

No ratings yet โ€” be the first!

Details

Tool Name
ai_code_optimizer
Category
AI-Powered Code Tools
Generated
March 31, 2026
Tests
Passing โœ…
Fix Loops
3

Quick Install

Clone just this tool:

git clone --depth 1 --filter=blob:none --sparse \
  https://github.com/ptulin/autoaiforge.git
cd autoaiforge
git sparse-checkout set generated_tools/2026-03-31/ai_code_optimizer
cd generated_tools/2026-03-31/ai_code_optimizer
pip install -r requirements.txt 2>/dev/null || true
python ai_code_optimizer.py
AI Code Optimizer โ€” AI Tools by AutoAIForge