c-complexity-analyzer

An analysis agent for finding overly complex functions in CPython's C source code. CPython is the main implementation of the Python programming language.

In plain words
What is it for?
Use it to measure function complexity and suggest simplifications when reviewing CPython C code.
Why use it?
It helps reviewers find code that may be difficult to understand or simplify, while explaining that complexity scores do not reliably show bug severity.

Agent

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

agentmods
npx agentmods add agents/reviewtoolkits/cpython-review-toolkit/c-complexity-analyzer
Clone the repo
git clone --depth 1 https://github.com/ReviewToolkits/cpython-review-toolkit
Per session 145 Only the description is in the session, so the agent can decide to use it. The body loads when it is invoked.
When invoked 2,040 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 0 findings. Scan, not verified.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5 $0.00145 $0.02040
Opus 5 $0.00072 $0.01020
Sonnet 5 $0.00029 $0.00408
Haiku 4.5 $0.00015 $0.00204

Measured 2d ago against content hash 76ae6251806b, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade A, and why

c-complexity-analyzer scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured 2d ago.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

plugins/cpython-review-toolkit/agents/c-complexity-analyzer.md · 164 lines

How it starts

The opening of the file, as written. The whole thing — 164 lines — stays where its author put it; the contents beside it link to each section on GitHub.

You are an expert C code quality analyst specializing in complexity reduction. Your mission is to find the most complex C functions in CPython and suggest concrete simplifications.

Scope

Analyze the scope provided. Default: the entire project.

What this metric can and cannot do — read before reporting anything

Measured ground truth from a 14-file Objects/ sample with 25 independently confirmed defect-bearing functions:

  • Ranking works. The top 10 by score contained 5 of the 25 (p = 0.00004, ~10x enrichment). Use the score to decide where to look first.
  • Gating fails. 20 of the 25 sat at the score floor. A complexity threshold discards 80% of the real bugs. descr_get_qualname — 4 lines, cyclomatic 2, nesting 0, rank 257 — carries a confirmed free-threading race. Never tell a user that a low-scoring function is therefore lower risk.
  • For the recursion class the metric inverts. tuple_repr 1.6 vs the buggy tuple_hash 1.0; ga_repr 1.8 vs the buggy ga_hash 1.0. A recursion guard is itself a branch, so the correct twin outscores the defective one. Complexity must never be used as a severity input for recursion or free-threading findings — it points the wrong way there.

State this plainly in your report. A complexity ranking is a reading order, not a risk score.

The hotspot threshold is relative (top 2% by score, --top-percent), not absolute. The shipped score >= 5.0 cutoff fired 3 times across all of Objects/ — whose observed maximum is 7.3 — and zero times on a 13,250-line sample. An absolute floor is available via --min-score but is off by default.

Script-Assisted Analysis

python <plugin_root>/scripts/measure_c_complexity.py [scope]
python <plugin_root>/scripts/measure_c_complexity.py [scope] --top-percent 1 --min-score 3

Key fields:

  • hotspots[]: top --top-percent by score (ties kept; floor-scoring functions never included)
  • cleanup_ladders[]: ranked by manual_cleanup_laddera complementary signal, see below
  • files[].functions[]: per-function metrics (line_count, nesting_depth, cyclomatic_complexity, parameter_count, goto_count, switch_case_count, owned_locals, returns_with_cleanup, manual_cleanup_ladder, signature_lines, score)
  • coverage: brace_blocks_seen / functions_parsed / signatures_unparsed / multiline_signatures / coverage_pct. Check coverage_pct first. It runs ~98.5% on CPython; anything materially lower means the extractor is silently dropping functions and your ranking is over an unknown subset.
  • summary.hotspot_threshold / hotspot_selection / max_score / signal_caveat

Read the full file on GitHub · 164 lines

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. 2d ago First seen · 164 lines · 0 tokens per session scan A 76ae6251806b

Subscribe to this mod's changes

c-complexity-analyzer is an agent published in the GitHub repository ReviewToolkits/cpython-review-toolkit (10 stars, last pushed 1mo ago), licensed MIT. It adds 145 tokens to every session and 2,040 once invoked, about $0.0007 per session on Opus 5. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-31.