What Hidden Secrets Does the Agent’s System Prompt Code Reveal?
This article dissects OpenClaw's agent architecture, detailing how the System Prompt, Skill modules, and Agent Loop interact, explaining PromptMode variations, safety rules, tool definitions, skill loading pipelines, heartbeat handling, sub‑agent spawning, silent replies, and the context engine that assembles messages for LLMs.
Overall Architecture: Three Components
The agent consists of three tightly coupled parts: the System Prompt (the script), Skill (the extension pack), and the Agent Loop (the actor following the script).
System Prompt is the screenplay, Skill is the expansion pack, and Agent Loop is the performer.
System Prompt – What It Is and How to Assemble It
The System Prompt acts as a "pre‑training manual" injected before each LLM conversation. It tells the model its identity, available tools, rules to obey, which Skills can be used, and the working directory.
You are a personal assistant running inside OpenClaw.
## Tooling – a list of available tools.
## Safety – hard‑coded safety rules.
## OpenClaw CLI – quick reference.
## Skills (mandatory) – where Skills are injected.
## Memory Recall – memory rules.
## Workspace – working directory.
## Reply Tags – response format tags.
## Messaging – message‑sending rules.
## Silent Replies – rules for no‑output cases.
## Heartbeats – heartbeat mechanism description.
## Runtime – OS/model/channel info.
Project Context – project files such as AGENTS.md.
The function buildAgentSystemPrompt() in src/agents/system-prompt.ts concatenates these modules in order and joins them with newlines.
PromptMode – Different Depths for Different Scenarios
type PromptMode = "full" | "minimal" | "none";"full" – used by the main agent, includes all modules.
"minimal" – used by sub‑agents, keeps only core tool sections.
"none" – minimal mode with only a single identity line.
If promptMode === "none", the function returns only the identity line; otherwise, it builds the full list, skipping memory, identity, and messaging sections when isMinimal is true.
Skill Mechanism – The Core Design
Skills are injected into the System Prompt via the buildSkillsSection() function. The section looks like:
## Skills (mandatory)
Before replying: scan <available_skills> <description> entries.
- If exactly one skill clearly applies: read its SKILL.md at <location> with `read`, then follow it.
- If multiple could apply: choose the most specific one, then read/follow it.
- If none clearly apply: do not read any SKILL.md.
<available_skills> … </available_skills>Each skill entry contains name, description, and location (e.g., ~/.agents/skills/git/SKILL.md).
Skill Loading Pipeline
Discover sources – merge skills from extra, bundled, managed, personalAgents, projectAgents, and workspace with increasing priority using a Map<string, Skill>.
Eligibility filtering – filter by OS, required binaries, and environment variables.
Path compaction – replace the home directory with ~ to save tokens (≈5‑6 tokens per path).
Truncation limits – cap the number of skills ( MAX_SKILLS_IN_PROMPT = 150), total characters ( MAX_SKILLS_PROMPT_CHARS = 30_000), and file size ( MAX_SKILL_FILE_BYTES = 256_000). The algorithm first slices by count, then binary‑searches for the maximal set that fits the token budget.
Generate SkillSnapshot – a snapshot containing the prompt fragment and metadata, ensuring the skill directory is re‑injected even after context compression.
Skill Type System
type OpenClawSkillMetadata = {
always?: boolean;
emoji?: string;
os?: string[];
requires?: {
bins?: string[];
anyBins?: string[];
env?: string[];
config?: string[];
};
install?: SkillInstallSpec[];
};
type SkillInvocationPolicy = {
userInvocable: boolean;
disableModelInvocation: boolean;
};Agent Runtime Mechanisms
Agent Loop
The loop receives a user message, runs the tool chain, generates a reply, and outputs it.
Heartbeat
Periodically the system sends a heartbeat message. The System Prompt defines the response protocol:
## Heartbeats
Heartbeat prompt: ${heartbeatPrompt}
If you receive a heartbeat poll and there is nothing that needs attention, reply exactly: "HEARTBEAT_OK"
If something needs attention, do NOT include "HEARTBEAT_OK"; reply with the alert text instead.In a merchant scenario, a heartbeat checks inventory; if stock is normal the agent replies HEARTBEAT_OK, otherwise it sends an alert.
Sub‑Agent
For complex or long‑running tasks the agent can spawn a sub‑agent using the sessions_spawn tool. Sub‑agents run in minimal mode (only Tooling + Skills), keeping them lightweight.
Silent Reply
When the agent has nothing to say, it must output only the special token __SILENT__ and nothing else.
## Silent Replies
When you have nothing to say, respond with ONLY: __SILENT__
It must be your ENTIRE message — nothing else
Never append it to an actual responseContext Engine
The ContextEngine assembles historical messages and the System Prompt into the final context sent to the LLM, respecting a token budget and performing automatic summarisation when the context exceeds the limit.
interface ContextEngine {
readonly info: ContextEngineInfo;
assemble(params: { sessionId: string; messages: AgentMessage[]; tokenBudget?: number; }): Promise<AssembleResult>;
compact(params: { sessionId: string; sessionFile: string; tokenBudget?: number; force?: boolean; }): Promise<CompactResult>;
}
type AssembleResult = {
messages: AgentMessage[];
estimatedTokens: number;
systemPromptAddition?: string;
};System Mark
A constant SYSTEM_MARK = "⚙️" prefixes all system‑generated messages (heartbeats, sub‑agent notifications, etc.). The helper prefixSystemMessage(text: string) adds the mark if it is not already present.
Core Design Takeaways
System Prompt as layered LEGO blocks – built from independent section functions (e.g., buildSkillsSection(), buildMemorySection()), allowing selective inclusion via PromptMode.
On‑demand Skill loading saves tokens – only name and description are placed in the prompt (≈100 tokens per skill); the full skill file is read later with the read tool, reducing token usage by >98 %.
SkillSnapshot prevents "context‑compression amnesia" – the snapshot is re‑injected each run, guaranteeing the agent always knows which Skills are available even after long conversations.
In summary, the large‑model input is a single string. OpenClaw maximises its expressive power by modularising the System Prompt, lazily loading Skills, and providing robust runtime mechanisms such as heartbeats, sub‑agents, and silent replies.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Tencent Cloud Developer
Official Tencent Cloud community account that brings together developers, shares practical tech insights, and fosters an influential tech exchange community.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
