How code-review-graph Reduces Token Usage in AI Code Reviews: 5 Core Optimization Strategies
By building a persistent knowledge graph and transmitting only impacted sub-graphs as compact JSON payloads, code-review-graph reduces LLM token consumption by over 90% while maintaining review quality.
The code-review-graph tool from the tirth8205/code-review-graph repository addresses the high cost of AI code reviews by replacing full-file context with intelligent graph-based filtering. Instead of sending entire source files to language models, it constructs a repository knowledge graph and extracts only the minimal context necessary to evaluate changes. This approach dramatically reduces token usage in AI code reviews while preserving the semantic relationships required for accurate analysis.
The Knowledge Graph Foundation
At the core of the token reduction strategy is a persistent knowledge graph that maps the entire repository structure. The graph creates nodes for files, classes, functions, imports, and tests, establishing relationships that represent code dependencies and call flows.
When a review is triggered via detect-changes or update commands, the tool does not transmit the raw source tree. Instead, it queries this graph to identify precisely which elements relate to the current change set, ensuring the LLM receives only relevant context.
Selective Sub-Graph Extraction
The first optimization layer involves isolating the impact radius of modifications. Using the git diff as input, the tool calculates the minimal sub-graph that influences—and is influenced by—the changes.
Identifying Impact with get_impact_radius
The tools.get_impact_radius function (invoked from cli.py in the impact_cmd handler at lines 123–129) queries the graph for directly changed nodes plus their dependencies. This includes callers, callees, and data flow paths that could affect change behavior.
The tools.list_flows function (exposed via flows_cmd at lines 190–196 in cli.py) further refines this by tracing execution flows between components, ensuring critical paths are included while peripheral code is excluded.
Compact JSON Serialization
Once the relevant sub-graph is identified, the tool converts it into a concise JSON payload rather than transmitting full file contents. The graph.to_json method in code_review_graph/graph.py serializes only node identifiers, signatures, and minimal source snippets.
The code_review_graph/changes.py module assembles this into the final response payload. This structure contains strictly the semantic information required for review—function signatures, call relationships, and test coverage gaps—without the overhead of whitespace, comments, or unchanged implementation details.
Context Savings Measurement and Verification
The tool implements transparent tracking of token efficiency through the code_review_graph/context_savings.py module.
Conservative Token Estimation
The estimate_context_savings() function applies a conservative heuristic of 4 characters per token to calculate baseline costs. It compares the full repository context against the compact graph payload:
# From code_review_graph/context_savings.py
def estimate_context_savings(baseline, returned):
saved = max(0, baseline - returned)
percent = round((saved / baseline) * 100)
return {
"estimated": True,
"saved_tokens": int(saved),
"saved_percent": int(percent)
}
This metadata is attached to API responses under the context_savings key, enabling users to audit efficiency gains. The CLI flags --brief and --verify that trigger this display are implemented in cli.py at approximately lines 779 and 992.
Real-World Verification with Tiktoken
When the --verify flag is passed to the CLI, the verify_with_tiktoken function (lines 156–190 in context_savings.py) loads the tiktoken library to tokenize both the original source and the response JSON. This exposes the actual token count versus estimates, ensuring the reported savings reflect real LLM consumption patterns.
The CLI displays this in a Token Savings panel (formatted via format_context_savings_panel), showing side-by-side comparisons between naive full-context submission and the optimized graph payload.
Risk-Scored Review Prioritization
The final optimization layer applies risk-based filtering to the sub-graph. Nodes are ranked by risk scores representing change complexity, test coverage gaps, and dependency criticality.
By sending only the highest-risk items when token budgets are constrained, the model focuses its context window on the most valuable analysis targets. This prevents waste on low-impact boilerplate while ensuring complex logic receives adequate attention.
Practical Implementation Examples
CLI Usage with Token Savings Display
Run a brief review that displays the Token Savings panel:
code-review-graph detect-changes --brief
code-review-graph update --brief
Add verification to see real token counts using tiktoken:
code-review-graph detect-changes --brief --verify
The output displays actual savings metrics:
┌──────────────── Token Savings ────────────────┐
│ Full context would be: 12,932 tokens │
│ Graph context used: 773 tokens │
│ Saved: 12,159 tokens (~94%)│
│ Verified (tiktoken): 12,120 tokens (~93%) [12,932 → 812]
└───────────────────────────────────────────────┘
Programmatic Integration
Attach savings metadata to custom tooling:
from code_review_graph.context_savings import attach_context_savings
response = {"graph": my_subgraph}
response = attach_context_savings(
response,
original_context=repo_root,
returned_context=response["graph"]
)
This enables downstream tools to report efficiency metrics alongside review results.
Summary
The code-review-graph repository implements a multi-layered strategy to minimize token usage in AI code reviews:
- Graph-based filtering extracts only affected nodes using
get_impact_radiusandlist_flows, eliminating irrelevant file content from LLM context - Compact serialization converts sub-graphs to minimal JSON via
graph.to_jsonincode_review_graph/graph.py, removing whitespace and unchanged code - Quantified savings via
context_savings.pytracks estimated and verified token reductions, typically achieving 90%+ savings - Risk prioritization ensures high-value context consumes the token budget before peripheral elements
- CLI transparency with
--briefand--verifyflags (defined incli.pylines ~779 and ~992) exposes real-time efficiency metrics to developers
Frequently Asked Questions
How much can code-review-graph reduce token usage compared to full-file submission?
According to the estimation logic in code_review_graph/context_savings.py, typical implementations achieve 90% to 94% token reduction. The tool compares the full repository baseline against the compact graph payload, with verification via tiktoken confirming these estimates align with actual LLM tokenization patterns.
What is the impact radius and how does it limit context size?
The impact radius defines the boundary of code affected by a change, including direct modifications, calling functions, called functions, and related test files. The tools.get_impact_radius function (called from cli.py lines 123–129) queries the knowledge graph to return only nodes within this radius, excluding files that have no semantic relationship to the diff.
How does the tool verify that estimated token savings are accurate?
When using the --verify flag, the verify_with_tiktoken function (lines 156–190 in code_review_graph/context_savings.py) tokenizes both the original source files and the serialized JSON payload using OpenAI's tiktoken library. This produces verified token counts that display alongside estimates in the CLI output, ensuring transparency in the reported efficiency metrics.
Can I integrate the token savings calculation into my own Python tools?
Yes. Import attach_context_savings from code_review_graph.context_savings and pass your original repository context plus the returned graph payload. The function calculates baseline tokens (using the 4-character heuristic), measures the actual payload size, and injects saved_tokens and saved_percent metadata into your response object, enabling custom reporting dashboards.
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 →