How to Configure the Minimum Size for Detecting Duplicated Code in Code-Graph-RAG
Set the min_size parameter (default 15 skeleton nodes) via the --min-size CLI flag or the min_size argument in the Python API to filter out trivial functions and focus on substantial code clones.
Code-Graph-RAG detects structural duplicates by walking the abstract-syntax-tree (AST) of every function and building a skeleton of significant nodes. The min_size configuration controls the minimum number of skeleton nodes a function must contain to be eligible for duplicate detection, effectively filtering out noise from trivial getters and setters. This guide shows you how to configure this threshold using the command-line interface, Python API, or constants file in the vitali87/code-graph-rag repository.
Understanding the min_size Filter
The duplicate detection engine analyzes the AST structure of each function to create a simplified skeleton representation. Small functions consisting of only a few skeleton nodes—such as simple property accessors or passthrough methods—are filtered out because they would generate excessive noise and dominate the candidate index.
In codebase_rag/constants/duplicates.py (line 92), the default threshold is defined as DUPLICATES_DEFAULT_MIN_NODES = 15. This means any function with fewer than 15 skeleton nodes is automatically excluded from duplicate analysis. The engine receives this value as the min_nodes field of a DuplicatesConfig object and discards candidates below the threshold before performing similarity calculations.
How to Configure the Minimum Size
Using the Command-Line Interface
The CLI exposes the --min-size flag (short form -n) in codebase_rag/cli.py (lines 49-53). This parameter maps directly to the internal configuration and overrides the default constant.
cgr duplicates --min-size 30 --threshold 0.85 my-project
This command increases the threshold to 30 skeleton nodes, ensuring only substantial functions are compared for duplication. Omitting the flag uses the default value of 15 defined in the constants file.
Using the Python API
When invoking duplicate detection programmatically via codebase_rag/tools/duplicate_detection.py, pass the min_size argument to the find_duplicate_code function (lines 22-26). The tool validates the input (lines 94-99) before forwarding it to the detection engine.
from codebase_rag.tools.duplicate_detection import create_find_duplicates_tool
tool = create_find_duplicates_tool(ingestor)
report = await tool.find_duplicate_code(
project="my-project",
threshold=0.85,
min_size=30, # Custom minimum skeleton size
limit=20,
)
print(report)
Changing the Global Default
To permanently change the default behavior without specifying the parameter each time, modify the constant in codebase_rag/constants/duplicates.py:
# Change from 15 to 30
DUPLICATES_DEFAULT_MIN_NODES = 30
After rebuilding or reinstalling the package, all invocations that rely on the default will automatically use the new value of 30 nodes.
Validation Rules and Constraints
The system enforces strict validation to prevent invalid configurations. In codebase_rag/tools/duplicate_detection.py (lines 58-60), the _validation_error logic checks that min_size is greater than or equal to 1. If you attempt to set a value of 0 or a negative number, the tool emits an error message and halts execution.
Selecting an Appropriate min_size Value
Choosing the right threshold depends on your codebase characteristics:
- Default (15 nodes) – Works well for medium-to-large codebases; filters out most trivial accessor methods while capturing meaningful logic.
- Higher values (30+) – Use when your project contains many small utility functions and you want to focus exclusively on substantial clones with complex logic.
- Lower values (≥1) – Only necessary for very small repositories where every function is of interest and even simple methods should be checked for duplication.
Summary
- The
min_sizeparameter filters functions by AST skeleton node count to eliminate noise from trivial code. - The default value is 15 nodes, defined as
DUPLICATES_DEFAULT_MIN_NODESincodebase_rag/constants/duplicates.py. - Configure via the
--min-sizeCLI flag incodebase_rag/cli.pyor themin_sizeAPI argument incodebase_rag/tools/duplicate_detection.py. - Values must be ≥ 1; otherwise validation fails with an error.
- Increase to 30 or higher for codebases with many small utilities; decrease only when analyzing very small repositories where every function matters.
Frequently Asked Questions
What is the default minimum size for duplicate detection in Code-Graph-RAG?
The default is 15 skeleton nodes, defined as DUPLICATES_DEFAULT_MIN_NODES in codebase_rag/constants/duplicates.py (line 92). This value effectively filters out trivial property accessors and one-line methods while ensuring meaningful function structures are captured for analysis.
How do I change the minimum size when using the command-line interface?
Pass the --min-size flag (or -n shorthand) to the duplicates command. For example: cgr duplicates --min-size 30 my-project. This overrides the default constant for that specific invocation without modifying the source code.
Can I set a custom minimum size when calling the duplicate detection tool programmatically?
Yes. When using the find_duplicate_code tool created via create_find_duplicates_tool in codebase_rag/tools/duplicate_detection.py, pass the min_size argument in the function call (lines 22-26). The implementation validates this value (lines 94-99) before processing the request.
What happens if I set min_size to 0 or a negative number?
The validation logic in codebase_rag/tools/duplicate_detection.py (lines 58-60) rejects values less than 1 and emits an error message. The minimum valid value is 1, ensuring that at least one skeleton node exists for structural comparison.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →