A fresh build dropped in r/PromptEngineering yesterday, and step two of the workflow is the twist that got me hooked. It’s a jab at how we usually prompt AI to clean up code, cross fingers, and hope for the best. u/DoItForTheXP, the Redditor behind it, built an open-source CLI called “chemx” that skips that guessing game entirely.
The problem this contributor is solving is one most of us have felt. Asking a model to “review and clean up this code” burns expensive context on discovery work a simple script could do in milliseconds. Worse, it invites the model to hallucinate changes nobody asked for, and you get a different answer every time you run the same prompt. That’s not a workflow, that’s a slot machine, and slot machines are a terrible way to ship production code.
Here’s the setup: chemx runs local AST checks instead of paying an LLM in tokens to hunt for messy code. It looks for file length caps, tangled conditionals, and type monoliths that grew out of control. Those checks take about 10 milliseconds and cost nothing to run. The tool then turns whatever it finds into a structured prompt, ready to paste into your model of choice.
The Twist
Here’s the part I didn’t expect: chemx doesn’t just describe the problem, it writes the entire order for you! The generated prompt assigns a role, “Systems Architect,” names the exact file, and lists hard constraints like a 100-line cap. It also spells out concrete moves, like extracting state into a separate controller file or converting tangled conditionals into two-stage atomic booleans. Then it tells the model to output nothing but a unified diff.
The LLM’s job shrinks from “go find something wrong” to “execute this surgical instruction and touch nothing else.” That’s a real shift in how the loop works. A linter like ESLint usually just flags a violation and stops there, leaving you to write the fix prompt yourself. This creator’s tool turns the flag directly into a deterministic prompt, so the same violation produces the same instructions every time you run it.
The original post frames this as three benefits, and they hold up. Zero-token discovery means the scan costs nothing, saving your whole budget for the part that actually needs a model: writing the fix. Constrained output means the model can’t wander off and “improve” code you never asked it to touch. Deterministic results mean two engineers hitting the same violation get the exact same prompt, which makes the process auditable instead of vibes-based.
Try It Yourself
Want to see it on your own codebase? This contributor laid out a simple mini-workflow in the repo.
- Run npx chemx audit in your project root.
- Let the local AST scan flag violations like oversized files or unstructured booleans.
- The tool compiles a structured refactor prompt and copies it straight to your clipboard.
- Paste it into your LLM, get back a unified diff, and apply only that.
No back and forth needed, and no re-explaining your codebase from scratch every session. Apply the diff, rerun the audit, and watch the grade move.
Pro Tips
Before you trust this blindly, know what one sharp commenter, u/jkz88, pointed out on the thread. The proof-of-concept links show a grade jumping from D to A+, but they don’t show the actual diff or which symbols changed. Ask for the real before and after, not just a passing badge, before you lean on a tool like this in production.
Second tip: pair the rigid constraints with a quick human skim. A prompt this strict stays in its lane, but it can still miss a refactor that’s technically outside scope and worth doing. Rigid rules are great for catching the obvious stuff, not so great at judging taste.
Third tip: start small. Point chemx at one messy file instead of your whole repo on the first run. Read the generated prompt closely, line by line, and confirm the constraints actually match how your team wants code structured.
This is a different bet than the usual approach of pasting a file and asking for advice. It swaps a slow, unpredictable conversation for a fast, boring, repeatable one, and boring is usually what you want in a build pipeline. If your codebase has grown a few 400-line components nobody wants to touch, this is worth ten minutes of your time.
Head over to the original Reddit thread to grab the repo link and the docs, and see what this savvy professional builds next. ⚓
Frequently Asked Questions
Q: What exactly does chemx refactor, and why were the proof-of-concept examples unclear?
Great question, that feedback is fair. chemx doesn’t rewrite code itself; it scans your codebase locally using AST analysis to find architectural violations (files over 100 lines, nested conditionals, type bloat), then generates a surgical prompt that tells Claude or your LLM exactly what to refactor and where. You paste that prompt into Claude, it makes the changes, and you see the diff. The GitHub examples could definitely show before/after code more explicitly, opening an issue asking for detailed refactoring examples would help the project improve that.
Q: What does “PASSED” mean when I run chemx audit?
“PASSED” simply means your code now satisfies the architectural constraints chemx was checking for. Run `chemx audit` again on your refactored code; if the violations disappear, it passed. It’s a straightforward go/no-go signal that the rules were met.
Q: What specific violations does chemx detect?
chemx looks for common architectural debt: files exceeding your length cap (e.g., 100 lines, too large to reason about), nested conditionals stacked three levels deep (should be atomic booleans), and type definitions that try to do too much. These checks run locally in milliseconds at zero cost; they’re just static AST analysis, no LLM involved.
Q: How much money and tokens actually get saved?
Traditional code-review prompts spend 20, 30% of input tokens on LLM-powered discovery before generating fixes. chemx offloads discovery to your laptop (free, instant), so every token goes toward actual refactoring. At typical Claude pricing, that’s roughly $0.30, $0.50 saved per file, and the savings compound across large codebases. You also get deterministic, repeatable refactoring instead of random hallucinations.
Stop Asking LLMs to Find Code Smells: Auto-Generating Surgical Prompts via AST
by u/DoItForTheXP in PromptEngineering