Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 16.3 — Calling an LLM API from Python
Python · Example 1 of 2

Two request shapes side by side

Compare where the system text sits in each body.

Calling a language model is sending a list of messages (system, user, assistant) as JSON over HTTP and reading text back. Different providers use slightly different shapes, so you hide the call behind a small function and test everything else with a fake client.

Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.

Ready for more? This phase has 6 hands-on exercises with automatic checks.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Write build_messages(system, history, user) that returns the full list with the system message first and the new user message last.

02

Write openai_text(response) and anthropic_text(response) that pull the answer text out of each shape (as plain dictionaries).

03

Write load_key(env, name) that returns the key from a dictionary of settings, or raises RuntimeError with a helpful message.

Explain it without notes

01

Why must you send the whole conversation on every call?

02

What are the main differences between the OpenAI-compatible and Anthropic Messages request and response shapes?

03

How do you make code that calls an LLM testable?

04

A model reply arrives but your JSON parser fails on it. Before blaming the prompt, what do you check in the response object?

Two request shapes side by side
Sign in to run this example in your browser.

Expected output

OpenAI-compatible request body:
{
  "model": "your-model-name",
  "messages": [
    {
      "role": "system",
      "content": "You are a concise tutor."
    },
    {
      "role": "user",
      "content": "What is a list comprehension?"
    }
  ]
}
Anthropic Messages request body:
{
  "model": "your-model-name",
  "max_tokens": 200,
  "system": "You are a concise tutor.",
  "messages": [
    {
      "role": "user",
      "content": "What is a list comprehension?"
    }
  ]
}