Earlier chapters build system prompts directly in code. As the agent grows, the prompt must include role, constraints, skills, memory, project instructions, and dynamic context. Inline string formatting becomes hard to maintain.
This chapter introduces structured system prompt construction.
cargo run -p s10_system_prompt- Add a prompt builder.
- Render prompt content from a Markdown template.
- Include available skills.
- Include memory guidance and memory content.
- Load
CLAUDE.mdinstructions. - Include dynamic context such as current directory, date, model, and platform.
- Keep prompt sections explicit and easy to modify.
The system prompt is part of the harness contract. It defines:
- What the agent is.
- What tools it can use.
- How it should behave.
- Which external knowledge is available.
- What project and user rules apply.
If this is just one giant string, it becomes difficult to reason about changes.
s10_system_prompt/
├── src/
│ ├── main.rs
│ ├── lib.rs
│ ├── prompt.rs
│ ├── memory.rs
│ ├── skill.rs
│ ├── system_prompt_template.md
│ └── tool/
│ ├── load_skill.rs
│ ├── memory.rs
│ ├── bash.rs
│ ├── read_file.rs
│ ├── write_file.rs
│ └── edit_file.rs
└── s10.md
Prompt construction lives in src/prompt.rs. The template is src/system_prompt_template.md.
The prompt builder collects sections:
- role
- guidelines
- constraints
- available skills
- memory
CLAUDE.md- dynamic context
- memory guidance
Then it renders them through the template.
This keeps prompt composition explicit and makes it easier to add or remove sections.
Dynamic context can include:
- Current date.
- Working directory.
- Model name.
- Platform.
- Git branch or other environment state.
This information changes between runs, so it should be generated at runtime instead of stored in a static prompt file.
The harness can load project-specific instructions from CLAUDE.md.
This gives the project a conventional place to express:
- Coding style.
- Test commands.
- Repository-specific constraints.
- Workflow preferences.
The loaded content is added as a prompt section.
The prompt includes skill summaries, not full skill bodies. Full skills are loaded through load_skill.
Memory content is loaded into the prompt so the agent can apply durable preferences and facts without an extra tool call.
- Use a typed prompt builder instead of ad hoc string concatenation.
- Keep the template in a Markdown file.
- Keep dynamic context generation separate from static prompt text.
- Treat prompt construction as runtime state, not a tool.
- The prompt is still a single rendered string.
- No token budgeting by section.
CLAUDE.mdloading is simple.- No priority or conflict resolution between prompt sections.
- No localization of prompt sections.
s11 adds error recovery for prompt-too-long errors, transient transport errors, and truncated model output.