prompt optimization, how to write a system prompt,

Morgan80 Advanced 6h ago 310 views 3 likes 5 min read

Can I actually make an AI agent follow a 20-step logic chain without it hallucinating by step 12?

Yes, by shifting from "instructional prose" to "structured constraints" and using a Few-Shot anchored system prompt.

Most 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.

Why your system prompts are failing

I spent three hours last Thursday fighting with a 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.

The problem is "prompt bleed." When you use soft language, the model's training on conversational data overrides your instructions.

To 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'."

The "Constraint-First" Framework

If you want an agent to stop drifting, structure your system prompt like this:

1. Role Identity: (e.g., "You are a TypeScript Compiler Specialist.")
2. Strict Constraints: (e.g., "Never suggest external libraries outside of the Standard Library.")
3. Input/Output Schema: Define exactly what goes in and what comes out.
4. Chain-of-Thought Trigger: Force the model to think in a hidden block.

Here is a comparison of a "Standard" prompt versus an "Optimized" prompt for a coding agent:

| Feature | Standard Prompt (Weak) | Optimized Prompt (Strong) |
| :--- | :--- | :--- |
| Instruction | "Write a Python script for me." | "Generate Python 3.11 code following PEP 8." |
| Context | "Make it efficient." | "Time complexity must be O(n log n) or better." |
| Error Handling | "Handle errors properly." | "Wrap API calls in try-except blocks; log errors to stderr." |
| Format | "Give me the code." | "Output format: [Thought] -> [Code] -> [Test Case]" |

Building a reliable AI agent: A case study in automation

I 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 files were impacted and suggest a specific edit.

The "aha!" moment came when I stopped asking it to "find the files" and started giving it a tool-use loop.

prompt optimization, how to write a system prompt, AI agent case studies

The workflow looked like this:

  • Step 1: Agent receives the git diff.
  • Step 2: Agent identifies modified functions.
  • Step 3: Agent searches the /docs folder for those function names.
  • Step 4: Agent proposes a diff for the documentation.
prompt optimization, how to write a system prompt,

The 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 ls tool confirms it." Suddenly, the hallucination rate dropped to nearly zero.

If you're deep in the weeds of 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.

The mechanics of prompt optimization

Optimization isn't about finding a "magic word." It's about reducing the search space for the LLM.

When 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.

The Few-Shot Anchor


If you want a specific style, don't describe it. Show it.

Bad: "Write code in a functional style."
Good:
"Follow this pattern:
Input: [1, 2, 3] -> Output: [2, 4, 6] (via map)
Input: [10, 20] -> Output: [20, 10] (via reverse)"

This gives the model a mathematical pattern to follow. I've found that three high-quality examples are worth more than five paragraphs of instructions.

Where to find the real-world patterns

You 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 pipeline.

This is why hanging out in a dedicated space like the PromptCube homepage 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 (Model Context Protocol) integration, you steal the structure, and you apply it to your own bot.

A quick checklist for your next agent

Before you deploy your next agent, run it through this stress test:

  • [ ] 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).
  • [ ] The Edge Case: What happens if the input is an empty string? Does the agent crash or output a graceful NULL?
  • [ ] 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...").
  • [ ] The Format Lock: If you need JSON, are you using a Pydantic schema or a JSON-mode flag?

To 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.

Just keep iterating. Most of my "perfect" prompts are actually the result of 15 failed versions and a lot of annoyance.

All Replies (0)

No replies yet — be the first!

Write a Reply

Markdown supported