Module 1 - Lesson 3b: System Prompt

Defining AI role and behavior with system prompts.

Published: 1/3/2026

Example 2: System Prompt

System prompts define the AI's role, personality, and behavior. This is crucial for getting consistent, role-appropriate responses.

What It Does

Adds a system message to define the AI as a "travel assistant" before the user's question. This gives context and sets expectations.

Code Snippet

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

import OpenAI from "openai";
import dotenv from "dotenv";

dotenv.config();
const openai = new OpenAI();

async function basicPromptWithSystem(): Promise<void> {
  try {
    console.log("Testing OpenAI connection...");

    // Using message array with system and user roles
    const response = await openai.responses.create({
      model: "gpt-5-nano",
      input: [
        {
          role: "system",
          content: "You are a helpful travel assistant.",
        },
        {
          role: "user",
          content: "Suggest a travel destination",
        },
      ],
    });

    console.log("✅ Basic Prompt Success!");
    console.log("AI Response:", response.output_text);
    console.log("Tokens used:");
    console.dir(response.usage, { depth: null });
  } catch (error) {
    if (error instanceof OpenAI.APIError) {
      console.log("❌ API Error:", error.status, error.message);
    } else if (error instanceof Error) {
      console.log("❌ Error:", error.message);
    }
  }
}

basicPromptWithSystem().catch(console.error);

Run It

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

Key Points

  • System role: Defines AI's identity and behavior
  • User role: Contains the actual question
  • Better responses: AI knows its role and responds accordingly
  • Use case: When you need consistent, role-specific responses

System Prompt Best Practices

// ❌ Too vague
content: "You are helpful.";

// ✅ Specific and clear
content: "You are a helpful travel assistant specializing in European destinations.";

// ✅ With constraints
content: "You are a travel assistant. Always provide practical, budget-friendly options.";