Skip to content

Getting Started with Code2Prompt

Tutorial Overview

Welcome to Code2Prompt! This tutorial provides an introduction to using Code2Prompt to generate AI-ready prompts from your codebases. We’ll explore its core functionality and demonstrate its usage across different integration methods.

Code2Prompt is a system tool designed to extract codebase informations for LLMs. It extracts relevant code snippets, applies filtering, and formats the information into structured prompts optimized for LLM. This simplifies tasks like code documentation, bug detection, refactoring, and more.

Code2Prompt offers different flavors to suit various use cases:

A core rust library that provides the foundation for code ingestion and prompt

For detailed installation instructions for all methods (CLI, SDK, MCP), please refer to the

Let’s start with a simple example using the CLI. Create a sample project:

Terminal window
mkdir -p my_project/{src,tests}
touch my_project/src/main.rs my_project/tests/test_1.rs
echo 'fn main() { println!("Hello, world!"); }' > my_project/src/main.rs

Now, generate a prompt:

Terminal window
code2prompt my_project

This copies a prompt to your clipboard. You can customize this:

  • Filtering: code2prompt my_project --include="*.rs" --exclude="tests/*" (includes only .rs files, excludes tests directory)
  • Output File: code2prompt my_project --output-file=my_prompt.txt
  • JSON Output: code2prompt my_project -O json (structured JSON output)
  • Custom Templates: code2prompt my_project -t my_template.hbs (requires creating my_template.hbs)

To learn more about code2prompt, check out the following tutorials:

For programmatic control, use the Python SDK:

from code2prompt_rs import Code2Prompt
config = {
"path": "my_project",
"include_patterns": ["*.rs"],
"exclude_patterns": ["tests/*"],
}
c2p = Code2Prompt(**config)
prompt = c2p.generate_prompt()
print(prompt)

This requires installing the SDK (pip install code2prompt_rs). Refer to the SDK documentation for more details.

To reduce token usage of your harness, you can let your agent run code2prompt as a MCP server or as a skill. This allows agents to request code context dynamically in a way that replaces multiple find, glob and grep calls.