0PricingLogin
AI Engineering Academy · Lesson

The Chat Completions Endpoint

Understand the messages array with system, user, and assistant roles, craft your first prompt, and interpret the response object that comes back from the API.

The Messages Array Architecture

The Chat Completions endpoint runs on a messages array: a list of turns, each with a role (system, user, or assistant). The model is stateless, so you send the history every time.

from openai import OpenAI

client = OpenAI()

response = client.chat.completions.create(
    model='gpt-4o-mini',
    messages=[
        {'role': 'system', 'content': 'You are a concise Python tutor.'},
        {'role': 'user', 'content': 'What is a list comprehension?'}
    ]
)

print(response.choices[0].message.content)

The System Role: Defining Behavior

The system message is your biggest lever. It sets the model's persona, rules, and format before the user types a word. Invest time here — it shapes everything. See the code.

system_prompt = '''You are a customer support agent for TechShop.
You help customers with: order tracking, returns, and product questions.
You do NOT discuss pricing changes or competitor products.
Always respond in 2-3 sentences maximum.
If you cannot help, say: 'Let me connect you with a human agent.'
'''

response = client.chat.completions.create(
    model='gpt-4o-mini',
    messages=[
        {'role': 'system', 'content': system_prompt},
        {'role': 'user', 'content': 'Where is my order #12345?'}
    ]
)

All lessons in this course

  1. Setting Up Your Python Environment
  2. The Chat Completions Endpoint
  3. Controlling Model Behavior with Parameters
  4. Error Handling and Rate Limits
← Back to AI Engineering Academy