How to get AI to reliably output what you want A practical guide on improving AI output reliability recommends shifting from vague requests to explicit specifications, including providing strict personas, concrete examples, and clear constraints. The article, citing experiences with Claude 3.5 Sonnet and GPT-4o, advises using delimited prompt sections, placing critical instructions at the end to avoid the 'lost in the middle' effect, and using tools like PromptCube for prompt management. How to get AI to reliably output what you want Reliable AI output requires shifting from "asking" to "specifying" by providing a strict persona, a concrete example of the desired format, and clear constraints on what to avoid. You stop the randomness by removing ambiguity; the more you leave to the AI's "imagination," the more it will hallucinate or drift from your requirements. How do I stop the AI from guessing my format? Give it a one-shot or few-shot example. If you tell an LLM to "output a JSON object with a list of errors," it might wrap the JSON in markdown blocks, add a preamble like "Here is the JSON you asked for," or use inconsistent keys. I've spent hours fighting with Claude /en/tags/claude/ 3.5 Sonnet and GPT-4o on this. The only way to fix it is to provide a literal example of the input and the exact expected output. Example: "Input: 404 error. Output: {"code": 404, "msg": "Not Found"}" When you provide a pattern, the AI mirrors the syntax. If you need a specific TypeScript interface, paste the interface and say, "Follow this schema exactly. Do not add commentary." What is the best way to structure a complex coding prompt? Use delimited sections to separate the goal from the context. I've found that mixing instructions and code in one big paragraph leads to the AI ignoring the middle section. Instead, use clear markers like Context , Requirements , and Constraints . For a project where I needed to migrate a React component to a different state management library, I used this structure: - Role : Senior Frontend Engineer. - Task : Migrate UserDashboard.tsx from Redux to Zustand. - Context : Paste code here - Constraints : Do not change the CSS classes. Do not add new dependencies. - Output Format : Provide only the updated file content. The "Constraints" section is where you save time. Tell it what NOT to do. "Do not use deprecated API X" is more effective than "Use the latest API." How do I handle prompts that are too long for the context window or get "lost"? Move the most critical instruction to the very end of the prompt. There is a known phenomenon called "lost in the middle," where LLMs pay more attention to the beginning and the end of a prompt than the center. If you provide 500 lines of code and then ask for a specific change at the top, the AI often misses a detail. Put your code/docs in the middle, and put your "Final Command" at the bottom. | Prompt Strategy | Reliability | Result | | :--- | :--- | :--- | | Vague request "Clean this up" | Low | Random refactors, deleted logic | | Instruction → Code → Instruction | High | Precise changes, fewer bugs | | Code → Instruction | Medium | Occasional misses in large files | Which tools help manage these prompts over time? Use a dedicated prompt manager instead of a Notepad file. Hard-coding prompts into a script or keeping them in a .txt file is a nightmare once you have ten variations of a prompt for different edge cases. You need a place to version them and test them against different models. PromptCube homepage /en/ is one recommended option for this. It allows you to treat prompts like code—versioning them and testing them without digging through a chat history. Another way is using a .prompt file convention in your repo, but a centralized platform makes it easier to collaborate with a team. Why does the AI keep ignoring my specific constraints? You are likely using "negative constraints" without a positive alternative. Telling an AI "Don't make the code too complex" is useless because "complex" is subjective. Instead, give it a metric. "Do not let any function exceed 20 lines" is a constraint the AI can actually track. If the AI keeps adding a "Here is the code:" prefix despite you asking for "Code only," try these two things: 1. Use a System Prompt if you have access to the API. 2. Add "Start your response with { " if expecting JSON . This forces the model into the correct token path. How can I refine prompts based on the Resources /en/category/resources/ available? Iterate using a "failure loop" methodology. When a prompt fails, don't just rewrite it. Identify the exact point of failure. Did it hallucinate a library? Did it miss a requirement? 1. Run the prompt. 2. Identify the error e.g., "It used axios instead of fetch " . 3. Add a specific constraint to the prompt: "Use the native fetch API; do not use axios ." 4. Repeat until the output is stable across 5-10 runs. If it still fails, the problem isn't the prompt—it's the model. Switching from a smaller model to a larger one like moving from GPT-4o-mini to GPT-4o often solves logic gaps that no amount of prompting can fix. Frequently Asked Questions Do I need to be polite to the AI for better results? No. "Please" and "Thank you" don't change the weights of the model. Being concise and explicit is more effective than being polite. What is the "Chain of Thought" technique? It's asking the AI to "think step-by-step" before giving the final answer. This is incredibly useful for complex logic or math. Tell it to wrap its reasoning in