Send your first request and read the response

Send a working request from your own machine and read every part of the response.

Priya has a provider account, a spending limit, an API key stored as an environment variable and a rough idea of what a call costs. Priya has not yet sent a request. Set aside about half an hour to do that now.

You need the provider account from Lesson 1.1, the key set up as in Lesson 1.2, "Keep your API key on the server, never in the browser", and an AI coding assistant.

Trying the request in the playground first

Every major provider has a playground in its developer console. Some providers call it a workbench. It's a web page where you type a prompt and adjust settings without writing code. Open it and pick a model. Write a short system message, for example "You summarise customer feedback in one sentence". Then paste a piece of customer feedback in as the user message and run it.

Look at the settings panel. It shows the model name, a maximum output length and usually a randomness setting called temperature. Many playgrounds also have a button that shows the request as code. If yours has one, open it and read the request. Your script will send this same request from your own machine, so once you've read it here, the script will hold no surprises.

Asking the assistant for the script

Open your AI coding assistant in an empty project folder and ask for exactly what you want. Priya asked for a short Python script that calls the model API using the provider's official Python library and reads the key from the environment variable MODEL_API_KEY. The script sends one system message and one user message and prints the full raw response. It then prints the reply text, the input token count and the output token count on separate lines.

The official library is the one the provider publishes and documents. It handles details such as retries and current request formats. If you're unsure which library is the official one, ask the assistant for it by name. When you name the environment variable in your request, the assistant has no reason to write the key into the file. If it writes the key in anyway, refuse the change.

Install the library with the command the assistant gives you, then run the script from the terminal.

Reading each field of the response

On the first run, don't skip straight to the reply. Go through the printed data one field at a time. You'll find:

an ID for the response the model that actually answered the reply itself, often nested inside a list, because some settings let you ask for more than one reply a stop reason, which says why the reply stopped: either the model finished naturally or it hit the length limit a usage section with the input and output token counts, which are what you are billed on

Priya's run showed 41 input tokens and 23 output tokens (example figures), with a stop reason saying the model had finished naturally.

Next, open the usage page in the provider console. It can take a few minutes to update. Find your request and check that its token counts match the ones your script printed.

Changing one setting at a time

Change only one setting per run so you know what caused each difference. Write down what you saw after each change.

Start by setting the max output length very low, 10 tokens for example, and run the script again. The reply stops mid-sentence and the stop reason changes to say it hit the limit. Later in the course, your code will use the stop reason field to detect cut-off replies.

Put the max output length back to normal and run the same request three times. The wording usually comes out slightly different each time. Then lower the temperature and run it three more times. The replies should be more alike. Some newer models don't accept a temperature setting at all, and their documentation will say so. If your model is one of them, skip that part.

Last, make the system message much longer, a full paragraph of instructions, and run it again with the same user message. The input token count goes up even though the user message hasn't changed.

If the script fails at any point, read the error before you change anything. An authentication error usually means the environment variable isn't set in the terminal you ran the script from. A 429 error means you've hit a rate limit, so wait and try again. Lesson 1.3, "Tokens, pricing and limits you have to plan around", explains rate limits. For any other error, paste the whole error into the assistant and ask what it means before you ask it to fix anything.

The script, response and notes to keep

You should end up with three things. The first is a script that runs from the terminal and reads the key from the environment, with no key anywhere in the file. The second is a saved copy of one full response. The third is your notes beside that response. They should explain in your own words what each field means and what happened when you changed each setting.

Once your notes would make sense to someone who has never seen an API response, you are ready for the task below.

Run a script that sends one request and prints the reply and the token usage. Save the output, with a note next to each field saying what it means.

Course

Junxiong-WFG Organisation is an authorised representative of AIA Financial Advisers Private Limited (Reg. No. 201715016G).