Why Prompt Quality Matters

The system prompt is the first message the LLM reads on every turn โ€” it defines the agent's persona, capabilities, tone, and decision boundaries. A vague or over-permissive system prompt leads to hallucinations, scope creep, and security vulnerabilities. A well-crafted prompt reduces token usage, improves response consistency, and makes behavior auditable.

OpenClaw separates prompt concerns into distinct layers: the system prompt defines role and constraints, few-shot examples guide format and reasoning style, and dynamic context injects runtime data (memory summaries, tool outputs, user history). Keeping these layers clean makes prompts composable, testable, and easy to version.

Anatomy of a Good System Prompt

The following template covers the six components that make a system prompt effective:

# Role
You are a precise research assistant specializing in technical documentation.

# Capabilities
You can search the web, read files, and write structured reports.

# Constraints
- Cite every claim with a URL or file path.
- Do NOT execute code unless explicitly asked.
- Respond in the user's language.

# Output Format
Return a Markdown document with H2 sections and a references list.

# Tone
Professional, concise, and factual. Avoid filler phrases.

Common Prompt Pitfalls

PitfallSymptomFix
Vague role definitionInconsistent persona, scope driftAdd explicit Role + Capabilities sections
Missing output formatJSON, Markdown, and plain text mixed unpredictablySpecify exact format with an example
Over-permissive constraintsAgent executes unintended actionsList explicit prohibitions with "Do NOT"
No injection defenseAdversarial inputs override system promptAdd input sanitization + injection detection
Prompt bloatHigh token cost, truncated contextMove static content to a RAG corpus

Sections

Few-Shot Prompting Example

Providing 2โ€“3 examples in the system prompt dramatically improves output consistency. Here is a few-shot prompt for a data extraction agent:

Extract the company name, role, and salary from the job posting provided.
Return a JSON object.

Example 1:
Input: "Software Engineer at Stripe โ€” $180,000โ€“$220,000/year"
Output: {"company": "Stripe", "role": "Software Engineer", "salary": "$180,000โ€“$220,000"}

Example 2:
Input: "Senior Data Scientist @ Anthropic, comp $250k"
Output: {"company": "Anthropic", "role": "Senior Data Scientist", "salary": "$250,000"}

Now extract from the following:
Input: "{{job_description}}"

Chain-of-Thought Example

Chain-of-thought prompting asks the model to reason step-by-step before giving a final answer. Add "Let's think step by step" or a <thinking> block to improve accuracy on complex tasks:

Analyse the following Python code for security vulnerabilities.
Think step by step:
1. Identify all external inputs (function parameters, environment variables, HTTP headers).
2. Trace each input through the code to find where it reaches sinks (SQL queries, shell commands, file paths).
3. Determine if any input reaches a sink without sanitisation.
4. List the vulnerabilities found with CWE codes.

<thinking>
[Your reasoning here]
</thinking>

**Vulnerabilities found:**
[Your final answer here]

Dynamic Context Injection

Use OpenClaw's prompt template system to inject runtime data into prompts without hardcoding:

# openclaw.yaml
prompt:
  system: |
    You are a research assistant for {{company_name}}.
    Today's date is {{today}}.
    The user's name is {{user.name}} and their preferred language is {{user.language}}.
    Relevant context from memory:
    {{memory.top_3_relevant}}
  variables:
    company_name: Acme Corp
    today: "{{ now | format('%B %d, %Y') }}"

Token Optimisation Tips

System prompts are attached to every LLM call. Keeping them lean saves significant cost at scale:

TechniqueToken SavingsTrade-off
Remove redundant role repetition5โ€“15%None
Move static knowledge to RAG30โ€“60%Retrieval latency added
Use code comments style (terse bullets)10โ€“20%Slightly less readable for humans
Summarise memory instead of verbatim history40โ€“70%Some context detail lost
Model-specific prompts (gpt-4o vs claude)5โ€“10%Maintenance overhead