BeginnerCoder

Lesson 10 — Talking to a real model API

What a real API call looks like

Until now fake_llm stood in for the model. A real provider (Anthropic, OpenAI, Google) runs the model on its servers, and your code sends it a request over the internet. The request is plain data — in Python, a dict:

  • model — which model to use, written as a string (each provider publishes its own model names).
  • max_tokens — the most tokens the model may write in its reply.
  • messages — the same list of role/content dicts you built in Lesson 2.

The response is a dict too. In Anthropic's Messages API it has a content list of blocks (a text block is {"type": "text", "text": "..."}), a stop_reason, and usage: input_tokens (what you sent) and output_tokens (what the model wrote). OpenAI's and Google's field names differ, but every provider reports token usage.

Why it matters: providers charge per token, usually as a price per 1 million tokens, with output tokens costing more than input tokens. The cost of one call is:

input_tokens * input_price / 1_000_000 + output_tokens * output_price / 1_000_000

Watch out: the prices in this lesson are made-up example numbers, not any provider's real prices — check the provider's pricing page. And a real call also needs an API key: it lives in an environment variable on your computer, never in your code or in a lesson. Here fake_api stands in for the server, so no key and no internet are needed.

Want it another way?
Task

Finish call_cost(usage): input tokens times the input price plus output tokens times the output price, each divided by 1,000,000.

Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.

← Back
Next →
Test your knowledge

A quick 3-question quiz on this lesson — instant feedback.

Enjoying this lesson? Share it.