AI
AI Certified
Professional AI Certification
Get Started
72lesson.complete • 13/18
Courses›Google Gemini Pro›Production & Exam Preparation
📖 Production & Exam Preparation

System Instructions and Role Prompting

⏱ 10 min·Lesson 13 of 18FREE

System Instructions and Role Prompting

Overview

System instructions are persistent instructions that shape the model's behaviour for every turn of a conversation. They define the model's persona, constraints, response format, and domain focus. Mastering system instructions is essential for building reliable, consistent AI-powered applications.

What Are System Instructions?

Unlike user messages (which the model treats as requests), system instructions define the model's operating context. They're processed separately and applied consistently throughout the interaction.

typescript
import { GoogleGenerativeAI } from '@google/generative-ai';

const genAI = new GoogleGenerativeAI(process.env.GEMINI_API_KEY!);

const model = genAI.getGenerativeModel({
  model: 'gemini-2.5-flash',
  systemInstruction: `You are Aria, a friendly customer support agent for TechShop.
  
  Guidelines:
  - Only discuss TechShop products and policies
  - Always be polite and professional
  - If you don't know something, say so and offer to escalate
  - Keep responses concise (under 150 words unless detail is requested)
  - Format prices as £X.XX
  - Never discuss competitors`,
});

const result = await model.generateContent('What is your return policy?');
console.log(result.response.text());

Role Prompting Patterns

Expert Persona

typescript
const legalAnalyst = genAI.getGenerativeModel({
  model: 'gemini-2.5-pro',
  systemInstruction: `You are a senior contract lawyer specialising in UK commercial law.
  Analyse documents with precision, flag risks clearly, and always recommend seeking 
  qualified legal advice for binding decisions. Use formal legal terminology but explain 
  it in plain English.`,
});

Structured Output Enforcement

typescript
const dataExtractor = genAI.getGenerativeModel({
  model: 'gemini-2.5-flash',
  systemInstruction: `Extract information and respond ONLY with valid JSON.
  No explanations, no markdown code fences, no additional text.
  Schema: { "name": string, "email": string | null, "phone": string | null, "company": string | null }`,
});

const result = await dataExtractor.generateContent(
  'Contact: John Smith from Acme Corp, reach him at john@acme.com or 07700 900123'
);
// Returns clean JSON every time

Tone and Style Control

typescript
const contentWriter = genAI.getGenerativeModel({
  model: 'gemini-2.5-flash',
  systemInstruction: `You are a content writer for a fintech startup targeting millennials.
  Tone: conversational, confident, jargon-free
  Style: short sentences, active voice, occasional humour
  Avoid: corporate speak, passive voice, financial jargon without explanation
  Always end articles with a clear call to action`,
});

Dynamic System Instructions

Build system instructions programmatically based on user context:

typescript
function buildSystemInstruction(user: { name: string; plan: string; language: string }) {
  return `You are a personal AI assistant for ${user.name}.
  
  Account tier: ${user.plan}
  ${user.plan === 'premium' ? 'Provide detailed, comprehensive responses.' : 'Keep responses concise.'}
  
  Language preference: Respond in ${user.language}.
  
  Always address the user by their first name (${user.name.split(' ')[0]}).`;
}

const model = genAI.getGenerativeModel({
  model: 'gemini-2.5-flash',
  systemInstruction: buildSystemInstruction({
    name: 'Sarah Johnson',
    plan: 'premium',
    language: 'English',
  }),
});

Best Practices

Be specific, not vague. "Be helpful" is less useful than "Answer questions about our API documentation. If a question is outside API docs scope, say 'I can only help with API questions.'"

Use delimiters to separate sections of your instruction:

code
ROLE: You are a coding assistant...
CONSTRAINTS: Only use TypeScript...  
FORMAT: Always include working code examples...

Test edge cases: Try to break your instruction with adversarial inputs. If users can override your system instruction via the user message, tighten it.

Key Takeaways

  • System instructions persist across all conversation turns
  • Use them to define persona, constraints, output format, and domain scope
  • Build instructions dynamically when context is user-specific
  • Be specific and testable — vague instructions produce inconsistent results
  • Always test with adversarial inputs to find gaps in your instruction
🎓

Enjoyed this lesson?

Unlock all 18 lessons for a one-time payment.

Get Full Access — £29 →