{"slug": "how-to-get-ai-to-reliably-output-what-you-want", "title": "How to get AI to reliably output what you want", "summary": "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.", "body_md": "# How to get AI to reliably output what you want\n\nReliable 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.\n\n## How do I stop the AI from guessing my format?\n\nGive it a one-shot or few-shot example.\nIf 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.\n\nExample:\n\n\"Input: 404 error. Output: {\"code\": 404, \"msg\": \"Not Found\"}\"\n\nWhen 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.\"\n\n## What is the best way to structure a complex coding prompt?\n\nUse delimited sections to separate the goal from the context.\nI'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`.\n\nFor a project where I needed to migrate a React component to a different state management library, I used this structure:\n\n- **Role** : Senior Frontend Engineer.\n- **Task** : Migrate`UserDashboard.tsx` from Redux to Zustand.\n- **Context** : [Paste code here]\n- **Constraints** : Do not change the CSS classes. Do not add new dependencies.\n- **Output Format** : Provide only the updated file content.\n\nThe \"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.\"\n\n## How do I handle prompts that are too long for the context window or get \"lost\"?\n\nMove the most critical instruction to the very end of the prompt.\nThere 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.\n\nPut your code/docs in the middle, and put your \"Final Command\" at the bottom.\n\n| Prompt Strategy | Reliability | Result |\n\n| :--- | :--- | :--- |\n\n| Vague request (\"Clean this up\") | Low | Random refactors, deleted logic |\n\n| Instruction → Code → Instruction | High | Precise changes, fewer bugs |\n\n| Code → Instruction | Medium | Occasional misses in large files |\n\n## Which tools help manage these prompts over time?\n\nUse a dedicated prompt manager instead of a Notepad file.\nHard-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.\n\nAnother way is using a `.prompt` file convention in your repo, but a centralized platform makes it easier to collaborate with a team.\n\n## Why does the AI keep ignoring my specific constraints?\n\nYou are likely using \"negative constraints\" without a positive alternative.\nTelling 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.\n\nIf the AI keeps adding a \"Here is the code:\" prefix despite you asking for \"Code only,\" try these two things:\n\n1. Use a System Prompt if you have access to the API.\n\n2. Add \"Start your response with `{`\" (if expecting JSON). This forces the model into the correct token path.\n\n## How can I refine prompts based on the [Resources](/en/category/resources/) available?\n\n Iterate using a \"failure loop\" methodology.\nWhen a prompt fails, don't just rewrite it. Identify the exact point of failure. Did it hallucinate a library? Did it miss a requirement?\n\n1. Run the prompt.\n\n2. Identify the error (e.g., \"It used `axios` instead of `fetch`\").\n\n3. Add a specific constraint to the prompt: \"Use the native `fetch` API; do not use `axios`.\"\n\n4. Repeat until the output is stable across 5-10 runs.\n\nIf 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.\n\n## Frequently Asked Questions\n\n**Do I need to be polite to the AI for better results?**\n\nNo. \"Please\" and \"Thank you\" don't change the weights of the model. Being concise and explicit is more effective than being polite.\n\n**What is the \"Chain of Thought\" technique?**\n\nIt'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 `<thinking>` tags so you can see the logic without it cluttering the final code.\n\n**Does the temperature setting affect reliability?**\n\nYes. For coding and data extraction, set the temperature to 0 or 0.2. High temperature (0.7+) introduces creativity, which is the enemy of reliability in programming.\n\n**Should I use a single giant prompt or multiple small ones?**\n\nBreak it up. \"Chain of prompts\" (passing the output of one prompt into the next) is far more reliable than one \"God Prompt\" that tries to handle architecture, implementation, and testing all at once.\n\n[Next Why AI coding agents make verification the most expensive part of the loop →](/en/threads/9102/)\n\n[a practical ChatGPT prompt guide](https://tanyan888.com/), with plenty of directly applicable cases.", "url": "https://wpnews.pro/news/how-to-get-ai-to-reliably-output-what-you-want", "canonical_source": "https://promptcube3.com/en/threads/9121/", "published_at": "2026-09-09 21:38:56+00:00", "updated_at": "2026-09-09 22:17:30.260678+00:00", "lang": "en", "topics": ["artificial-intelligence", "large-language-models", "ai-tools", "ai-products"], "entities": ["Claude 3.5 Sonnet", "GPT-4o", "PromptCube"], "alternates": {"html": "https://wpnews.pro/news/how-to-get-ai-to-reliably-output-what-you-want", "markdown": "https://wpnews.pro/news/how-to-get-ai-to-reliably-output-what-you-want.md", "text": "https://wpnews.pro/news/how-to-get-ai-to-reliably-output-what-you-want.txt", "jsonld": "https://wpnews.pro/news/how-to-get-ai-to-reliably-output-what-you-want.jsonld"}}