Aider

0.86.2 · active · verified Sun Apr 12

Aider is an AI pair programming tool that allows you to chat with an AI model directly in your terminal, making it easy to edit code in your local git repository. It integrates with various LLM providers (OpenAI, Anthropic, Gemini, OpenRouter, etc.) and supports many programming languages. The current version is 0.86.2, with active development and frequent releases focusing on new model support and user experience improvements.

Warnings

Install

Quickstart

This quickstart demonstrates how to set up a dummy project with a `main.py` file and a git repository, then launch Aider via the command line. Aider operates interactively in your terminal, acting as an AI pair programmer. You'll need to set an API key for your chosen LLM provider (e.g., OpenAI, Anthropic, Gemini) as an environment variable or pass it via CLI arguments. The example uses a `subprocess.run` call to execute Aider, allowing for direct terminal interaction.

# 1. Set your API key (e.g., OpenAI, Anthropic, Gemini, OpenRouter)
# For OpenAI, set OPENAI_API_KEY. For other providers, check Aider docs.
# export OPENAI_API_KEY="sk-..." (Linux/macOS)
# $env:OPENAI_API_KEY="sk-..." (PowerShell)

import os
import subprocess
import shutil

# Using a dummy key for demonstration; replace with your actual key
openai_api_key = os.environ.get('OPENAI_API_KEY', 'sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')

if not openai_api_key.startswith('sk-'):
    print("WARNING: OPENAI_API_KEY not set or invalid. Aider will likely fail without a valid key.")

# Create a dummy project directory and a file to edit
project_dir = "aider_quickstart_project"
if os.path.exists(project_dir):
    shutil.rmtree(project_dir) # Clean up previous run
os.makedirs(project_dir)

os.chdir(project_dir)

with open("main.py", "w") as f:
    f.write("def hello_world():\n    print('Hello')\n")

# Initialize a git repo (Aider works best with git)
subprocess.run(["git", "init", "-b", "main"], check=True, capture_output=True)
subprocess.run(["git", "add", "main.py"], check=True, capture_output=True)
subprocess.run(["git", "commit", "-m", "Initial commit"], check=True, capture_output=True)

print(f"\nCreated '{project_dir}/main.py' and initialized git. Now launching Aider...\n")
print("To interact, type your requests into the terminal. E.g., 'Add 'world!' to the print statement.'")
print("Type '/help' for Aider commands, or '/exit' to quit.\n")

# Run Aider. The user will interact with it in the terminal.
# Use a model alias like 'gpt-4o' if OPENAI_API_KEY is set.
# Set environment variables for the subprocess call.
# Note: This will block until Aider exits.
result = subprocess.run(
    ["aider", "--model", "gpt-4o", "main.py"],
    env={**os.environ, 'OPENAI_API_KEY': openai_api_key},
    check=False # Aider might exit with non-zero if user cancels, etc.
)

print(f"\nAider process exited with code {result.returncode}.")
print(f"Check '{os.path.join(project_dir, 'main.py')}' for changes.")

# Clean up (optional)
os.chdir("..")
shutil.rmtree(project_dir)
print(f"Cleaned up '{project_dir}'.")

view raw JSON →