OpenClaw Prompt Engineering
Write better prompts for OpenClaw agents: system prompts, few-shot examples, chain-of-thought reasoning, dynamic prompt construction, optimization techniques, and injection defense.
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
| Pitfall | Symptom | Fix |
|---|---|---|
| Vague role definition | Inconsistent persona, scope drift | Add explicit Role + Capabilities sections |
| Missing output format | JSON, Markdown, and plain text mixed unpredictably | Specify exact format with an example |
| Over-permissive constraints | Agent executes unintended actions | List explicit prohibitions with "Do NOT" |
| No injection defense | Adversarial inputs override system prompt | Add input sanitization + injection detection |
| Prompt bloat | High token cost, truncated context | Move static content to a RAG corpus |
Sections
Overview
Prompt engineering fundamentals, anatomy of a good prompt, and OpenClaw-specific patterns.
Read โTemplates
Reusable prompt templates with variables, conditionals, and composition patterns.
Read โSystem Prompts
Design effective system prompts that define agent persona, scope, and behavior constraints.
Read โFew-Shot
Few-shot and zero-shot prompting: providing examples to guide model output.
Read โChain of Thought
CoT prompting, reasoning steps, scratchpad patterns, and tree-of-thought techniques.
Read โDynamic Prompts
Runtime prompt generation, context injection, memory summarization, and tool output formatting.
Read โOptimization
Token efficiency, prompt compression, auto-optimization, and cost reduction techniques.
Read โTesting
Prompt testing strategies, regression suites, and evaluating prompt quality at scale.
Read โInjection Defense
Detect and prevent prompt injection attacks, jailbreaks, and adversarial inputs.
Read โLibraries
Community prompt libraries, curated templates, and sharing best-practice prompts.
Read โ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:
| Technique | Token Savings | Trade-off |
|---|---|---|
| Remove redundant role repetition | 5โ15% | None |
| Move static knowledge to RAG | 30โ60% | Retrieval latency added |
| Use code comments style (terse bullets) | 10โ20% | Slightly less readable for humans |
| Summarise memory instead of verbatim history | 40โ70% | Some context detail lost |
| Model-specific prompts (gpt-4o vs claude) | 5โ10% | Maintenance overhead |