Command Palette

Search for a command to run...

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

A fake client and a chat loop

The fake has the same method path as an OpenAI-style SDK. Change the scripted replies and run again.

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?

A fake client and a chat loop
Sign in to run this example in your browser.

Expected output

   system: Be concise.
     user: What is a list comprehension?
assistant: A list comprehension builds a list in one line.
     user: Can it filter?
assistant: Yes: add an if at the end.
calls made: 2
messages sent on call 2: 4