Model or dataset
mufeedvh/code2prompt avatar
mufeedvh/code2prompt

code2prompt: Convert codebases into LLM prompts with token counting

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

7,717 stars453 forksRustMIT

At a glance

What is it?
A Rust-built CLI tool that reads your codebase, respects .gitignore rules, applies custom templates, and estimates token usage, producing a single prompt ready for ChatGPT, Claude or other large language models.
Who is it for?
Use code2prompt if you frequently paste code into LLM chat or build agents that read source trees. Skip it if you work in a closed IDE without clipboard access, or if your codebase is too large to fit in a single prompt window.
Can I use it commercially?
Yes. MIT is a permissive licence: you can use, modify and sell software built on it, as long as you keep its copyright and licence notices.
Is it still maintained?
Yes. The repository last received commits 5 days ago.
What is it written in?
Mainly Rust, according to GitHub's language statistics.

Answers come from the project's GitHub data, last synced on September 30, 2026, and from our analysis. They are not legal advice.

Editorial analysis

code2prompt reads your repository and generates structured prompts

code2prompt is a command-line context engineering tool written in Rust. Point it at a directory and it reads the source tree, respects .gitignore rules to exclude irrelevant files, formats the code into a readable structure, and outputs a prompt that you can paste into ChatGPT, Claude or any other LLM. The README describes it as a tool designed to ingest codebases and format them for large language models, whether you are manually copying context, building AI agents or running an MCP server.

The tool operates at the file level. It traverses your repository, applies include and exclude filters using glob patterns, reads each file respecting encoding detection, and assembles everything into a single formatted output. Unlike a simple concatenation of files, code2prompt applies structure through templates, counts tokens to help you stay within context limits, and integrates Git metadata like diffs and logs if present. The README notes that the tool provides a minimal CLI, an interactive terminal user interface (TUI), Python bindings, and an MCP server implementation.

Installing code2prompt via cargo, Homebrew or pip

Install code2prompt using the Rust package manager if you have Cargo installed:

bash
cargo install code2prompt

For Wayland-based systems (e.g., recent Linux desktops), enable Wayland clipboard support with a feature flag:

bash
cargo install --features wayland code2prompt

On macOS with Homebrew, install directly from the tap:

bash
brew install code2prompt

For Python integration, install the Python SDK from PyPI:

bash
pip install code2prompt-rs

The Python bindings allow you to use code2prompt from Python scripts, which is useful if you are building AI agents or integrating codebase reading into a larger pipeline. If your platform lacks a pre-built binary, compile from source using Git and Rust:

bash
git clone https://github.com/mufeedvh/code2prompt.git
cd code2prompt/
cargo install --path crates/code2prompt

Generating prompts and saving to file

Once installed, run code2prompt with a directory path:

bash
code2prompt .

This reads the current directory and outputs the generated prompt to stdout. Use the `-c` flag to copy the output directly to your clipboard instead of printing it:

bash
code2prompt . -c

To save the output to a file rather than stdout or clipboard, use the `--output-file` flag:

bash
code2prompt path/to/project --output-file prompt.txt

code2prompt provides both a minimal CLI for automation and an interactive TUI that lets you select which files to include, configure templates, and see token counts before generating. The TUI mode is useful for exploring a large codebase and selecting relevant portions without writing shell scripts.

Filtering files, respecting .gitignore, and using templates

code2prompt filters files using glob patterns, just like Git does. The tool respects your .gitignore rules automatically, so files you have ignored for version control are also excluded from the prompt. You can add further include and exclude patterns without editing .gitignore; the documentation covers glob syntax and pattern matching.

Prompts are structured using Handlebars templates, which allow customization for different use cases. The tool provides flexible templating so you can adapt the output format for specific LLM models or task types. If no template is specified, code2prompt uses a default that presents files in a readable hierarchy.

The tool also offers smart file reading for various formats: CSV, Jupyter notebooks, JSONL and other structured files are parsed and presented readably rather than included as raw binary data. This avoids token waste on unparseable content.

Token counting and Git integration

A core feature is token estimation. Before generating the full prompt, code2prompt estimates how many tokens it will consume using parallel per-file token counts and estimated template overhead. The full rendered prompt is not re-tokenized and the estimate excludes the JSON output envelope, so the count is an educated guess rather than the true final token count, but it helps prevent accidentally generating a prompt that exceeds your model's context window.

code2prompt can also include Git metadata in your prompt. You can include Git diffs, logs, and branch comparisons. This is useful when asking an LLM to review changes or understand recent development history without manually copying commit messages and diffs.

Running as an MCP server for agent applications

In addition to CLI usage, code2prompt can run as a local MCP (Model Context Protocol) server. This enables AI agent applications to read your codebase efficiently without embedding the entire repository in the agent's context window. The MCP server implementation allows agentic applications to query your local codebase on demand.

The project also provides an agent skill via the Skills CLI. You can install it with `npx skills add mufeedvh/code2prompt`, which teaches a coding agent to use code2prompt for repository navigation and scoped context gathering. The skill includes templates and can use an optional `entity-map` feature to create a compact map of functions and classes before reading relevant source files and tests.

Rust workspace with separate crates for CLI, Python, and MCP

code2prompt is actively developed. The last push was on 2026-09-25, and recent releases include v4.2.0 from 2025-12-11, v4.0.2 from 2025-09-18, and v3.0.2 from 2025-04-09. The tool is written in Rust for performance and is organized as a workspace with multiple crates: a core library, the CLI tool, Python bindings, and the MCP server.

The project is licensed under the MIT license. The repository includes documentation at code2prompt.dev with installation, usage, and configuration guides. The Cargo.toml workspace shows dependencies on Tokio for async operations, Ratatui for the TUI, Git2 for Git integration, Handlebars for templating, and Tiktoken-rs for token counting.

Editorial conclusion

Use code2prompt if you frequently paste code into LLM chat or build agents that read source trees. Skip it if you work in a closed IDE without clipboard access, or if your codebase is too large to fit in a single prompt window. Start by installing via cargo or Homebrew, pointing it at your repository root, and checking the token estimate before copying to your LLM.

Frequently asked questions

How do I use code2prompt?

Point code2prompt at your repository directory. Run `code2prompt path/to/project` to generate a prompt. It reads the source tree, filters by .gitignore and your custom patterns, applies a template, and outputs the result to stdout. Use `-c` to copy to clipboard or `--output-file` to save to a file.

Does code2prompt work with VS Code?

code2prompt is a CLI tool, not a VS Code extension. You run it from your terminal or shell, then copy its output to your LLM. The documentation and community may provide integrations, but the core tool is command-line only.

What formats does code2prompt support?

According to the README, code2prompt handles many file formats intelligently. It reads CSV, Jupyter notebooks, JSONL and other structured files, parsing them readably rather than including raw binary data. The tool respects .gitignore rules and applies glob patterns for filtering.

Official sources

  1. License: MIT
  2. mufeedvh/code2prompt on GitHub
  3. Project website
  4. README
  5. Releases
Add this badge to your README

If you maintain this project, the badge below links readers to this analysis and shows its maintenance status from the daily GitHub snapshot. Paste the markdown into your README; add ?metric=license or ?metric=stars to the image URL for a different field.

Add this badge to your README

markdown
[![Hysen Labs](https://hysenlabs.com/badge/mufeedvh-code2prompt.svg)](https://hysenlabs.com/projects/mufeedvh-code2prompt)