System Prompts

Use the system field to set Claude's role, rules, and tone for the whole conversation.

The system prompt is a separate top-level field that shapes how Claude behaves across the entire conversation. It is where you set the model's role, rules, and output style.

System vs. user

  • system — persistent instructions: "You are a helpful assistant for a cooking site. Answer only cooking questions."
  • user — the actual question or request for this turn.

Good uses for the system prompt

  • Define the assistant's persona and scope.
  • State formatting rules ("answer in short bullet points").
  • Give background the model should always keep in mind.

Example

Example · javascript
const response = await client.messages.create({
  model: "claude-opus-4-8",
  max_tokens: 512,
  system:
    "You are a friendly assistant for a recipe website. " +
    "Only answer cooking and food questions. " +
    "Keep answers under 80 words and use plain language.",
  messages: [
    { role: "user", content: "How do I stop pasta from sticking?" },
  ],
});

When to use it

  • Set a system prompt to make Claude always respond in French for an international customer-support bot.
  • Use the system field to enforce JSON-only output for an internal data-extraction pipeline.
  • Give Claude a 'helpful coding tutor' persona so it explains code in a beginner-friendly tone across the whole session.

More examples

System prompt sets Claude's role

The system field sets global behaviour for the entire conversation without occupying a user turn.

Example · javascript
const msg = await client.messages.create({
  model: "claude-haiku-4-5",
  max_tokens: 128,
  system: "You are a concise technical writer. Always respond in plain text, no markdown.",
  messages: [{ role: "user", content: "What is an API?" }],
});

Enforce JSON output via system prompt

Combining a strict system instruction with a parsing step enforces structured output for data pipelines.

Example · javascript
const msg = await client.messages.create({
  model: "claude-sonnet-4-5",
  max_tokens: 256,
  system: "Respond ONLY with valid JSON. No prose, no code blocks, no explanation.",
  messages: [{ role: "user", content: "Extract name and price from: Blue Widget $9.99" }],
});
console.log(JSON.parse(msg.content[0].text));

System prompt with app-specific rules

Shows a multi-rule system prompt that scopes Claude's behaviour to a specific product and tone.

Example · javascript
const system = `You are the support assistant for AcmeCorp.
Rules:
- Only answer questions about AcmeCorp products.
- If the user asks about competitors, say you cannot help.
- Always end with "Is there anything else I can help with?"`;  

const msg = await client.messages.create({
  model: "claude-haiku-4-5",
  max_tokens: 256,
  system,
  messages: [{ role: "user", content: "How do I reset my password?" }],
});

Discussion

  • Be the first to comment on this lesson.