Home Tutorials Categories Skills About
ZH EN JA KO
Advanced

Advanced Techniques for Custom System Prompts in OpenClaw

· 17 min read

Introduction

The system prompt is the foundation that defines AI Agent behavior. A well-designed system prompt can make the AI accurately assume a specific role, follow specific rules, and produce responses in a specific format. OpenClaw not only supports static system prompts but also provides a powerful dynamic template system with 15+ built-in parameters and conditional logic, enabling you to build intelligent prompts that adapt to different scenarios.

This article provides a systematic guide to advanced system prompt configuration techniques in OpenClaw.

Basic Configuration

Static System Prompt

The simplest approach is to specify the prompt directly in the Agent configuration:

{
  agents: {
    "my-agent": {
      systemPrompt: "You are a professional technical assistant. Your answers should be concise, accurate, and include code examples."
    }
  }
}

Loading from a File

For longer prompts, using a separate file is recommended:

{
  agents: {
    "my-agent": {
      systemPromptFile: "./prompts/tech-assistant.md"
    }
  }
}

Dynamic Template System

OpenClaw's system prompts support Mustache-style template syntax with runtime parameter substitution.

Basic Syntax

Hello, {{userName}}. The current time is {{currentTime}}.

All Built-in Parameters

OpenClaw provides the following built-in template parameters:

Parameter Description Example Value
{{userName}} Current user name "John"
{{userId}} User ID "user_12345"
{{channelType}} Channel type "dm" / "group"
{{channelName}} Channel name "Tech Discussion Group"
{{platform}} Chat platform "telegram" / "discord"
{{agentId}} Agent ID "my-agent"
{{agentName}} Agent display name "Tech Assistant"
{{currentDate}} Current date "2026-03-14"
{{currentTime}} Current time "14:30:00"
{{currentDateTime}} Full date and time "2026-03-14 14:30:00"
{{timezone}} Timezone "Asia/Shanghai"
{{locale}} Language locale "zh-CN"
{{sessionId}} Session ID "session-abc123"
{{messageCount}} Message count in current session "15"
{{modelName}} Model in use "claude-sonnet-4-20250514"
{{toolList}} Available tools list "search, calculator, ..."

Usage Example

You are {{agentName}}, an AI assistant running on {{platform}}.

Current environment information:
- User: {{userName}}
- Time: {{currentDateTime}} ({{timezone}})
- Channel: {{channelName}} ({{channelType}})
- This session has had {{messageCount}} rounds of conversation

You can use the following tools: {{toolList}}

Please adjust your response style and content based on the above environment information.

Conditional Logic

Switching Behavior Based on Platform

{{#if platform == "telegram"}}
You are running on Telegram. Please format responses using Markdown and make good use of emoji.
Keep message length under 4096 characters.
{{/if}}

{{#if platform == "discord"}}
You are running on Discord. You can use Discord's Markdown syntax.
Code block highlighting and embed formats are supported.
{{/if}}

{{#if platform == "slack"}}
You are running on Slack. Use Slack's mrkdwn format.
Block Kit rich text format is supported.
{{/if}}

Adjusting Style Based on Channel Type

{{#if channelType == "dm"}}
You are in a one-on-one private chat with the user. You can provide detailed, personalized responses.
You can remember the user's preferences and history.
{{/if}}

{{#if channelType == "group"}}
You are in a group chat. Please note:
- Only respond when @mentioned or clearly needed
- Keep responses concise to avoid flooding the chat
- Be mindful of the group topic context
{{/if}}

Dynamic Adjustment Based on Time

{{#if hour >= 22 || hour < 6}}
The user appears to be active late at night. Remind them to rest when appropriate.
{{/if}}

{{#if dayOfWeek == "Saturday" || dayOfWeek == "Sunday"}}
Today is a weekend. Responses can be more relaxed and casual.
{{/if}}

Advanced Composition Techniques

Multi-Layer Prompt Concatenation

OpenClaw supports dividing system prompts into multiple layers that are concatenated in order:

{
  agents: {
    "my-agent": {
      systemPrompt: {
        // Base personality layer
        base: "./prompts/base-personality.md",
        // Tool usage guidelines layer
        tools: "./prompts/tool-guidelines.md",
        // Safety rules layer
        safety: "./prompts/safety-rules.md",
        // Dynamic context layer
        context: "Current user: {{userName}}, Platform: {{platform}}"
      }
    }
  }
}

The final system prompt is concatenated in the order base → tools → safety → context.

Custom Parameters

In addition to built-in parameters, you can define your own template parameters:

{
  agents: {
    "my-agent": {
      systemPromptFile: "./prompts/custom-agent.md",
      promptParams: {
        companyName: "Geek Tech",
        productName: "SuperApp",
        supportEmail: "[email protected]",
        workingHours: "Monday to Friday 9:00-18:00",
        maxResponseLength: 500
      }
    }
  }
}

Use them in the prompt template:

You are the {{productName}} customer support assistant for {{companyName}}.

Response rules:
- Responses should not exceed {{maxResponseLength}} characters
- For issues you cannot resolve, guide the user to contact {{supportEmail}}
- Working hours are {{workingHours}}

Knowledge Base Injection

Dynamically inject external knowledge base content into the system prompt:

{
  agents: {
    "my-agent": {
      systemPrompt: {
        base: "You are a product expert...",
        // Load FAQ from knowledge base file
        knowledge: {
          source: "./knowledge/faq.md",
          // Maximum injected token count
          maxTokens: 4000
        }
      }
    }
  }
}

Persona Preset Combinations

OpenClaw includes a built-in Persona preset system that you can customize on top of:

{
  agents: {
    "my-agent": {
      // Base personality preset
      persona: "professional",  // Options: friendly, professional, technical, creative
      // Append custom instructions on top of the preset
      systemPromptAppend: "Additional rule: All code examples must use Python."
    }
  }
}

Prompt Debugging

Viewing the Active Prompt

# View the Agent's final concatenated system prompt
openclaw agent inspect my-agent --show-prompt

# Simulate with context (specify platform and user)
openclaw agent inspect my-agent --show-prompt \
  --platform telegram --channel-type dm --user "test-user"

Prompt Version Management

It is recommended to put prompt files under version control, so you can:

  • Track the history of every prompt modification
  • Review prompt changes through PRs
  • Quickly roll back to a previous version
prompts/
├── base-personality.md
├── tool-guidelines.md
├── safety-rules.md
├── CHANGELOG.md
└── v2/
    └── base-personality.md

Best Practices

  1. Layered Design: Divide prompts into personality, tools, and rules layers, each with a clear responsibility
  2. Leverage Template Parameters: Avoid hardcoding to make prompts adaptable to multiple scenarios
  3. Platform Adaptation: Different chat platforms have different message formats and length limits — use conditional logic to adapt
  4. Version Control: Prompts are code and should be managed like code
  5. Regular Testing: After modifying prompts, verify their effects across platforms and scenarios
  6. Keep It Concise: The longer the system prompt, the more tokens it consumes, and the higher the cost

Conclusion

The system prompt is the soul of an AI Agent. OpenClaw's dynamic template system allows you to automatically adjust prompt content based on user identity, chat platform, time context, and other factors, building truly intelligent AI assistants that adapt to different scenarios. Mastering these advanced techniques will enable your Agent to perform excellently across a variety of complex situations.

OpenClaw is a free, open-source personal AI assistant that supports WhatsApp, Telegram, Discord, and many more platforms