Sub topic 10
OpenAI-style and Anthropic-style system prompts compared
This chapter assumes you can read Python, know what a list of dictionaries is, and have seen a chat message with a role and content. You should also know that chat APIs are stateless, so you send the whole history on every call.
Two ways to carry the same instruction
Every chat API needs a place for the instruction that sets behavior, tone, and constraints. Providers made two different choices:
- OpenAI-style: the system prompt is the first item in the
messageslist, withroleset to"system". - Anthropic-style: the system prompt is a separate top-level
systemparameter. Themessageslist holds onlyuserandassistantturns.
The idea is the same in both. The difference is where you put the text and what the list is allowed to contain.
OpenAI-style: system as a message
In the Chat Completions API, the system prompt is an ordinary message. It sits at index 0 of the history, and you keep it there for the whole conversation.
from openai import OpenAI
client = OpenAI()
history = [
{"role": "system", "content": "You are a helpful assistant. Keep answers under three sentences."}
]
def chat(user_text):
history.append({"role": "user", "content": user_text})
response = client.chat.completions.create(
model="MODEL_NAME", # replace with a chat model available to you
messages=history,
)
reply = response.choices[0].message.content
history.append({"role": "assistant", "content": reply})
return reply
print(chat("What is a list in Python?"))
print(chat("How do I add an item to it?"))Because the system prompt is just the first message, it travels with the history automatically. You do not need to pass it separately.
Anthropic-style: system as a parameter
In the Anthropic Messages API, you pass the system prompt as system= and leave it out of messages.
import anthropic
client = anthropic.Anthropic()
response = client.messages.create(
model="MODEL_NAME", # replace with a model available to you
max_tokens=300,
system="You are a helpful assistant. Keep answers under three sentences.",
messages=[
{"role": "user", "content": "What is a list in Python?"},
],
)
print(response.content[0].text)Two other differences show up in this example. max_tokens is a required argument. The reply text is read from response.content[0].text, not from choices[0].message.content.
Side-by-side comparison
| Aspect | OpenAI-style | Anthropic-style |
|---|---|---|
| Where the system prompt goes | First entry in `messages` | Top-level `system` parameter |
| Roles allowed in `messages` | `system`, `user`, `assistant`, and others | `user` and `assistant` |
| Reading the reply | `response.choices[0].message.content` | `response.content[0].text` |
| Output length limit | Optional | `max_tokens` required |
Common errors when switching providers
- Sending a
systemmessage to the Anthropic API. The Messages API expects onlyuserandassistantroles inmessages, so asystementry is rejected. Move that text to thesystemparameter. - Forgetting the system prompt on an Anthropic call. The parameter is not part of your history list. If you build
historyonce and reuse it, store the system text separately and pass it every time. - Dropping the system message when trimming. With the OpenAI style, a naive slice such as
history[-10:]can cut off the first message. Keep it explicitly. - Forgetting
max_tokens. Anthropic calls fail without it.
A trimming helper written for the OpenAI style keeps system messages and the latest turns:
def trim_history(messages, max_turn_messages=10):
system_messages = [m for m in messages if m["role"] == "system"]
others = [m for m in messages if m["role"] != "system"]
return system_messages + others[-max_turn_messages:]With the Anthropic style, the same helper is simpler to reason about. The system text is never in the list, so slicing cannot remove it.
Writing code that works with both
If you support several providers, keep your own neutral record: a system_text string and a turns list of user and assistant messages. Convert at the edge, just before the call:
- For OpenAI-style, build
[{"role": "system", "content": system_text}] + turns. - For Anthropic-style, pass
system=system_textandmessages=turns.
This keeps provider details out of the rest of your program.
Exercise
You wrote a chat loop for the OpenAI API where history starts with a system message. You port it to the Anthropic API and keep history unchanged. The call fails. What do you change?
Show answer
Remove the system message from history and pass its text as the top-level system= argument. Keep only user and assistant messages in messages, and make sure max_tokens is set.
Exercise
Why is it safer to trim history with the Anthropic style than with a plain slice in the OpenAI style?
Show answer
In the Anthropic style the system prompt is not in the messages list, so slicing the list cannot remove it. In the OpenAI style the system prompt is the first message, so a plain slice like history[-10:] can drop it unless you keep it explicitly.