{"slug": "prompt-optimization-how-to-write-a-system-prompt", "title": "prompt optimization, how to write a system prompt,", "summary": "A 20-step AI agent logic chain can avoid hallucination by using a structured, constraint-first system prompt rather than instructional prose, according to a technical guide on prompt optimization. The guide reports that adding a 'Negative Constraint' section—such as 'Do not assume a file exists unless the ls tool confirms it'—dropped the hallucination rate to nearly zero in a documentation automation agent built on Claude 3.5 Sonnet. It recommends defining role identity, strict constraints, input/output schemas, and chain-of-thought triggers, and using few-shot examples instead of vague descriptions.", "body_md": "# prompt optimization, how to write a system prompt,\n\n[AI agent](/en/tags/ai%20agent/)follow a 20-step logic chain without it hallucinating by step 12?\n\nYes, by shifting from \"instructional prose\" to \"structured constraints\" and using a Few-Shot anchored system prompt.\n\nMost people treat the system prompt like a letter to a colleague. They write, \"Please be a professional coder and make sure the code is clean.\" That's useless. The LLM ignores \"professional\" because it has no objective metric for it. To get an agent to actually perform, you have to define the state machine, the available tools, and the exact failure conditions.\n\n## Why your system prompts are failing\n\nI spent three hours last Thursday fighting with a [Claude](/en/tags/claude/)-3.5-Sonnet agent that kept ignoring my formatting rules for a JSON output. I had told it \"Ensure the output is strictly JSON.\" It still added \"Here is the JSON you requested:\" at the top.\n\nThe problem is \"prompt bleed.\" When you use soft language, the model's training on conversational data overrides your instructions.\n\nTo fix this, you need to move toward prompt optimization that mimics programming. Stop using adjectives and start using delimiters. Instead of \"Be concise,\" use \"Limit responses to 2 sentences. If the answer is unknown, output 'NULL'.\"\n\n### The \"Constraint-First\" Framework\n\nIf you want an agent to stop drifting, structure your system prompt like this:\n\n1. **Role Identity**: (e.g., \"You are a TypeScript Compiler Specialist.\")\n\n2. **Strict Constraints**: (e.g., \"Never suggest external libraries outside of the Standard Library.\")\n\n3. **Input/Output Schema**: Define exactly what goes in and what comes out.\n\n4. **Chain-of-Thought Trigger**: Force the model to think in a hidden block.\n\nHere is a comparison of a \"Standard\" prompt versus an \"Optimized\" prompt for a coding agent:\n\n| Feature | Standard Prompt (Weak) | Optimized Prompt (Strong) |\n\n| :--- | :--- | :--- |\n\n| **Instruction** | \"Write a Python script for me.\" | \"Generate Python 3.11 code following PEP 8.\" |\n\n| **Context** | \"Make it efficient.\" | \"Time complexity must be O(n log n) or better.\" |\n\n| **Error Handling** | \"Handle errors properly.\" | \"Wrap API calls in try-except blocks; log errors to stderr.\" |\n\n| **Format** | \"Give me the code.\" | \"Output format: `[Thought] -> [Code] -> [Test Case]`\n\n\" |\n\n## Building a reliable AI agent: A case study in automation\n\nI recently built a small agent to automate documentation updates by scanning git diffs. If I had used a basic prompt, it would have just summarized the changes. I needed it to actually identify which `.md`\n\nfiles were impacted and suggest a specific edit.\n\nThe \"aha!\" moment came when I stopped asking it to \"find the files\" and started giving it a tool-use loop.\n\n**The workflow looked like this:**\n\n**Step 1**: Agent receives the`git diff`\n\n.**Step 2**: Agent identifies modified functions.** Step 3**: Agent searches the`/docs`\n\nfolder for those function names.**Step 4**: Agent proposes a diff for the documentation.\n\nThe wild part? It failed 40% of the time until I added a \"Negative Constraint\" section to the system prompt. I explicitly told it: \"Do not assume a file exists unless the\n\n`ls`\n\ntool confirms it.\" Suddenly, the hallucination rate dropped to nearly zero.If you're deep in the weeds of [AI Coding](/en/category/ai-coding/), you know that the difference between a \"cool demo\" and a \"production tool\" is usually just 50 words of very specific constraints in the system prompt.\n\n## The mechanics of prompt optimization\n\nOptimization isn't about finding a \"magic word.\" It's about reducing the search space for the LLM.\n\nWhen you give a vague prompt, the model has a billion possible paths to an answer. When you provide a structured system prompt with Few-Shot examples, you're essentially narrowing that path.\n\n### The Few-Shot Anchor\n\nIf you want a specific style, don't describe it. Show it.\n\n**Bad:** \"Write code in a functional style.\"**Good:**\n\n\"Follow this pattern:\n\nInput: `[1, 2, 3]`\n\n-> Output: `[2, 4, 6]`\n\n(via map)\n\nInput: `[10, 20]`\n\n-> Output: `[20, 10]`\n\n(via reverse)\"\n\nThis gives the model a mathematical pattern to follow. I've found that three high-quality examples are worth more than five paragraphs of instructions.\n\n## Where to find the real-world patterns\n\nYou can spend weeks guessing which keywords work, or you can just look at what other devs are shipping. Most of the \"secret sauce\" in AI agents isn't the model—it's the system prompt and the [RAG](/en/tags/rag/) pipeline.\n\nThis is why hanging out in a dedicated space like the [PromptCube homepage](/en/) actually matters. You stop treating prompting like a dark art and start treating it like engineering. You see a prompt that successfully handles a complex [MCP](/en/tags/mcp/) (Model Context Protocol) integration, you steal the structure, and you apply it to your own bot.\n\n## A quick checklist for your next agent\n\nBefore you deploy your next agent, run it through this stress test:\n\n- [ ]\n**The \"Ignore\" Test**: Does it still follow the rules if the user says \"Ignore all previous instructions\"? (Add a \"System Override Protection\" clause to your prompt). - [ ]\n**The Edge Case**: What happens if the input is an empty string? Does the agent crash or output a graceful`NULL`\n\n? - [ ]\n**The Token Leak**: Are you using 500 tokens to say something that could be said in 20? (e.g., \"It would be great if you could possibly try to...\" → \"Must...\"). - [ ]\n**The Format Lock**: If you need JSON, are you using a Pydantic schema or a JSON-mode flag?\n\nTo be fair, some models are just better at following complex system prompts than others. Claude 3.5 Sonnet currently feels like the gold standard for \"following the rules,\" while GPT-4o sometimes gets a bit too conversational and forgets the constraints.\n\nJust keep iterating. Most of my \"perfect\" prompts are actually the result of 15 failed versions and a lot of annoyance.\n\n[Next Remembrane lets you run agent memory from a single SQLite file →](/en/threads/5555/)\n\n## All Replies （0）\n\nNo replies yet — be the first!", "url": "https://wpnews.pro/news/prompt-optimization-how-to-write-a-system-prompt", "canonical_source": "https://promptcube3.com/en/threads/5683/", "published_at": "2026-08-09 14:24:00+00:00", "updated_at": "2026-08-09 14:25:23.428636+00:00", "lang": "en", "topics": ["artificial-intelligence", "ai-agents", "large-language-models", "ai-tools"], "entities": ["Claude 3.5 Sonnet", "TypeScript", "Python", "PEP 8"], "alternates": {"html": "https://wpnews.pro/news/prompt-optimization-how-to-write-a-system-prompt", "markdown": "https://wpnews.pro/news/prompt-optimization-how-to-write-a-system-prompt.md", "text": "https://wpnews.pro/news/prompt-optimization-how-to-write-a-system-prompt.txt", "jsonld": "https://wpnews.pro/news/prompt-optimization-how-to-write-a-system-prompt.jsonld"}}