AI agents

Real-world examples

Example: Refactor a large module

Prompt examples are illustrative; commands are verified against official docs.

Scenario: billing_service.py has grown to ~1,500 lines and needs splitting. Large refactors fail when the agent starts editing immediately. The pattern: plan first, change incrementally, run tests between steps.

Workflow

  1. Map the module’s responsibilities and dependents.
  2. Enter plan mode / read-only mode and agree on a split strategy before any edit.
  3. Extract one responsibility at a time.
  4. Run the test suite between every step; stop on red.
  5. Review the full diff at the end.

Claude Code

Start in plan mode so exploration stays read-only:

claude --permission-mode plan
Analyze billing_service.py and its dependents. Propose a split into focused
modules (invoicing, payments, refunds). The public API must not change.
Iterate on the plan with me before touching any file.

Approve the plan to exit plan mode, then enforce the incremental loop by prompt:

Execute the plan one step at a time. After each extraction run
`pytest tests/billing/` — if anything fails, stop and report instead of
continuing.

The acceptEdits permission mode (Shift+Tab cycles modes) auto-accepts file edits while still prompting for other tools — useful during a long mechanical refactor. /rewind rolls back code and conversation to a checkpoint if a step goes wrong.

End with /diff and /code-review:

/code-review medium

Codex

Explore with a read-only sandbox first (the Read Only preset):

codex --sandbox read-only --ask-for-approval on-request
Map billing_service.py: its responsibilities, public functions, and every
file that imports it.

Then switch into plan mode:

/plan Design a split of billing_service.py into invoicing, payments, and
refunds modules. Preserve the public API. Order the steps so the test suite
stays green after each one.

Implement under workspace-write, one verified step at a time:

codex --sandbox workspace-write --ask-for-approval on-request
Execute the plan incrementally: one extraction per step, run
`pytest tests/billing/` after each, and stop on any failure.

Review the accumulated diff with /diff, then /review the working tree (or codex review --uncommitted from the shell).

Kimi Code

Start in Plan mode — Write/Edit are restricted to the plan file until you approve the plan:

kimi --plan
Analyze billing_service.py and everything that imports it. Write a plan to
split it into invoicing, payments, and refunds modules without changing the
public API. Steps must be individually testable.

Approving the plan (ExitPlanMode always asks for confirmation) unlocks edits. Then work incrementally — Ask When Needed mode keeps routine edits and test runs from prompting:

/yolo
Execute the plan one extraction at a time. Run `pytest tests/billing/`
after every step; on failure, stop and show me the output.

Check the diff along the way via shell mode:

!git diff --stat

Note: Kimi Code’s /undo rolls back prompts and plan state, not code changes — rely on git for reverting edits (e.g. !git checkout -- <file>).


Last verified: 2026-09-07

AI coding tools evolve quickly. If you find outdated information, please open an issue or pull request.