How to prompt AI coding agents: give Claude Code, Codex, and Cursor the information they are missing

Translated from the original Traditional Chinese version.

Summary

When Claude Code, Codex, or Cursor keeps introducing new bugs while fixing old ones, or edits files you never asked about, the agent is usually missing information and filling the gap with a plausible guess, which is what people call a hallucination. Before you write a prompt, work out what the agent is missing and what you are missing yourself. Once those gaps are filled, you can set a goal and let the agent finish the work on its own.

Table of contents

A common problem with AI coding agents such as Claude Code, Codex, and Cursor is that fixing one bug introduces another. When the problems keep piling up, the agent is usually missing some key information. An AI model that lacks information does not always stop to ask. It often fills the gap with an answer that only looks reasonable, which is what people call a hallucination. If you let the agent keep fixing in that state, the problems spread faster than it can fix them. The way out is to supply the missing information when you write the prompt.

Think of it as a client and a contractor. When the client cannot say what they want, the contractor has to read the client’s mind, and the result often misses the brief or simply has problems. When you build with AI, you are the client and the AI is the contractor. The same applies to you. If you never try to understand the system the AI built, it becomes a black box. When something breaks, you cannot tell which part to suspect, so all you can do is hand the whole thing back to the AI and let it guess again.

The method in this article has one principle: when you prompt, make sure both the AI and you know the why behind the work. If the AI clearly knows the facts, what you want, and where the problem is, it guesses far less. I went back through the more than 2,000 prompts I wrote in Claude Code over three months, and most of them supplied information that either the AI or I was missing. They come down to five things you, as the client, need to spell out, listed in the order they usually come up during development. Find the situation you are in, and the table shows what the AI is guessing and what you need to supply:

Situation What the AI is guessing What to supply
The result is not what you pictured What the finished result looks like State the goal and the reason
The AI wrote a lot of code in the wrong direction Which approach to take Ask for a proposal before any code
The AI edited files you never mentioned, or asks at each step What it may change and when to ask Set the scope and permissions
Each bug fix introduces a new bug Where the problem is Reproduce the problem first
The AI says it is done but shows no test output What counts as done Ask the AI for evidence

The prompts in this article are examples you can adapt and paste into the input box of Claude Code, Codex, or Cursor. They are plain sentences, and the only command you will see is the one for compacting a conversation.

State the goal and why

When the result is not what you pictured, it is usually because you only said “build X” and never described what done looks like, so the AI had to guess. Describe the goal from the user’s point of view: what someone can see when the work is finished, with numbers where you have them, and the reason behind it. For example:

1I want code on article pages to be readable on phones, because most readers read on their phones.
2The goal: on a 375px-wide screen, code blocks scroll horizontally and do not break the layout.

When the AI knows the reason, it can usually follow the logic and make sensible decisions on its own. When I was tuning the performance of StreamTurbo, my tool that regenerates TouchDesigner video frames with AI in real time, I added: “Later the output will be upscaled to 1024 pixels or larger for projection, so image quality cannot drop too far. 20 to 30 frames per second is the target.” With that, the AI did not trade image quality for speed, and it did not stop to ask me before every decision. If the features have priorities, say which ones you need now, which can wait, and which are nice to have. The AI is then more likely to make the decisions you expect.

If you have not worked out what you want yet, flip it around and let the AI ask the questions. The Claude Code best practices also suggest letting Claude interview you until it can write a spec, and only then starting the implementation:

1Ask me questions until you can write a spec.

Ask for a proposal before any code

An AI that jumps straight into coding and only later turns out to be heading the wrong way usually faced a request with two or more reasonable approaches, or a change that touches many files. By starting right away, the AI picks the direction for you. If the direction is wrong, the more it writes, the harder it is to back out, so ask for a proposal first. A model’s knowledge also stops at its training date, and it may keep using an outdated approach after a tool has changed, so have it check the official documentation before it proposes anything. For example:

1Do not change any files yet.
2Read the official documentation for the current version of this package, and include the URLs and version numbers you used.
3Then propose two or three options, list the pros and cons of each, give your recommendation, and draw a diagram so I can choose.

After you read the options, a reply such as “go with your recommendation” or the name of one option is enough. Uncommon technologies have less material to look up when something breaks, and the AI is more likely to guess wrong about them. When several options would work, pick the one the industry commonly uses.

You can also ask the AI to draw the system as an architecture diagram during the proposal stage, so that you have a rough picture of what the system looks like. The section “Ask the AI to draw an architecture diagram” shows how.

Set the scope and permissions

An AI that edits files you never asked about, or stops at every step to ask whether it may continue, is guessing the scope because nobody told it what it may touch. Before it starts, put three things in the prompt: what it must not touch, what it already has permission to do, and what counts as done. For example:

1Only change the src/api folder. Do not touch the tests.
2You may search the web. Ask me before installing any new package.
3After the change, run the tests, and commit only if they pass.

The commit you allow in that prompt is saved only in your local Git history. Nothing reaches the remote until you push. Put scope that applies only to this task in the prompt, and put rules the AI should follow every time in a rules file: CLAUDE.md for Claude Code, AGENTS.md for Codex, and project rules in .cursor/rules for Cursor.

Reproduce the problem first when a fix will not stick

When each bug fix introduces a new bug, the AI usually has not seen where the problem really is. It often sees only the error message in front of it, so it guesses at a cause and patches some code. The patch may break something else, and the AI has to guess again. It is like a client who only says “this version is wrong” without saying what is wrong, so the contractor keeps guessing one version after another.

Reproduce the problem, then check the design

At this point, ask the AI to stop fixing and reproduce the problem first. Reproducing means finding steps that fail every time, ideally written as a failing test. If you cannot reproduce a problem, you have no way to confirm the AI’s fix actually worked.

Once the problem reproduces reliably, make it measurable, which means turning it into numbers or results you can see. For example, have the code print the actual values to a log, the record a program writes while it runs, or write a failing test that shows the expected value next to the actual one. For performance problems, measure frames per second or response time.

After the problem is reproducible and measurable, have the AI zoom out, which means stepping back to check the overall design: how the modules divide the work and whether the boundaries between them make sense. In my experience, when new problems keep appearing, the root cause is usually in the design direction or in the boundaries between modules. Changing code at that point only treats the symptoms, and each patch adds another layer that makes the problems worse. In the diagram, the left side is the guessing loop and the right side is the order to follow instead:

On the left, the guessing loop: an error appears, the AI guesses a cause and patches some code, something else breaks, and the loop returns to the error. On the right: stop, reproduce the problem and add logs to measure it, zoom out to check the design and find the root cause, then propose a fix and change code only after approvalOn the left, the guessing loop: an error appears, the AI guesses a cause and patches some code, something else breaks, and the loop returns to the error. On the right: stop, reproduce the problem and add logs to measure it, zoom out to check the design and find the root cause, then propose a fix and change code only after approval

You can turn the order on the right into a single prompt. For example:

1Stop. Do not fix anything else yet.
2First find steps that reproduce this problem reliably, and write them as a failing test if you can.
3Then add logging to measure the problem, and paste the printed values here.
4Finally, zoom out and check whether the overall design makes sense. Find the root cause, propose a fix, and wait for my approval before changing anything.

Ask the AI to draw an architecture diagram

To find where a problem is, you need a rough picture of the system yourself. When you design a system with AI today, you do not need to know how every function is called, but you must be able to answer four questions. The table uses a typical CMS website and StreamTurbo as examples:

Questions you should be able to answer A typical CMS website StreamTurbo
Where is the data stored? In a database Settings in last-settings.toml
How does the data move? The front end talks to the back end via an API Images via Spout or NDI, settings via OSC
Where does it run, and with what limits? On a cloud server Windows with an NVIDIA GPU
What is the core technology? A web framework Generation with sd-turbo, sped up by TensorRT

You can ask the AI to propose a design and draw it as an architecture diagram:

1Propose an architecture for this system and draw it as an architecture diagram.
2Show where the data is stored, how the data moves, where it runs and with what limits, and what the core technology is.
3Then describe each component in one sentence.

Take StreamTurbo as an example. TouchDesigner is a visual programming tool for real-time graphics, and StreamTurbo takes the frames TouchDesigner sends, regenerates them in real time, and sends them back. Spout and NDI are two ways to pass live video between programs. Spout only works within the same computer, while NDI can send frames over the network. OSC carries control messages such as settings and prompts:

TouchDesigner sends frames to StreamTurbo through Spout or NDI. StreamTurbo generates with sd-turbo, upscales with the Upscaler, and sends the result back. Settings come from TouchDesigner through OSC, the Web Panel in a browser edits the same settings, and they are saved to last-settings.toml on exitTouchDesigner sends frames to StreamTurbo through Spout or NDI. StreamTurbo generates with sd-turbo, upscales with the Upscaler, and sends the result back. Settings come from TouchDesigner through OSC, the Web Panel in a browser edits the same settings, and they are saved to last-settings.toml on exit

With this diagram, if GPU usage shows that generation is running but TouchDesigner receives no frames, you can tell the problem is probably in frame transport. Then check whether Spout is crossing computers, or whether the NDI network settings are right. That lets you write a precise prompt that asks the AI to fix only the frame transport. You can also let the AI analyze the problem on its own, but it may head in the wrong direction.

Ask the AI for evidence

An AI that says “done” or “tests pass” without showing any output was usually never asked for evidence, so you cannot tell whether the tests actually ran. A client accepting a delivery looks at the result instead of taking the contractor’s word for it, so make evidence part of what counts as done, and set the scope of the tests. For example:

1Only test the code we wrote, and use fake data in place of external services.
2When you are done, paste the test command and its output.

Without a scope, the AI may test third-party packages or call real external APIs, and the tests become slow and flaky. If you cannot follow its report, add a line such as “Answer concisely, in full sentences, without heavy abbreviations.”

In my experience, an AI that wrote the code tends to be convinced by its own reasoning, and when there are many items to check, it often looks at a few and reports that everything is fine. When a change touches many files, have the agent start a subagent, a separate AI that has not seen this conversation, or open a new conversation yourself, and have it review every item:

1Start a subagent to review this change. It should read only and change nothing.
2Check every file one by one instead of sampling, and list the problems with their severity.

Let the AI finish once the gaps are filled

Once all five are covered, and the project is under Git so you can undo a bad change, you no longer need to approve every step. You can set a goal and give the AI the permissions to finish on its own. To keep the AI away from things it should not touch, such as sensitive files like .env, see Claude Code deny rules and using hooks to block pushes to main.

The first prompt I gave the AI when starting StreamTurbo already covered the first three: the goal and the reason, checking sources before proposing, and the scope and permissions.

1This is a fork of StreamDiffusion, and upstream is no longer maintained. Read the StreamDiffusion paper and the original design first.
2It only runs on Windows, and should work in one step after a clone.
3For now, get NDI input and output working. It will connect to TouchDesigner later, and I would also like a GUI.
4You may install and update CUDA, Python, and PyTorch, and search the web for official resources and papers.
5Raise any other questions, then survey, investigate, and analyze before you propose.
6Use diagrams, lists, and tables in the proposal instead of long paragraphs.

After I approved the proposal, I added one more prompt that let the AI take over, and it planned the remaining details on its own:

1Plan any details we have not covered yourself. Use the standard approach, and only ask me about decisions that really need me.

The standard approach here means what the official documentation describes, or what the industry commonly uses.

When the AI runs for a long time, the conversation keeps growing. Once the context window, the amount of conversation the AI can take in at once, gets too full, the tool compacts the conversation automatically, and the generated summary may drop decisions you care about. So when the context window is about half full, use a quiet moment to have the AI write the compact command for a handoff, then copy it and send it yourself:

1Give me a compact command that keeps the current decisions, the files we changed, and the next steps.

The AI replies with a line such as /compact keep .... Paste it into the input box and send it, and the compacted conversation keeps that information.

Summary

When an AI keeps making things worse, it often means you, the client, left something out. Before you prompt, think about what the AI is guessing and what you still do not understand, fill in that information, and only then let go. As a next step, pick a task you are working on, find the closest situation in the table at the top, write that missing piece as one sentence, and add it to your next prompt.

Ted Liou

Ted Liou

Software engineer writing about Unity and AI development.

Comments