Module 2 - Lesson 2b: System Prompt with Anthropic

Defining AI role and behavior with system prompts in Anthropic Claude.

Published: 1/10/2026

Lesson 2b: System Prompt with Anthropic Claude

System prompts define Claude's role, personality, and behavior. Learn how Anthropic handles system prompts differently from OpenAI.

Key Difference from OpenAI

OpenAI: System prompt is part of the messages array

messages: [
  { role: "system", content: "You are a helpful assistant" },
  { role: "user", content: "Hello" }
]

Anthropic: System prompt is a separate top-level parameter

system: "You are a helpful assistant",
messages: [
  { role: "user", content: "Hello" }
]

Code Example

Create src/anthropic/basic-prompt-with-system.ts:

import Anthropic from "@anthropic-ai/sdk";
import dotenv from "dotenv";

// Load environment variables
dotenv.config();

// Create Anthropic client with typed configuration
const anthropic = new Anthropic();

// Async function with proper return type
async function basicPromptWithSystem(): Promise<void> {
  try {
    console.log("Testing Anthropic connection...");

    // Make API call - response is automatically typed!
    // Using a system prompt along with user prompt
    const response = await anthropic.messages.create({
      model: "claude-haiku-4-5",
      max_tokens: 1000,
      system: "You are a helpful travel assistant.",  // System prompt here!
      messages: [{ role: "user", content: "Suggest a travel destination" }],
    });

    console.log("✅ Basic Prompt with System Success!");
    // show response usage
    console.log("Tokens used:");
    console.dir(response.usage, { depth: null });

    // Check if we got a response
    if (!response.content || response.content.length === 0) {
      throw new Error("No content in response");
    }

    // Extract text
    const textBlocks = response.content.filter(
      (block) => block.type === "text"
    );

    if (textBlocks.length === 0) {
      throw new Error("No text content in response");
    }

    console.log(
      "AI Response:",
      textBlocks.map((block) => block.text).join("\n")
    );
  } catch (error) {
    // Proper error handling with type guards
    if (error instanceof Anthropic.APIError) {
      console.log("❌ API Error:", error.status, error.message);
    } else if (error instanceof Error) {
      console.log("❌ Error:", error.message);
    } else {
      console.log("❌ Unknown error occurred");
    }
  }
}

// Run the test
basicPromptWithSystem().catch((error) => {
  console.error("Error:", error);
});

Run It

pnpm tsx src/anthropic/basic-prompt-with-system.ts

System Prompt Best Practices

1. Be Clear and Specific

Bad:

system: "You help people"

Good:

system: "You are a helpful travel assistant with expertise in European destinations. Provide detailed, practical travel advice."

2. Define Personality and Tone

system: "You are a friendly, enthusiastic travel expert. Use a warm, conversational tone while providing accurate information."

3. Set Constraints

system: "You are a travel assistant. Always include: 1) Destination details 2) Best time to visit 3) Budget estimate. Keep responses under 200 words."

4. Specify Format

system: "You are a travel assistant. Format all responses with clear sections: Overview, Attractions, Practical Tips."

Comparison: With vs Without System Prompt

Without System Prompt

const response = await anthropic.messages.create({
  model: "claude-haiku-4-5",
  max_tokens: 1000,
  messages: [{ role: "user", content: "Suggest a travel destination" }]
});

Response: Generic travel suggestion

With System Prompt

const response = await anthropic.messages.create({
  model: "claude-haiku-4-5",
  max_tokens: 1000,
  system: "You are a budget travel expert specializing in off-the-beaten-path destinations.",
  messages: [{ role: "user", content: "Suggest a travel destination" }]
});

Response: Budget-friendly, unique destination with cost-saving tips


Advanced System Prompts

Multi-Part System Prompts

system: `You are a professional travel assistant with the following guidelines:

**Expertise:**
- European destinations
- Budget travel tips
- Cultural experiences

**Response Format:**
1. Destination name and location
2. Why visit (2-3 sentences)
3. Best time to visit
4. Estimated budget
5. Top 3 attractions

**Tone:** Friendly, informative, and encouraging`

Role-Based System Prompts

// Code reviewer
system: "You are an experienced TypeScript developer. Review code for best practices, potential bugs, and performance issues."

// Creative writer
system: "You are a creative writing coach. Provide constructive feedback on story structure, character development, and prose."

// Technical explainer
system: "You are a patient teacher explaining complex technical concepts to beginners. Use analogies and simple language."

System Prompt vs User Prompt

System Prompt (Persistent Role)

  • Sets the AI's personality
  • Defines expertise area
  • Establishes response format
  • Stays the same across conversation

User Prompt (Specific Request)

  • What you want the AI to do
  • Changes with each message
  • Specific questions or tasks

Example Conversation

// Persistent system
system: "You are a travel assistant specializing in Europe"

// First user message
messages: [{ role: "user", content: "Suggest a destination in France" }]

// Second user message
messages: [
  { role: "user", content: "Suggest a destination in France" },
  { role: "assistant", content: "..." },
  { role: "user", content: "What about Italy?" }
]

// System prompt affects BOTH responses with European expertise

Key Takeaways

  • ✅ System prompt is a separate system parameter (not in messages)
  • ✅ Use system prompts to define role, personality, and constraints
  • ✅ System prompts persist across the entire conversation
  • ✅ Be specific about tone, format, and expertise

Next Steps

Learn how to control response creativity with temperature!

Next: Lesson 2c - Temperature Control →


Quick Reference

// Minimal system prompt example
const response = await anthropic.messages.create({
  model: "claude-haiku-4-5",
  max_tokens: 1000,
  system: "You are a helpful travel assistant.",
  messages: [{ role: "user", content: "Suggest a destination" }]
});