Artificial Intelligence (AI) · Intermediate level

Intermediate level · Module 2: Working with AI Model APIs · Lesson 1 of 3

Calling a Language Model API

Send a conversation to a language model API and read its reply.

In this lesson you will

  • Structure a request with system and user messages
  • Read the response and its token usage

Key concepts

system messageuser and assistant messagesmodel parameterstoken usage

The lesson

Language model APIs take a list of messages. A system message sets the assistant's role and rules; user messages contain the request; previous assistant messages provide conversation history.

Parameters control behaviour: the model name, the maximum length of the reply and settings such as temperature, which affects how varied the output is. Lower values give more consistent answers.

Responses include the generated text and token usage — the number of input and output tokens — which determines cost.

Step by step

  1. Send a simple request with a system and user message.
  2. Print the reply and token usage.
  3. Change temperature and compare outputs.
  4. Add conversation history and continue.

Practice activity

Write a function ask(question) that sends a question with a fixed system message and returns the answer and token counts.

Expected result: A working function with usage reported for each call.

Check your understanding

  1. What is the system message for?

    Show answer

    Setting the assistant's role and rules.

  2. What determines the cost of a call?

    Show answer

    The number of input and output tokens and the model's prices.

Taught live by an instructor — online or in person. Free course lessons are for self-study and do not lead to a certificate — see registration options.