Skip to content
Claude Code cluster · Build

Prompt Engineering for Magento 2 Devs Using Claude Code

Prompt engineering for Magento with Claude Code is mostly about three things: naming the files and the symptom, saying what done looks like, and giving Claude a check it can run. This page has a prompt builder and four worked examples, each with the prompt, why every line is there and what to check in the result.

builder inputs
7
worked examples
4
no signup
Free
Topic 8 of 11 Build Free. No signup, no API key.
PROMPT BUILDER

Prompt engineering lab

Pick a goal, scope and constraints. The builder assembles a Claude Code prompt in the shape I use for Magento work. Treat it as a first draft: add the file names and the symptom, then read what Claude does with it.

Try a preset
01 · Goal What do you want Claude to do?
02 · Scope Where should Claude touch?
03 · Magento Target version
04 · Constraints Non-negotiables (uncheck to relax)
05 · Output format How should Claude respond?
06 · Verbosity How long should the answer run?
07 · Sub-agents Your own subagents in .claude/agents/ (Claude Code does not ship these names)
WORKED EXAMPLES

Four prompts, line by line

Each example is a prompt for a real kind of Magento task, the reason for each part, and what you still have to check yourself. Module names are examples; swap in your own paths.

Example 1

Fix a bug with a failing test first

Prompt
Customers report that the delivery note they type at checkout is empty on the admin order view.
Look at app/code/Panth/DeliveryNote/Observer/SaveDeliveryNote.php and etc/events.xml.
First explain where the value is lost. Do not edit yet.
Then write a PHPUnit test in app/code/Panth/DeliveryNote/Test/Unit that fails because of this bug, fix the bug, and run:
vendor/bin/phpunit app/code/Panth/DeliveryNote/Test/Unit
Show me the command and its output. Do not touch vendor/.

Why each part is there

  • Symptom and files Naming the file and the symptom gives Claude what it cannot find on its own, which is what the Claude Code best-practices page recommends.
  • Explain before editing A short diagnosis first shows whether it understood the bug before it changes code.
  • A check it can run The PHPUnit path is the definition of done. Claude can run it, read the failure and try again.

Check: the test failed before the fix and passes after it, and the diff stays inside the module.

Example 2

Follow a pattern that already exists

Prompt
@app/code/Panth/Loyalty/view/adminhtml/ui_component/panth_loyalty_rule_listing.xml is our existing rule grid.
Build the same kind of admin grid for the panth_loyalty_points table: listing XML, data provider, controller,
ACL resource Panth_Loyalty::points and a menu entry under the existing Loyalty menu.
Plan first: list every file you will create or change, then wait for my go.
No new libraries. After writing, run php -l on each new PHP file.

Why each part is there

  • @ includes the file Typing @ and a path puts that file in the prompt, so Claude copies your structure instead of a generic one.
  • Plan, then wait For a change across many files, a file list you approve is cheaper to fix than a finished diff.
  • Limits stated once "No new libraries" and the ACL name are choices Claude cannot read from the code.

Check: the ACL resource exists in etc/acl.xml, the controller checks it, and the grid loads after setup:upgrade on your local copy.

Example 3

Port a Luma template to Hyvä

Prompt
Port app/design/frontend/Panth/default/Magento_Catalog/templates/product/view/stock-alert.phtml to Hyvä.
It uses a RequireJS widget through data-mage-init. Replace it with an Alpine.js x-data component and Tailwind classes.
Our theme is Hyvä 1.4, which uses Tailwind CSS v4. Read the theme's web/tailwind folder before choosing classes.
Keep the same block and data. Do not edit vendor/hyva-themes.
Then run npm run build in app/design/frontend/Panth/default/web/tailwind and tell me if it fails.

Why each part is there

  • Version stated Hyvä 1.4 and later ship Tailwind v4; 1.2 and 1.3 ship v3. Saying which one avoids classes for the wrong version.
  • Old and new pattern named "RequireJS widget to Alpine.js x-data" says exactly what changes and what stays.
  • Build as the check npm run build fails on broken Tailwind input, so Claude sees the error itself.

Check: open the product page in a browser. A passing build does not prove the component behaves the same.

Example 4

Review a branch without editing

Prompt
Use a subagent to review git diff main...HEAD for Magento problems:
ObjectManager used outside Setup or Test code, raw SQL where db_schema.xml should be used,
new admin controllers without an ACL check, and unescaped output in .phtml files.
Report each finding as file:line and one sentence. Skip style. Do not edit any file.

Why each part is there

  • A fresh context A subagent did not watch the code being written, so the reasoning that produced it does not sway the review.
  • A fixed list Four named problems give a short, checkable report instead of a general opinion.
  • Output format file:line per finding is easy to verify and paste into a pull request.

Check: open each file:line yourself. A review prompt finds candidates; it does not prove there are no other problems.

FAQ

Common questions

How much Magento context can I cram into one prompt before it falls over?

There is no fixed safe number. Context limits depend on the model, and a long session fills up with everything read so far. Name the files that matter and type @ with a path to include them, point at an existing class as the pattern to follow, and let Claude read the rest when it needs it. For a long search or review, use a subagent so that work happens in its own context window.

How does CLAUDE.md "anchoring" work in practice?

CLAUDE.md is loaded at the start of every session, so specific rules in it shape what Claude does, but it is context, not enforcement. Concrete rules work better than vague ones: “never edit vendor/”, “use db_schema.xml, not raw SQL”, “plugins over preferences”, plus the commands that check the work. Keep it under 200 lines. For anything that must never happen, add a deny rule or a PreToolUse hook as well.

Does role priming ("you are a senior Magento dev") actually help?

It can. Role prompting is one of the techniques in Anthropic’s prompting guide. For a Magento task, the task itself matters more: name the file and the symptom, say what done looks like, and give Claude a check to run, for example php -l, a PHPUnit path or phpcs --standard=Magento2, then ask for the command and its output. A role fits well in a subagent, such as a read-only reviewer.

What is an "output contract" in a prompt?

It is a fixed shape for the answer that you can check. For a scaffold you might ask for the file list first, then each file in its own block with its path on the first line, and no other text. That makes the result easy to review or to pass to a script. It does not make the code correct, so still run a check such as php -l on each PHP file.

What does an "eval loop" look like for Magento prompts?

A loop of write, check and fix. Decide what success looks like first, then give Claude a check it can run: php -l, a PHPUnit path, phpcs --standard=Magento2 or a build exit code. Ask it to run the check and show the command and its output before it says the task is done, and to fix and rerun when it fails. A PostToolUse hook can run php -l after every PHP edit. If the same mistake keeps coming back, add a rule to CLAUDE.md or a hook.

Kishan Savaliya

Need the module shipped, not just learned?

I build Magento modules with Claude Code, tested and ready for review. Adobe Certified since 2021.

Claude Code for Magento Free prompts and tools Ask me