← Building with AI

System prompts

Standing instructions that shape every answer.

Start this lesson

A system prompt gives the model its job and rules for the whole conversation: who it is, who it is talking to, what tone to use, what to avoid. It is not part of the back-and-forth. In the Anthropic API you pass it with the system parameter, separate from messages. A good system prompt saves you from repeating the same instructions in every user message.

# To run on your own computer with an API key (it cannot run in this browser).
# First: pip install anthropic, and set the ANTHROPIC_API_KEY environment variable.
import anthropic
client = anthropic.Anthropic()

reply = client.messages.create(
    model="claude-sonnet-5-5",
    max_tokens=300,
    system="You are a friendly science tutor for 12-year-olds. Use short sentences and one everyday example.",
    messages=[{"role": "user", "content": "Why is the sky blue?"}],
)
print(reply.content[0].text)

The same question gets a different answer for a different system prompt, for example one written for university students.

Some other providers put the system prompt inside the messages list with role 'system'. The idea is the same; only where you put it changes. Read your provider's docs.

The pretend model only knows one system trick: if the system prompt mentions a pirate, it starts with 'Arr!'.