AlgoMaster Logo

Exercise: Making Your First LLM API Call

3 min readUpdated June 22, 2026
Listen to this chapter
Unlock Audio

Exercise 1: Send Your First Completion

Make a single chat completion call through OpenRouter, print the model's answer, and print the token counts for the request.

This is the smallest complete unit of an AI application: send messages, get a response, account for the tokens you used. Everything else in the course builds on this call, so it is worth getting the shape of it into your fingers.

Requirements

  • Create a client pointed at OpenRouter.
  • Send a system message and a user message to openai/gpt-5.4-mini.
  • Print the model's reply.
  • Print the input, output, and total token counts from response.usage.
main.py
Loading...

Expected Output

You get a coherent answer followed by the token accounting. The wording of the answer varies between runs; the structure of the output does not.

Solution

main.py
Loading...

The only thing that makes this an OpenRouter call rather than a direct OpenAI call is the base_url. The messages list carries the system instruction and the user question, the reply lives at response.choices[0].message.content, and response.usage reports the exact token counts the provider billed, which is the number you log and budget against in a real application.

Exercise 2: Hold a Two-Turn Conversation

A single completion has no memory. To hold a conversation, you resend the whole message history each turn, including the model's own previous replies. Send a first message, append the reply, then ask a follow-up that can only be answered from the earlier turn.

Requirements

  • Send a first user message and print the reply.
  • Append the reply to messages as an assistant turn.
  • Append a follow-up user question that depends on the first message.
  • Send the updated history and print the second reply, which should recall the earlier detail.
main.py
Loading...

Expected Output

The second reply correctly recalls the detail from the first turn, proving the model saw the history.

Solution

main.py
Loading...

The model is stateless: it only knows what is in the messages you send. Appending the assistant's reply and then the follow-up rebuilds the full history, so the second call can answer a question about the first turn. Drop the assistant turn or the original message and the model loses the context, which is exactly why conversation management is about maintaining this list, not about the model remembering anything on its own.