Refactoring code with AI in agent mode: a practical guide - Datatoy
Datatoy Logo
🇬🇧 vibecodingCursorrefactoring

Refactoring code with AI in agent mode: a practical guide

October 29, 2025

Refactoring code with AI in agent mode

Lessons learned 7/11 on Cursor after 15 years in software

AI in agent mode is a great way to refactor code, across several themes. Here are the prompts that work.

Adding types

"This Python code is untyped, add types. Don't hesitate to add Pydantic classes you deem necessary."

Changing paradigms

"This file uses a functional syntax, switch paradigms to object-oriented. My main object will be called [...] and methods [...] will be static. Search the rest of the repo to update usage of these functions."

Trimming the fat

"This file is too verbose, remove the fluff. Factor similar structures."

Maintaining

"Add logs to this file on the main results."

Commenting (or not)

"This file's comments are outdated, update them."

or

"Remove the obsolete comments."

Simplifying

"Classes XX and YY seem to have redundant methods, harmonize the code to keep as few as possible."

Improving readability

"Variable x is not descriptive, find its purpose and rename it appropriately."

Why it works so well

Refactoring is the ideal use case for AI in agent mode. The code already exists, the expected behavior is defined, and the task is mechanical. The agent can browse the repo, understand dependencies and apply changes consistently.

It's the kind of work nobody wants to do by hand, but that's essential for a project's maintainability. With a good prompt, you can clean up in minutes what would have taken hours.


Originally published on LinkedIn.