Recommended resources
The freely available resources this lesson will direct you to.
Welcome back. In our previous lesson, we successfully built and executed our first worker agent, the cryptoPriceAgent. It was a significant step, as the agent could autonomously use its cryptoPriceTool to find information. However, its "mind"—the instructions guiding its behavior—was minimal: a single sentence telling it to be a helpful assistant. For a production-grade system, this is insufficient. An agent with vague instructions is unpredictable, unreliable, and potentially unsafe.
This lesson will focus on crafting a precise and robust system prompt for our agent. We will move from a simple instruction to a detailed "job description" that defines the agent's role, its operational boundaries, the exact workflow it must follow, and the format of its output. Mastering this is fundamental to building the controllable and specialized agents required for your hierarchical infrastructure.
1. The Power of the System Prompt
Before we start writing, let's solidify our understanding of what a system prompt is and why it's distinct from the user's input. A system prompt is a set of instructions provided to the language model before it processes any user query. It acts as a foundational rulebook, shaping the model's persona, capabilities, and constraints for the entire conversation.
The following diagram illustrates this relationship. The user defines the behavior and rules via a System Context, which persists and influences how the LLM processes every subsequent task or query.

This sequence diagram shows how a persistent System Context, defined by the developer, guides the LLM in processing user queries. This context is our system prompt.
For development, it's common to prefix a user prompt with instructions like, "You are a helpful assistant...". While this works for quick tests, it's not a true system prompt. In a production setting, relying on this method is problematic. Let's read a short article that explains why.
Guide to Writing System Prompts: The Hidden Force ...
This guide from Sahara AI clearly explains the role of system prompts and the critical distinction between them and user-provided instructions.
Read the introduction and the section The Invisible Architecture of AI Conversations. Pay close attention to the three reasons why a true system prompt is superior for production control: Authority, Persistence, and Security.
As the article highlights, a true system prompt, passed through the API's designated system parameter (or the instructions property in ToolLoopAgent), has higher priority and is more resilient to being ignored or overwritten. This control is non-negotiable for the reliable agentic infrastructure we are building.
2. The Anatomy of an Effective System Prompt
A well-crafted system prompt is not an unstructured block of text. It's a carefully organized document that covers several key aspects of the agent's behavior. The best prompts are structured hierarchically and provide explicit guidance.
Let's watch a video that breaks down the essential sections of a high-quality system prompt.
How to Write Better System Prompts
In "How to Write Better System Prompts," ZazenCodes provides an excellent walkthrough of a well-structured prompt, highlighting its key components.
First, watch the detailed breakdown from the anatomy of a system prompt. Notice the use of clear headings and even HTML-like tags to create a structured hierarchy. Then, watch the summary of essential elements from designing a high-quality prompt.
Synthesizing from the video and other best practices, here are the core components we should include in our prompts:
- Role and Goal: Clearly state the agent's identity and its primary objective.
- Boundaries and Constraints: Define what the agent must not do. This is crucial for safety and preventing scope creep.
- Workflow Policy (Chain of Thought): Provide a step-by-step process for the agent to follow when it receives a request. This makes its reasoning process more predictable.
- Tools and Actions: Specify the available tools and provide clear instructions on when and how to use them.
- Output Format: Dictate the exact structure of the final response. This ensures consistency for downstream processing or user display.
- Refusal Style: Instruct the agent on how to behave when it cannot fulfill a request. A concise and helpful refusal builds user trust.
A great example of these principles in action can be found in the Sahara AI article, which includes a toy system prompt for a coding assistant. It's a bit long to read in full, but skimming its structure is highly instructive. You'll see sections for INSTRUCTION HIERARCHY, ROLE & SCOPE, TOOLS, WORKFLOW POLICY, SECURITY & REFUSALS, and OUTPUT FORMAT, directly mapping to the principles we just discussed.
3. Practical Implementation: Upgrading the cryptoPriceAgent
Now, let's apply this knowledge. We will replace the simplistic instructions string in our cryptoPriceAgent from the last lesson with a comprehensive, structured system prompt.
Here is the previous, minimal implementation:
// Previous version
const cryptoPriceAgent_v1 = new ToolLoopAgent({
model: openai('gpt-4o-mini'),
tools: { getCryptoPrice: cryptoPriceTool },
instructions: 'You are a helpful assistant that provides cryptocurrency prices.',
});
Here is the new, production-ready system prompt, incorporating the best practices we've learned.
const cryptoPriceSystemPrompt = `
<System>
# ROLE & GOAL
You are a specialized financial data assistant. Your sole purpose is to provide the current price of a specified cryptocurrency in a designated fiat currency using the available tools.
# WORKFLOW POLICY
1. On receiving a user request, your first priority is to determine the cryptocurrency and the target fiat currency.
2. If either the cryptocurrency or the fiat currency is missing or ambiguous, you MUST ask ONE clarifying question. Do not make assumptions.
3. Once you have the necessary information, you MUST call the 'getCryptoPrice' tool to retrieve the data. Do not answer from memory or provide information you haven't just fetched.
# CONSTRAINTS & GUARDRAILS
- DO NOT provide any financial advice, market analysis, or price history. Your responses are strictly limited to the current price.
- DO NOT answer questions unrelated to cryptocurrency prices.
- If the 'getCryptoPrice' tool returns an error, you must relay that information to the user.
# REFUSAL STYLE
- If a user asks for something outside your scope (e.g., "Should I buy Bitcoin?"), you must politely decline by stating: "I can only provide current cryptocurrency prices and cannot offer financial advice."
- If the tool call fails, your response should be: "I was unable to retrieve the price for the requested currency pair. Please ensure it is a valid pair and try again."
# OUTPUT FORMAT
- Your final answer MUST be in the following format and nothing else: "The current price of [Cryptocurrency Name] is [Currency Symbol][Price]."
- Example: "The current price of Bitcoin is €60,000.00."
</System>
`;
Now, we update our agent definition to use this new prompt. The Vercel AI SDK's ToolLoopAgent uses the instructions property to pass the system prompt to the model.
import { ToolLoopAgent } from 'ai';
import { openai } from '@ai-sdk/openai';
import { cryptoPriceTool } from './crypto-price-tool';
// The new, detailed system prompt
const cryptoPriceSystemPrompt = `...`; // (paste the prompt from above)
// Define our upgraded worker agent
export const cryptoPriceAgent = new ToolLoopAgent({
model: openai('gpt-4o-mini'),
tools: {
getCryptoPrice: cryptoPriceTool,
},
instructions: cryptoPriceSystemPrompt,
// We can also set maxSteps to act as a safeguard,
// though our workflow is simple enough not to need many steps.
maxSteps: 5,
});
With this change, our cryptoPriceAgent is no longer just a "helpful assistant." It is a specialized worker with a clear, enforceable mandate. It knows its job, its limits, how to handle errors, and how to format its results. This level of precision is exactly what's needed for predictable components in a larger agentic system.
4. Advanced Consideration: The Problem of Monolithic Prompts
Our new prompt is a massive improvement. However, as we build more complex agents for our infrastructure, you can imagine these system prompts growing to be hundreds or even thousands of lines long. This introduces a problem known as "context rot".
Empowering your AI app with Claude Skills using the Vercel AI SDK
This article by Joachim Hodana on Medium explains the downsides of large, monolithic prompts and introduces a more scalable, modular approach.
Read the introduction, the section on monolithic prompts, and the definition of Claude Skills.
The key insight here is that as a prompt grows, the model's attention gets diluted, and performance can degrade. A more advanced architectural pattern is to break down large knowledge domains into smaller, modular files (sometimes called "skills") and have the agent dynamically load only the knowledge it needs for a specific task via a tool call.
For example, a complex SQL generation agent might have its knowledge split into joins.md, window-functions.md, and performance.md. If a user asks about a rolling average, the agent would use a tool to load only the contents of window-functions.md into its context, ignoring the rest.
This is a powerful pattern for building scalable, maintainable, and accurate agents. While we won't implement it today, it's a crucial concept to keep in mind. We will revisit and implement this dynamic, retrieval-based approach when we build our Prompt Store in a future module.
Conclusion
In this lesson, we have given our worker agent its "mind." We've moved beyond a simple one-line instruction to craft a detailed system prompt that precisely governs its behavior, making it a reliable and predictable component.
Key Takeaways:
- A system prompt is a foundational set of instructions that has higher authority and persistence than instructions embedded in a user query.
- Effective system prompts are structured and define the agent's role, workflow, boundaries, and output format.
- Using clear headings, explicit rules (DO/DON'T), and example formats significantly improves agent reliability.
- For complex domains, monolithic system prompts can lead to "context rot." A more scalable, long-term solution involves modularizing knowledge and loading it dynamically, a pattern we will implement later.
You have now built a worker agent with both a body (tools) and a well-defined mind (system prompt). In our next lesson, we will make this mind even more flexible by learning how to implement dynamic prompt composition using template variables and runtime context. This will allow us to create reusable prompt templates that can be adapted on the fly, a critical capability for our multi-tenant prompt store.