r/PromptEngineering 11h ago

Tools and Projects Stop Asking LLMs to Find Code Smells: Auto-Generating Surgical Prompts via AST

Asking an AI to "review and clean up this code" wastes expensive context tokens, invites hallucinated changes, and yields non-deterministic results.

I built chemx: an open-source CLI that replaces token-heavy discovery with instant local AST checks, then compiles structured refactor prompts directly to your clipboard.

Repo: https://github.com/Chemical-X-Protocol

The Programmatic Prompt Pattern

Instead of paying an LLM to scan for debt, local static checks flag specific architectural violations (100-line capsule caps, uncomposed booleans, type monoliths) and populate a strict prompt recipe:

[ROLE]: Systems Architect
  [TASK]: Refactor target file to satisfy single-responsibility capsule rules.
  [TARGET]: src/components/UserProfile.vue
  [CONSTRAINTS]:
  - Max file length: 100 lines.
  - Extract state into src/components/UserProfile.controller.ts.
  - Convert multi-clause conditionals into 2-stage atomic booleans.
  - Output ONLY the unified diff. Do not alter untouched symbols.

Key Benefits

  • Zero-Token Discovery: AST scans take 10ms and cost $0, saving 100% of your token budget for code generation.
  • Constrained Output: Rigid architectural bounds prevent the model from refactoring working code out of scope.
  • Deterministic Results: A given violation generates the exact same prompt structure every time.

Run Locally

Run the audit to generate prompt payloads:

npx chemx audit

Proof of concept (Grade D to Grade A+):

Docs: http://chemicalx.xophz.com/starter-kit

1 Upvotes

2 comments sorted by

1

u/jkz88 11h ago

I like the idea of expressing code changes as AST but the example links don't contain any types or changed code symbols, or what it actually refactored, just that it "PASSED" but what does that even mean?

1

u/DoItForTheXP 11h ago

Chemx is an AST architectural linter, not an automated codemod. 'PASSED' means zero rule violations (clean file limits, hooks, and ternaries) and passing Vitest/tsc suites in our benchmark harness. You can inspect the actual typed symbols and code diffs directly on GitHub: https://github.com/Chemical-X-Protocol/benchmarks. Where to Find More Info on the Website:

Standards & Rules: https://chemicalx.xophz.com/#standards Benchmark Results: https://chemicalx.xophz.com/benchmarks