How to Adjust the Similarity Threshold for Duplicate Code Detection in Code-Graph-RAG

You can adjust the similarity threshold for duplicate code detection by passing a custom --threshold value to the cgr duplicates command, or by modifying the DUPLICATES_DEFAULT_THRESHOLD constant in the source code for a permanent change.

Code-Graph-RAG identifies duplicate (clone) code by comparing the Jaccard similarity of structural fingerprints extracted from functions and methods. The threshold controls how similar two code blocks must be to trigger a match. This article covers both command-line overrides and source-level modifications using actual paths and APIs from the vitali87/code-graph-rag repository.


Understanding the Duplicate Detection Mechanism

The duplicate detection pipeline in Code-Graph-RAG computes structural fingerprints for code entities and compares them using Jaccard similarity. Two pieces of code are flagged as duplicates when their similarity score meets or exceeds the configured threshold.

The threshold parameter flows through three key layers:

  • CLI layer: evals/duplicates.py exposes --threshold as a user-facing option
  • Configuration layer: codebase_rag/duplicates.py builds a DuplicatesConfig object
  • Constant layer: codebase_rag/constants/ defines DUPLICATES_DEFAULT_THRESHOLD as the fallback value

Method 1: Override Threshold via Command Line

The fastest way to adjust sensitivity for a single run is the --threshold flag on the cgr duplicates command.

In evals/duplicates.py, the main function defines this argument and wires it to the duplicate detection engine:


# evals/duplicates.py (lines 44-46)

parser.add_argument(
    "--threshold",
    type=float,
    default=cs.DUPLICATES_DEFAULT_THRESHOLD,
    help="Minimum Jaccard similarity for duplicate detection (default: %(default)s)",
)

The parsed value is passed into the DuplicatesConfig used by the evaluation pipeline:


# evals/duplicates.py – configuration construction

config = DuplicatesConfig(
    threshold=args.threshold,
    # ... other parameters

)

Practical Example


# Require 90% similarity – stricter matching, fewer false positives

cgr duplicates \
    --project-name my-project \
    --threshold 0.90 \
    /path/to/graph

# Allow 60% similarity – looser matching, catches more potential clones

cgr duplicates \
    --project-name my-project \
    --threshold 0.60 \
    /path/to/graph

Values are floats between 0.0 and 1.0, where 1.0 requires identical fingerprints and 0.0 would match everything.


Method 2: Modify the Default Constant

For persistent changes across all invocations—including scripts that don't expose --threshold—edit the constant in codebase_rag/constants/.

The constant is defined as:


# codebase_rag/constants/__init__.py (or appropriate constants module)

DUPLICATES_DEFAULT_THRESHOLD = 0.85  # Default Jaccard similarity threshold

This constant is referenced when building default configurations in codebase_rag/duplicates.py:


# codebase_rag/duplicates.py – default config construction

from codebase_rag import constants as cs

def default_duplicates_config(
    threshold: float = cs.DUPLICATES_DEFAULT_THRESHOLD,
    min_nodes: int = cs.DUPLICATES_MIN_NODES,
    exact_only: bool = False,
    exclude_patterns: tuple[str, ...] = (),
) -> DuplicatesConfig:
    ...

Modification Steps

  1. Locate DUPLICATES_DEFAULT_THRESHOLD in codebase_rag/constants/__init__.py
  2. Change the value (e.g., from 0.85 to 0.75)
  3. Reinstall or run from source: pip install -e .

Method 3: Build Custom Configuration Programmatically

For integration into custom workflows, construct a DuplicatesConfig directly with any threshold value:

from codebase_rag.duplicates import default_duplicates_config, find_duplicates

# Create custom configuration with 80% threshold

custom_cfg = default_duplicates_config(
    threshold=0.80,
    min_nodes=5,           # Minimum AST nodes to consider

    exact_only=False,      # Allow near-matches, not just identical

    exclude_patterns=("test_", "__init__"),  # Exclude patterns

)

# Run detection with custom settings

duplicates = find_duplicates(
    graph_path="/path/to/graph",
    config=custom_cfg,
    project_name="my-project"
)

This approach isolates changes to specific call sites without affecting global defaults or CLI behavior.


Choosing the Right Threshold Value

Threshold Use Case
0.95–1.0 Near-exact clones only; minimal false positives
0.80–0.95 Standard duplicate detection; balanced precision/recall
0.60–0.80 Detect refactored or modified clones; higher false positive risk
< 0.60 Experimental similarity search; requires manual review

Start with the default (0.85) and adjust based on:

  • Language verbosity: Verbose languages (Java, Go) may need lower thresholds than concise ones (Python, Ruby)
  • Codebase maturity: Legacy codebases often accumulate near-duplicates that benefit from stricter thresholds
  • Downstream use: Automated refactoring requires higher precision than exploration or reporting

Key Source Files Reference

File Purpose
evals/duplicates.py CLI entry point; parses --threshold and orchestrates evaluation
codebase_rag/duplicates.py Core detection logic; consumes DuplicatesConfig with threshold parameter
codebase_rag/constants/__init__.py Defines DUPLICATES_DEFAULT_THRESHOLD and related constants
codebase_rag/constants/ast_python.py Language-specific constants that may influence fingerprint sensitivity

Summary

  • Command-line override: Use cgr duplicates --threshold 0.90 for one-off adjustments
  • Source modification: Edit DUPLICATES_DEFAULT_THRESHOLD in codebase_rag/constants/ for permanent changes
  • Programmatic control: Build DuplicatesConfig with custom thresholds for isolated use cases
  • Default value: 0.85 Jaccard similarity, defined in the constants module and exposed via cs.DUPLICATES_DEFAULT_THRESHOLD

All three methods reference the same underlying configuration class, ensuring consistent behavior across CLI, library, and custom integration contexts.


Frequently Asked Questions

What similarity metric does Code-Graph-RAG use for duplicate detection?

Code-Graph-RAG uses Jaccard similarity computed over structural fingerprints of Abstract Syntax Tree (AST) nodes. The metric compares the intersection and union of fingerprint sets, producing a score from 0.0 (no overlap) to 1.0 (identical fingerprints).

Where is the default threshold value defined in the source code?

The default threshold is defined as DUPLICATES_DEFAULT_THRESHOLD in codebase_rag/constants/__init__.py. This constant is imported as cs throughout the codebase and referenced in codebase_rag/duplicates.py when constructing default configurations.

Can I set different thresholds for different programming languages?

The current implementation uses a global threshold value. For language-specific behavior, you must create separate DuplicatesConfig instances with different thresholds and invoke find_duplicates() separately for each language's graph, or fork and modify the constants module to add language-specific thresholds.

Why does changing the threshold not affect my results immediately?

If running from an installed package (via pip install), modifying source files requires reinstallation with pip install -e . (editable mode) or rebuilding. The CLI uses the installed package's compiled constants, not the raw source files. Verify your changes with python -c "from codebase_rag import constants as cs; print(cs.DUPLICATES_DEFAULT_THRESHOLD)".

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:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →