← Journal
Turn a Codebase into One Prompt with code2prompt

Turn a Codebase into One Prompt with code2prompt

A record of running the code2prompt CLI with the network blocked, converting a repository's source tree and file contents into one prompt, and checking the resulting token count.

Repository covered in this article

mufeedvh/code2prompt

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

7.5k Rust MIT License

You have to decide what to copy and paste before asking an LLM about a codebase. You open files one by one, collect their contents, and decide how much to send. An LLM does not need to reason through this task. A command can handle it. code2prompt is a Rust CLI that converts the source tree and file contents of a specified directory into one prompt. I built it and ran it against its own source.

code2prompt scans a repository and formats its source tree and file contents into one text file. The project publishes its source under the MIT License(*1).

1. Build it

Along with the Rust toolchain, you need build-essential and perl because the git2 dependency crate builds OpenSSL from source.

FROM docker.io/library/rust:1.90.0-slim
RUN apt-get update && apt-get install -y --no-install-recommends \
    pkg-config libssl-dev build-essential perl \
    && rm -rf /var/lib/apt/lists/*
WORKDIR /work
podman build -t code2prompt-sandbox .
podman run --rm --userns=keep-id \
  -v "$(pwd)":/work -w /work code2prompt-sandbox \
  cargo build --release

The -v option makes the specified directory readable and writable from the container. Run this in an empty directory dedicated to the experiment, and keep private keys and credentials out of it. The installation phase is the sole network-enabled step: Cargo downloads dependency crates, and git2 builds OpenSSL from source. The default bridge network provides enough outbound access. Leave --network host off; it shares the host's network namespace. The build finished in under three minutes and produced a single binary at target/release/code2prompt.

2. Convert its own source

I ran the binary I built with the network disabled and pointed it at code2prompt's core crate source directory.

podman run --rm --network none --memory 2g --cpus 2 --pids-limit 512 --userns=keep-id \
  -v "$(pwd)":/work -w /work code2prompt-sandbox \
  ./target/release/code2prompt crates/code2prompt-core/src --output-file /work/output.md

The command printed:

[i] Using config from: /work/.c2pconfig
[i] Token count: 29K, Model info: ChatGPT models, text-embedding-ada-002
[✓] Prompt written to file: /work/output.md

The output file output.md contains 3,924 lines. It starts with the source tree in a code block, then lists each file's contents under a heading for its path.

Project Path: src

Source Tree:

┌── analysis.rs
├── builtin_templates.rs
├── configuration.rs
...

The tool reports an estimated token count, letting you check whether the prompt fits within the LLM's input limit before you send it. The tool does not follow symbolic links by default, so it reads within the target directory.

Let a command handle the work before copy and paste

Opening files and assembling their contents before you paste them takes time by hand. One command can write both the source tree and file contents. You can focus on what to ask the LLM. Treat the preparation before sending material to an LLM as a mechanical step.

Sources

*1 code2prompt (GitHub), https://github.com/mufeedvh/code2prompt, MIT License