Step by Step
M
Model and Messages — what to run and what to say
The "model" parameter selects which specific LLM to use, while "messages" is an array of role/content pairs: system (persistent instructions), user (input), and assistant (previous responses).
Example: an API call specifying a particular model, along with a messages array containing a system message setting the AI's role, plus the user's actual question.
M2
Max tokens — capping output length
The "max tokens" parameter sets the maximum length of the generated output, preventing runaway or excessively long responses.
Example: setting max_tokens to 500 to ensure a response doesn't exceed roughly 500 tokens in length, regardless of how much more the model might otherwise generate.
T
Temperature — controlling randomness
Temperature controls how deterministic or creative the output is: 0 produces fully deterministic output, 0.7 offers a balanced middle ground, and 1.0+ produces more creative, varied output.
Example: setting temperature to 0 for a factual data-extraction task requiring consistent, repeatable output, versus setting it to 1.0 for creative writing tasks wanting more varied, surprising output.
C
Cost — input tokens plus output tokens
API cost is calculated as input tokens plus output tokens, charged per million tokens — the practical optimization strategy is using the smallest, cheapest model capable of adequately solving the task.
Example: choosing a smaller, cheaper model for a simple classification task rather than defaulting to the largest, most expensive model available, since the smaller model adequately solves this particular task.
Applied Walkthrough
1
A developer is setting up an API call for a customer support chatbot that needs consistent, predictable responses.
2
They set temperature to a low value (like 0.2) rather than a high value, since consistency matters more than creative variation for this particular use case.
3
They also set an appropriate max_tokens value to prevent excessively long responses, and structure their messages array with a system message establishing the chatbot's persistent role, followed by the user's actual question.
4
To manage cost, they choose the smallest model that adequately handles this relatively straightforward customer support task, rather than defaulting to the most expensive, largest available model — since cost is calculated based on both input and output tokens.
Exam Application
Exams test whether you can name and describe the key API parameters (model, messages, max tokens, temperature) and whether you understand the practical cost optimization principle: choosing the smallest model that adequately solves the task, since cost scales with both input and output token counts.
⚠ Common Trap
The most common trap is assuming a higher temperature is always better for more "natural" or higher-quality output. Temperature should be matched to the specific use case: low temperature (like 0) for tasks requiring consistency and determinism, higher temperature for tasks wanting creative variation — neither setting is universally "better."
✓ Quick Self-Check
1. What do the three roles in the messages array represent?
System (persistent instructions), user (input), and assistant (previous responses).
Tap to reveal / hide
2. What does the max tokens parameter control?
The maximum length of the generated output.
Tap to reveal / hide
3. What does a temperature of 0 produce, compared to 1.0+?
Temperature 0 produces fully deterministic output; 1.0+ produces more creative, varied output.
Tap to reveal / hide
4. How is API cost typically calculated?
Input tokens plus output tokens, charged per million tokens.
Tap to reveal / hide
5. What is the practical cost optimization strategy recommended in this lesson?
Using the smallest, cheapest model capable of adequately solving the task, rather than defaulting to the largest available model.
Tap to reveal / hide