TechSetupGuides
Intermediateclirustllmprompt-engineeringaicode-analysisdev-tools

Code2Prompt: Convert codebases into LLM prompts

CLI tool to convert your entire codebase into a single formatted LLM prompt with source tree, templating, and token counting.

  1. Step 1

    What is Code2Prompt?

    Code2Prompt is a powerful context engineering tool designed to ingest codebases and format them for Large Language Models (LLMs). Whether you are manually copying context for ChatGPT, building AI agents via Python, or running an MCP server, Code2Prompt streamlines the context preparation process by converting your entire codebase into a well-structured prompt.

    It features a Terminal User Interface (TUI) for interactive configuration, smart file filtering, flexible Handlebars templating, and token tracking to stay within LLM context limits. Built in Rust for blazing-fast performance.

  2. Step 2

    Technology stack

    Code2Prompt is built with a robust ecosystem architecture:

    Core:

    • Rust (51.8% of codebase)
    • High-performance file traversal with .gitignore awareness
    • Token counting integrated via tiktoken-rs

    CLI & TUI:

    • clap for CLI argument parsing
    • ratatui and crossterm for terminal UI
    • inquery for interactive prompts
    • indicatif for progress bars

    Template Engine:

    • Handlebars (hbs) templating
    • Fully customizable prompt templates

    Python Integration:

    • PyO3 for Python bindings
    • Available as code2prompt-rs on PyPI

    Key Dependencies:

    • git2 with vendored libgit2 for Git operations
    • ignore for .gitignore parsing
    • serde/serde_json for data serialization
    • tokio for async runtime
    • rayon for parallel processing
    • arboard for clipboard integration

    Other languages:

    • MDX (39.2%) - Documentation website
    • Astro (3.7%) - Static site generation
    • Handlebars (2.1%) - Templates
    • Python (2.0%) - Bindings
    Language Distribution:
    ├── Rust: 51.8%
    ├── MDX: 39.2%
    ├── Astro: 3.7%
    ├── Handlebars: 2.1%
    ├── Python: 2.0%
    └── JavaScript: 0.6%
    
    Key Rust Crates:
    ├── clap 4.5 (CLI parsing)
    ├── ratatui 0.29 (TUI)
    ├── crossterm (terminal I/O)
    ├── inquery 0.9 (interactive prompts)
    ├── handlebars 6.4 (templating)
    ├── git2 0.20 (Git operations)
    ├── tiktoken-rs 0.9 (token counting)
    ├── ignore 0.4 (.gitignore)
    ├── arboard 3.6 (clipboard)
    ├── tokio 1.49 (async runtime)
    ├── rayon (parallel processing)
    └── pyo3 0.27 (Python bindings)
  3. Step 3

    Prerequisites

    Before installing Code2Prompt, ensure you have one of the following:

    For Cargo installation:

    • Rust toolchain installed (rustup recommended)
    • Cargo package manager

    For pip installation:

    • Python 3.12 or higher
    • pip package manager

    For source build:

    • Git
    • Rust and Cargo
    • CMake (for vendored dependencies)
    # Check Rust installation
    rustc --version
    cargo --version
    
    # Check Python installation (for SDK)
    python --version  # Should be 3.12+
    pip --version
    
    # Check Git (for source build)
    git --version
  4. Step 4

    Quick install

    The fastest way to get Code2Prompt is using one of these package managers:

    Cargo (Rust): Installs the CLI tool globally on your system.

    Homebrew (macOS/Linux): Cross-platform package manager installation.

    pip (Python SDK): Installs the Python bindings for programmatic use.

    # Using Cargo (recommended for CLI)
    cargo install code2prompt
    
    # For Wayland support (Linux with Wayland display server)
    cargo install --features wayland code2prompt
    
    # Using Homebrew (macOS/Linux)
    brew install code2prompt
    
    # Using pip (Python SDK for programmatic access)
    pip install code2prompt-rs
  5. Step 5

    Quick start

    Once installed, generating a prompt from your codebase is as simple as pointing the tool to your directory.

    Basic usage copies the generated prompt to your clipboard. Save to file outputs the prompt to a specified file for later use.

    # Basic usage: Generate prompt from current directory and copy to clipboard
    code2prompt .
    
    # Save prompt to file
    code2prompt path/to/project --output-file prompt.txt
    
    # With custom output
    code2prompt ./my-project --output-file my-code-context.txt
  6. Step 6

    Configuration file

    Code2Prompt uses a .c2pconfig file for persistent configuration. This JSON/TOML file allows you to customize:

    • Include/exclude patterns
    • Template selection
    • Token limit settings
    • Output formatting preferences
    • Git integration options
    {
      "include": ["*.rs", "*.toml"],
      "exclude": ["target/", "node_modules/", "*.lock"],
      "max_tokens": 100000,
      "template": "default",
      "git_context": {
        "include_diff": true,
        "include_log": false
      },
      "output": {
        "clipboard": false,
        "file": "prompt.txt"
      }
    }
  7. Step 7

    Terminal User Interface

    Code2Prompt features an interactive TUI that allows you to:

    • Browse your codebase structure
    • Selectively include/exclude directories
    • Preview generated prompts
    • Adjust token limits in real-time
    • Customize templates on-the-fly
    # Launch TUI mode
    code2prompt . --tui
    
    # In TUI mode:
    # - Use arrow keys to navigate
    # - Press space to toggle file/directory inclusion
    # - Press 'c' to copy to clipboard
    # - Press 'o' to output to file
    # - Press 'q' to quit
  8. Step 8

    Advanced filtering

    Use glob patterns and .gitignore rules to precisely control which files are included in your prompt:

    Include patterns specify file types to include. Exclude patterns filter out unwanted directories or files. The tool automatically respects your project's .gitignore file.

    # Include only specific file types
    code2prompt . --include "*.py" --include "*.md" --include "*.json"
    
    # Exclude directories
    code2prompt . --exclude "node_modules/" --exclude "__pycache__/" --exclude ".git/"
    
    # Combined patterns
    code2prompt . \
      --include "*.js,*.ts,*.tsx" \
      --exclude "dist/,build/,*.test.ts"
    
    # Using .gitignore (automatic)
    code2prompt .  # Respects existing .gitignore
  9. Step 9

    Template customization

    Code2Prompt uses Handlebars templates to format your prompts. Customize the template to match your LLM's requirements.

    Built-in templates:

    • default: Standard code presentation
    • tree: Shows file structure hierarchy
    • minimal: Stripped-down format for compact prompts

    Custom templates can be created with Handlebars syntax. Note: Custom templates must use Handlebars syntax ({{variable}}) and support the code2prompt context structure.

    # Use built-in templates
    code2prompt . --template default
    code2prompt . --template tree
    code2prompt . --template minimal
    
    # Use custom template file
    code2prompt . --template /path/to/custom-template.hbs
  10. Step 10

    Token tracking

    Monitor token usage to stay within LLM context limits:

    • Estimates tokens using tiktoken
    • Provides warnings when approaching limits
    • Helps optimize prompt size for different models
    # Set token limit
    code2prompt . --max-tokens 100000
    
    # View token count
    code2prompt . --dry-run  # Shows what would be included
    
    # Different limits for different models
    code2prompt . --max-tokens 8000   # For GPT-3.5
    code2prompt . --max-tokens 100000  # For GPT-4
    code2prompt . --max-tokens 200000  # For Claude 2
  11. Step 11

    Git integration

    Include Git context in your prompts for better AI understanding:

    • Diffs: Show recent changes
    • Logs: Include commit history
    • Branch comparisons: Compare branches

    This is useful when asking LLMs about recent changes or debugging.

    # Include git diff
    code2prompt . --git-diff
    
    # Include git log
    code2prompt . --git-log
    
    # Compare branches
    code2prompt . --git-compare main develop
    
    # All git context
    code2prompt . --git-all
  12. Step 12

    Python SDK usage

    Use Code2Prompt programmatically in Python scripts and AI agents:

    The Python SDK provides bindings to the Rust core, enabling integration into automation pipelines, RAG systems, and AI agent workflows.

    from code2prompt_rs import Code2Prompt, Config
    
    # Basic usage
    c2p = Code2Prompt()
    prompt = c2p.generate("./my-project")
    print(prompt)
    
    # With configuration
    config = Config(
        include=["*.py", "*.md"],
        exclude=["node_modules/", "__pycache__/"],
        max_tokens=100000,
        template="default"
    )
    c2p = Code2Prompt(config=config)
    prompt = c2p.generate("./my-project")
    
    # Save to file
    c2p.save("./my-project", "output.txt")
  13. Step 13

    MCP Server

    Run Code2Prompt as a local MCP (Model Context Protocol) server, enabling agentic applications to read your local codebase efficiently without bloating context windows.

    The MCP server allows AI agents to dynamically query parts of your codebase as needed.

    # Start MCP server (check documentation for latest start command)
    # Refer to https://code2prompt.dev/docs/ for MCP server setup
    
    # MCP server allows:
    # - Dynamic codebase querying
    # - Context-aware code reading
    # - Integration with MCP-compatible AI agents
  14. Step 14

    Alternative installation: Source build

    Build Code2Prompt from source for the latest features or custom compilation:

    This requires Git, Rust/Cargo, and CMake (for vendored libgit2 and OpenSSL).

    # Clone the repository
    git clone https://github.com/mufeedvh/code2prompt.git
    cd code2prompt/
    
    # Build and install
    cargo install --path crates/code2prompt
    
    # With all features
    cargo install --path crates/code2prompt --all-features
  15. Step 15

    Binary releases

    Download pre-built binaries for your operating system from the GitHub releases page if you cannot install via package managers.

    Download from: https://github.com/mufeedvh/code2prompt/releases
    
    Available for:
    - Linux (x86_64, aarch64)
    - macOS (x86_64, arm64)
    - Windows (x86_64)
  16. Step 16

    Resources

    Official website: https://code2prompt.dev

    Documentation: https://code2prompt.dev/docs/welcome/

    GitHub repository: https://github.com/mufeedvh/code2prompt

    Discord community: https://discord.com/invite/ZZyBbsHTwH

    Crates.io: https://crates.io/crates/code2prompt

    PyPI: https://pypi.org/project/code2prompt-rs/

    Docs.rs: https://docs.rs/code2prompt-core

    Website: https://code2prompt.dev
    Docs: https://code2prompt.dev/docs/welcome/
    GitHub: https://github.com/mufeedvh/code2prompt
    Discord: https://discord.com/invite/ZZyBbsHTwH
    Crates.io: https://crates.io/crates/code2prompt
    PyPI: https://pypi.org/project/code2prompt-rs/
    Docs.rs: https://docs.rs/code2prompt-core

Feature requests

Sign in to suggest features or vote on existing ones.

No feature requests yet.

Discussion

0 people marked this as worked·Sign in to mark your own.

Sign in to join the discussion.

No comments yet.