Skip to content

Configuring Code2Prompt 📖

Tutorial Overview

Manually typing long exclude patterns or specific tokenizer settings every time can be tedious. Therefore this tutorial shows you how to use a .c2pconfig configuration file to “set and forget” your project settings.


Ensure you have code2prompt installed. If you haven’t installed it yet, refer to the Installation Guide. Familiarity with TOML syntax is helpful but not required.


The .c2pconfig file is a configuration file written in TOML format. When you run code2prompt, it automatically searches for this file in your current working directory.

It allows you to define:

  • Filtering Rules: Persistent include/exclude patterns.
  • Output Formats: Default to JSON, Markdown, or XML.
  • Template Context: Pre-define variables for your Handlebars templates.

Create a file named .c2pconfig at the root of your project to define your base behavior.

# .c2pconfig example
default_output = "stdout" # Options: stdout, clipboard, file
include_patterns = ["src/**/*.rs", "Cargo.toml"]
exclude_patterns = ["**/target/**", "tests/fixtures/**"]
line_numbers = true
output_format = "markdown"
[user_variables]
project_name = "MyAwesomeProject"
author = "Developer"

The following table describes the keys available in the configuration file.

KeyTypeDescription
pathStringDefault path to codebase (usually .).
include_patternsArrayGlob patterns of files to include.
exclude_patternsArrayGlob patterns of files to exclude.
line_numbersBooleanIf true, adds line numbers to code blocks.
absolute_pathBooleanUse absolute paths instead of relative paths.
full_directory_treeBooleanGenerate the full tree even for excluded files.
output_formatStringmarkdown, json, or xml.
sort_methodStringname_asc, name_desc, date_asc, date_desc.
encodingStringTokenizer: cl100k, p50k, o200k.
diff_enabledBooleanInclude git diff (HEAD vs Index).
token_map_enabledBooleanDisplay a hierarchical token usage map.

Follow these steps to integrate a configuration file into your workflow.

  1. Initialize your Configuration Navigate to your project root and create the config file:

    Terminal window
    touch .c2pconfig
  2. Define your Source of Truth Exclude heavy directories like node_modules or build artifacts to keep the LLM context clean.

    exclude_patterns = [
    "**/node_modules/**",
    "package-lock.json",
    "dist/**"
    ]
  3. Set your Model Encoding Match the tokenizer to your target LLM. Use o200k for GPT-4o, or cl100k for Claude and GPT-4.

    encoding = "o200k"
  4. Inject Custom Context Use the [user_variables] section to pass data into your Handlebars templates.

    [user_variables]
    project_goal = "Refactor the authentication module for better security."
  5. Run with Zero Arguments Simply run the tool. It will now respect all your predefined rules without extra CLI flags.

    Terminal window
    code2prompt .

It is important to understand how code2prompt decides which settings to use when multiple sources conflict.


Use this setup if your primary goal is generating prompts for code reviews.

default_output = "clipboard"
include_patterns = ["*.rs"]
exclude_patterns = ["**/test*","**code2prompt-python*"]
line_numbers = false
absolute_path = true
[user_variables]
project = "code2prompt"