How to beginner · 3 min read

How to use system prompt to set AI persona

Quick answer
Use the system prompt parameter to define the AI's persona or role before user messages. This sets context and tone for all responses, for example, by specifying "You are a helpful assistant" in system when calling chat.completions.create.

PREREQUISITES

  • Python 3.8+
  • OpenAI API key (free tier works)
  • pip install openai>=1.0

Setup

Install the OpenAI Python SDK and set your API key as an environment variable.

  • Run pip install openai to install the SDK.
  • Set your API key in your shell: export OPENAI_API_KEY='your_api_key_here' (Linux/macOS) or setx OPENAI_API_KEY "your_api_key_here" (Windows).
bash
pip install openai

Step by step

Use the system parameter to set the AI persona before sending user messages. This example uses the OpenAI SDK gpt-4o model to create a helpful assistant persona.

python
import os
from openai import OpenAI

client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

response = client.chat.completions.create(
    model="gpt-4o",
    system="You are a helpful assistant that provides concise answers.",
    messages=[
        {"role": "user", "content": "What is the capital of France?"}
    ]
)

print(response.choices[0].message.content)
output
Paris is the capital of France.

Common variations

You can use the system prompt with other models like claude-3-5-sonnet-20241022 from Anthropic or switch to streaming responses. Here's an example with Anthropic's SDK setting a persona as a friendly tutor.

python
import os
import anthropic

client = anthropic.Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])

message = client.messages.create(
    model="claude-3-5-sonnet-20241022",
    max_tokens=200,
    system="You are a friendly tutor who explains concepts clearly.",
    messages=[{"role": "user", "content": "Explain photosynthesis."}]
)

print(message.content[0].text)
output
Photosynthesis is the process by which green plants use sunlight to convert carbon dioxide and water into glucose and oxygen.

Troubleshooting

If the AI ignores your persona instructions, ensure the system parameter is correctly passed and not included as a user message. Also, verify your SDK version supports the system parameter and you are using the correct model names.

Key Takeaways

  • Use the system prompt to set the AI's persona and tone before user messages.
  • Pass the persona as a separate system parameter, not as a user message.
  • The system prompt works across OpenAI and Anthropic SDKs with current models.
  • Verify SDK versions and model names to ensure support for the system parameter.
  • Customize personas to improve response relevance and style for your application.
Verified 2026-04 · gpt-4o, claude-3-5-sonnet-20241022
Verify ↗