BlogGuides

How to Write a System Prompt for an AI Agent

Learn to craft effective system prompts for AI agents: structure, tone, constraints, examples, and common pitfalls.

How to Write a System Prompt for an AI Agent

How to Write a System Prompt for an AI Agent

System prompts are the hidden instructions that shape how an AI agent behaves. Unlike a one-off user query, a system prompt sets the stage for every interaction, defining the agent's role, goals, tone, and boundaries. Whether you're building a customer support bot, a coding assistant, or a creative tool, mastering system prompts is essential.

This guide walks you through the core principles, a practical framework, and common mistakes to avoid. You'll also find real examples and links to further resources.

What Is a System Prompt?

A system prompt is the initial instruction given to a language model before any user input. It tells the model who it is (e.g., "You are a helpful legal assistant"), what it should do, and how it should behave. It persists across the conversation, acting as a persistent memory of the agent's purpose.

Think of it as the agent's job description and code of conduct rolled into one. A well-written system prompt can transform a generic model into a specialized tool.

Why System Prompts Matter for AI Agents

AI agents are more than simple chatbots. They can use tools, call APIs, and perform multi-step tasks. A system prompt is crucial because it:

  • Defines the agent's scope: Prevents it from wandering off-topic.
  • Sets expectations: Tells the model how to handle ambiguous requests.
  • Controls tone and style: Ensures consistent branding or personality.
  • Improves safety: Sets boundaries to avoid harmful outputs.
  • Enhances reliability: Reduces hallucinations by grounding the model in context.
  • For example, an agent that books flights needs a system prompt that specifies the airline policies, user preferences, and how to handle cancellations. Without it, the agent might invent rules or ignore constraints.

    Key Principles of Effective System Prompts

    1. Be Specific and Concrete

    Vague instructions lead to vague outputs. Instead of "be helpful," say "provide step-by-step instructions for troubleshooting common issues." The more specific you are, the better the model can follow.

    Example:

  • Weak: "You are a travel agent."
  • Strong: "You are a travel agent specializing in budget trips to Europe. Always recommend the cheapest flight options, mention baggage fees, and ask for the traveler's departure city before suggesting routes."
  • 2. Define the Role and Persona

    Give the agent a clear identity. This influences vocabulary, tone, and even the type of responses it generates. For instance, a "friendly librarian" will answer differently than a "stern data analyst."

    You can draw inspiration from existing system prompts, like the Transformation Architect system prompt in Naval Ravikant's voice, which shows how a persona shapes the entire interaction.

    3. Set Boundaries and Constraints

    Tell the agent what it cannot do. This is critical for safety and reliability. For example:

  • "Do not provide medical advice."
  • "If you don't know the answer, say 'I don't know' and suggest a human expert."
  • "Never share personal user data."
  • Constraints also include format requirements, like "respond in JSON" or "use bullet points."

    4. Provide Examples (Few-Shot Learning)

    Including examples in the system prompt can dramatically improve output quality. Show the model what a good response looks like. This is especially useful for tasks with specific formats or styles.

    Example snippet:

    User: What's the weather in Tokyo?

    Assistant: The current weather in Tokyo is 22°C, sunny, with a gentle breeze. Expect a high of 25°C and a low of 18°C. Bring a light jacket for the evening.

    5. Use Clear Structure and Formatting

    Organize your system prompt with sections, bullet points, or numbered lists. This makes it easier for the model to parse and follow. You can also use delimiters like ### to separate instructions from context.

    6. Iterate and Test

    System prompts are not set-and-forget. You must test them with various inputs and refine based on results. Pay attention to edge cases and adjust accordingly. This is a core part of prompt engineering, as highlighted in the OpenAI prompt engineering guide.

    A Step-by-Step Framework for Writing a System Prompt

    Here's a practical framework you can adapt:

  • Define the Agent's Purpose: What is its main function? Who is the end user?
  • Choose a Persona: What role should it play? What tone? (e.g., professional, friendly, concise)
  • List Key Capabilities: What tasks must it handle? What tools or data does it have access to?
  • Set Constraints: What are the do's and don'ts? Any legal or ethical boundaries?
  • Provide Context: Include any background information the agent needs.
  • Add Examples: Show 2-3 sample interactions.
  • Specify Output Format: If needed, define the response structure.
  • Include Fallback Behavior: What should the agent do when uncertain?
  • Let's flesh out an example for a customer support agent:

    You are a customer support assistant for TechGadgets Inc.

    Your goal is to resolve user issues quickly and politely.

    You have access to our FAQ database and order system.

    Rules:

  • Always greet the user by name if provided.
  • If the issue is about a defective product, offer a replacement or refund.
  • Never promise discounts unless explicitly stated in the FAQ.
  • If you don't know the answer, say: "I'm not sure, but I'll connect you with a human specialist."
  • Tone: Friendly, empathetic, and concise.

    Example:

    User: My laptop won't turn on.

    Assistant: I'm sorry to hear that. Let's troubleshoot. First, try holding the power button for 10 seconds. If that doesn't work, is the charger connected?

    This framework is versatile. For creative tasks, you can adapt it similarly. For instance, the Red Veil Register System Prompt for Image Generation shows how to constrain an image generator with a specific aesthetic.

    Common Mistakes to Avoid

    1. Being Too Vague

    Avoid generic phrases like "be helpful" or "answer questions." They don't give the model enough direction.

    2. Overloading with Information

    Too much context can confuse the model. Stick to what's essential. If you need more, consider using retrieval-augmented generation (RAG) instead of stuffing everything into the prompt.

    3. Ignoring Safety and Bias

    System prompts should explicitly address safety. For example, "Do not generate hate speech" or "Avoid stereotypes." This is especially important for public-facing agents.

    4. Not Testing Edge Cases

    Your agent will encounter weird inputs. Test with typos, ambiguous phrasing, and out-of-scope questions to see how it responds.

    5. Forgetting to Update

    As your product evolves, your system prompt should too. Version control your prompts and review them regularly.

    Advanced Tips for Agent-Specific Prompts

    AI agents often have access to tools and external data. Your system prompt should account for that:

  • Tool usage: Specify when and how to use tools (e.g., "Use the calculator for math problems.")
  • Multi-step reasoning: Encourage the agent to break down complex tasks ("Think step-by-step before answering.")
  • Memory: If the agent has memory, define what to remember and what to forget.
  • Error handling: Tell the agent how to respond when a tool fails.
  • For example, a coding agent might have a system prompt like:

    You are a Python coding assistant. You have access to a code interpreter. When asked to write code, first explain your approach, then write the code, and finally test it by running it. If the code has errors, debug and retry.

    This is more effective than a generic "help with code" prompt.

    Real-World Examples and Inspiration

    Looking at existing system prompts can spark ideas. Here are a few from our wiki:

  • Interactive course system prompt for teaching agent setup safely - demonstrates how to guide users through a setup process.
  • System prompt: rewrite prompts into Aperture Geometry / Velvet Trace scenes - shows a creative transformation task.
  • Dual-world watercolor travel poster prompt for any destination - a template for image generation.
  • Cinematic miniature world advertising poster prompt - another creative example.
  • These illustrate how system prompts can be tailored for specific domains.

    Further Reading and Resources

    To deepen your understanding, check out these authoritative guides:

  • Anthropic's prompt engineering overview - covers best practices for Claude.
  • Google Gemini prompting strategies - focuses on Gemini models.
  • Prompt Engineering Guide - a comprehensive community resource.
  • Learn Prompting - beginner-friendly tutorials.
  • Google Vertex AI prompt design - practical tips for enterprise.
  • Anthropic's prompt engineering for business - how to align prompts with business goals.
  • These resources offer deeper dives into specific techniques like chain-of-thought, few-shot learning, and evaluation.

    Checklist for Your Next System Prompt

    Before you finalize, run through this checklist:

  • [ ] Purpose clearly defined
  • [ ] Persona and tone specified
  • [ ] Key capabilities listed
  • [ ] Constraints and boundaries set
  • [ ] Examples included (if helpful)
  • [ ] Output format specified (if needed)
  • [ ] Fallback behavior defined
  • [ ] Tested with edge cases
  • [ ] Reviewed for safety and bias
  • Conclusion

    Writing a system prompt is both an art and a science. It requires clarity, specificity, and continuous iteration. By following the principles and framework above, you can create system prompts that make your AI agent reliable, useful, and safe.

    Remember, the best system prompts are invisible - they guide the agent so effectively that users never notice the instructions behind the scenes. Start simple, test often, and refine based on real interactions.

    For more inspiration, browse our system prompt collection or explore productivity-related prompts. Happy prompting!

    Tags
    system prompt·AI agent·prompt engineering·LLM·chatbot·automation·guide