Complete Aider Tutorial: AI Pair Programming in Your Terminal

# Tutorial Lengkap Aider: AI Pair Programming di Terminal Aider adalah tool open-source yang memungkinkan Anda melakukan pair programming dengan Large Language Model (LLM) langsung dari terminal. Den...

By Ruby Abdullah · · tutorial
AiderAI CodingPair ProgrammingLLMDeveloper Tools

Complete Aider Tutorial: AI Pair Programming in Your Terminal

Aider is an open-source tool that lets you pair program with Large Language Models (LLMs) directly from your terminal. With Aider, you can edit code in your Git repository through natural language conversations, without needing to copy-paste code into a web interface. The tool supports various AI models including GPT-4o, Claude, Gemini, and local models through Ollama.

Why Aider?

In the increasingly crowded AI coding assistant ecosystem, Aider stands out for several reasons:

  • Git-native: Every change is automatically committed to Git with descriptive commit messages
  • Multi-file editing: Edit multiple files simultaneously in a single conversation
  • Repository map: Aider understands your entire codebase structure using tree-sitter
  • Model agnostic: Supports OpenAI, Anthropic, Google, and local models
  • Terminal-first: Works directly in your terminal without requiring a specific IDE
  • Installation

    Using pip

    pip install aider-chat
    

    pipx installs Aider in an isolated environment so it doesn't conflict with your other Python packages:

    pipx install aider-chat
    

    Using Homebrew (macOS)

    brew install aider
    

    Verify Installation

    aider --version
    

    Configuring API Keys

    Aider needs an API key from the LLM provider you want to use. Here's how to configure them:

    OpenAI

    export OPENAIAPIKEY=sk-xxxxxxxxxxxxxxxxxxxxxxxx
    

    Anthropic (Claude)

    export ANTHROPICAPIKEY=sk-ant-xxxxxxxxxxxxxxxxxxxxxxxx
    

    Google Gemini

    export GEMINIAPIKEY=xxxxxxxxxxxxxxxxxxxxxxxx
    

    Using a .env File

    You can also store API keys in a .env file at the root of your project:

    # .env
    

    OPENAIAPIKEY=sk-xxxxxxxxxxxxxxxxxxxxxxxx

    ANTHROPICAPIKEY=sk-ant-xxxxxxxxxxxxxxxxxxxxxxxx

    Aider will automatically read the .env file when launched.

    Basic Usage

    Starting an Aider Session

    Navigate to your Git repository and run:

    cd /path/to/your/project
    

    aider

    By default, Aider will use the best available model based on your configured API keys.

    Specifying a Model

    # Using Claude Sonnet
    

    aider --model claude-sonnet-4-6

    Using GPT-4o

    aider --model gpt-4o

    Using a local model via Ollama

    aider --model ollama/deepseek-coder-v2

    Adding Files to the Conversation

    When starting Aider, you can specify files to edit directly:

    aider src/main.py src/utils.py tests/testmain.py
    

    Or add files from within a session using the /add command:

    /add src/models.py
    

    Simple Conversation Example

    $ aider app.py
    
    
    Add a GET /health endpoint that returns status "ok" and the current timestamp

    app.py

    <<<<<<< SEARCH

    from flask import Flask

    app = Flask(name)

    =======

    from flask import Flask, jsonify

    from datetime import datetime

    app = Flask(name)

    @app.route('/health')

    def healthcheck():

    return jsonify({

    'status': 'ok',

    'timestamp': datetime.utcnow().isoformat()

    })

    >>>>>> REPLACE

    Commit: feat: add health check endpoint with status and timestamp

    Aider shows the proposed changes and creates a commit immediately if you approve.

    In-Chat Commands

    Aider provides many commands you can use within a chat session:

    File Management

    /add        # Add a file to the conversation (editable)
    

    /read # Add a file as read-only (for context)

    /drop # Remove a file from the conversation

    /ls # Show currently active files

    Git Control

    /commit           # Commit pending changes
    

    /undo # Undo the last commit made by Aider

    /diff # Show the diff from the last change

    /git # Run a git command directly

    /clear            # Clear chat history
    

    /run # Run a shell command and show output

    /test # Run tests and ask Aider to fix failures

    Advanced Usage

    Multi-File Editing

    One of Aider's greatest strengths is its ability to edit multiple files simultaneously. This is particularly useful for refactoring:

    aider src/models/user.py src/routes/auth.py src/services/userservice.py tests/testauth.py
    

    > Refactor user authentication: move password validation logic from the route 
    
    to userservice.py, add password hashing using bcrypt, and update the tests

    Aider will analyze all added files and make coordinated changes across all relevant files.

    Using the Repository Map

    Aider automatically creates a map of your repository using tree-sitter. This allows Aider to understand relationships between files even when not all files are added to the conversation:

    # Aider will analyze the entire repository for context
    

    aider --map-tokens 2048 src/main.py

    The --map-tokens parameter controls how many tokens are used for the repository map. Higher values provide more context but consume more tokens.

    Architect Mode

    Architect mode uses two models: one for planning changes (architect) and another for executing them (editor):

    aider --architect --model claude-sonnet-4-6 --editor-model claude-haiku-4-5-20251001
    

    This is useful when you want a more powerful model to plan changes and a cheaper model to implement them.

    Integration with Ollama (Local Models)

    To use local models without sending code to the cloud:

    # Make sure Ollama is installed and the model is downloaded
    

    ollama pull deepseek-coder-v2

    Run Aider with the Ollama model

    aider --model ollama/deepseek-coder-v2

    Lint and Test Integration

    Aider can automatically run linters and tests after every change:

    aider --lint-cmd "ruff check --fix" --auto-lint \
    

    --test-cmd "pytest tests/" --auto-test

    If lint or tests fail, Aider will automatically attempt to fix the issues.

    Using .aider.conf.yml

    For consistent configuration across sessions, create a .aider.conf.yml file at the project root:

    model: claude-sonnet-4-6
    

    auto-commits: true

    auto-lint: true

    lint-cmd: "ruff check --fix"

    auto-test: false

    test-cmd: "pytest tests/ -x"

    map-tokens: 2048

    Practical Workflows

    1. Bug Fixing with Context

    # Add the problematic file and the test file
    

    aider src/parser.py tests/testparser.py

    There's a bug in the parsecsv function: when there are empty columns at
    the end of a row, the function drops them. Empty columns should be preserved
    as empty strings. Fix this bug and add a test case for this scenario.

    2. Adding New Features

    aider src/api/ src/models/ src/services/
    
    
    Add pagination to the GET /api/users endpoint. Use cursor-based pagination
    with "cursor" and "limit" parameters. Default limit 20, maximum 100.
    Response should include "nextcursor" and "hasmore".

    3. Code Review and Refactoring

    # Add files as read-only for context
    

    aider --read src/legacymodule.py src/newmodule.py

    Review src/newmodule.py and compare it with legacymodule.py. Make sure
    all edge cases from the legacy module are handled in the new module.

    4. Writing Documentation

    aider src/api/routes.py README.md
    
    
    Create API documentation in README.md based on all endpoints in routes.py.
    Include example requests and responses for each endpoint.

    Tips and Best Practices

    1. Start with a Small Scope

    Don't add too many files at once. Start with files that actually need to be edited. Use /read for files that only need to provide context:

    /add src/targetfile.py
    

    /read src/referencefile.py

    2. Give Specific Instructions

    The more specific your instructions, the better the results:

    # Less effective
    
    Fix the login function

    More effective

    In the login() function at line 45, add rate limiting: maximum 5 attempts
    per IP address within 15 minutes. Use Redis to store the counter.
    Return HTTP 429 if the limit is reached.

    3. Use /undo to Iterate

    If changes don't meet your expectations, use /undo to revert and try different instructions:

    /undo
    
    Try a different approach: use a @ratelimit(max=5, window=900) decorator
    instead of inline logic

    4. Use /run to Verify

    After Aider makes changes, verify the results:

    /run python -m pytest tests/testauth.py -v
    

    /run python src/main.py --dry-run

    5. Configure Git Conventions

    Aider creates automatic commits. You can customize the commit message format:

    aider --commit-prompt "Use conventional commit format (feat:, fix:, refactor:, etc). Write in English."
    

    6. Save Tokens with Read-Only Files

    Files added with /read use fewer tokens than /add because Aider doesn't need to process edit capabilities for those files:

    /read src/config.py        # Context only
    

    /read src/constants.py # Context only

    /add src/main.py # File to be edited

    Comparing Edit Formats

    Aider supports several edit formats that can be chosen based on your needs:

    | Format | Description | Best For |

    |--------|-------------|----------|

    | whole | Resends the entire file | Small files, large changes |

    | diff | Uses unified diff | Large files, small changes |

    | diff-fenced | Diff in fenced code blocks | Default for many models |

    | udiff | Universal diff format | Models that support it |

    To change the edit format:

    aider --edit-format diff
    

    Using Aider with Docker

    For an isolated environment:

    docker run -it --rm \
    

    -v $(pwd):/app \

    -e OPENAIAPIKEY=$OPENAIAPI_KEY \

    paulgauthier/aider \

    --model gpt-4o

    Troubleshooting

    Model Not Responding Well

    If the model produces poor output, try:

  • Use a more capable model (claude-sonnet or gpt-4o)
  • Reduce the number of added files
  • Provide more specific instructions
  • Use architect mode for complex tasks
  • Git Conflicts

    If conflicts occur when Aider creates commits:

    /git stash
    

    /clear

    Start a new conversation with clearer context

    Token Limits

    If the conversation gets too long:

    /clear           # Reset chat history
    

    /tokens # Check current token usage

    /drop # Remove unnecessary files

    Conclusion

    Aider is a powerful tool for AI-assisted coding directly from the terminal. Its key advantages are native Git integration, multi-file editing capabilities, and support for various AI models. Key takeaways:

    • Always start with a small, specific scope
    • Use /read to provide context without adding token costs
    • Use /undo liberally to iterate
    • Configure .aider.conf.yml for consistency across your team
    • Consider architect mode for large-scale refactoring

    With the right approach, Aider can be a significant productivity multiplier in your development workflow. It's not a developer replacement, but a partner that accelerates iteration and reduces boilerplate work.

    Related Articles

    MergeKit Tutorial: Merging LLM Models to Create Custom AI Models

    Tutorial MergeKit: Menggabungkan Model LLM untuk Membuat Model AI Kustom Model bahasa besar (LLM) open-source terus berk...

    bitsandbytes Tutorial: 4-bit and 8-bit Quantization for Large Language Models

    Tutorial bitsandbytes: Kuantisasi 4-bit dan 8-bit untuk Large Language Models Menjalankan Large Language Model (LLM) sep...

    Semantic Router Tutorial: Smart Routing for LLM Applications

    Tutorial Semantic Router: Routing Cerdas untuk Aplikasi LLM Semantic Router adalah library Python yang memungkinkan Anda...

    Complete Unstructured Tutorial: Document Preprocessing for RAG and LLM Applications

    Tutorial Lengkap Unstructured: Preprocessing Dokumen untuk RAG dan Aplikasi LLM Dalam membangun aplikasi Retrieval-Augme...