Lesson Introduction

Welcome! Today, we’ll explore a key part of working with language models: completion parameters. If you want to control how creative, factual, short, or long an AI’s response is, this lesson is for you. Our goal is to understand what completion parameters are, why they matter, and how to use them to shape AI model responses. By the end, you’ll know how to experiment with these parameters in your own code.

What Are Completion Parameters?

When you use the OpenAI API, you send a prompt and get a response. But you can also control how the model responds using completion parameters. Completion parameters are settings that influence the model’s output. Think of them as dials to adjust the model’s behavior. The most common are:

  • temperature: Controls randomness and creativity.
  • max_tokens: Limits response length.
  • top_p: Another way to control randomness.
  • n: Number of completions to generate.

We’ll focus on temperature and max_tokens — the two most important for shaping responses, whether you want concise answers or creative stories.

Deep Dive: Temperature Parameter

The temperature parameter controls how creative the model’s output is. It ranges from 0 to 2, with 0.0 being very predictable and 1.0 being more creative.

  • Low temperature (e.g., 0.2): Output is focused and predictable.
  • High temperature (e.g., 1.0): Output is more diverse and creative.

A low temperature is best for reliable, factual, or consistent answers, such as summaries or Q&A. A high temperature is ideal for creative, varied, or brainstorming responses, like stories or idea generation. Adjusting this parameter lets you control the model’s creativity to fit your needs.

If you set temperature to an extreme value, such as 2.0, the model’s responses can become highly random, sometimes to the point of being nonsensical or off-topic. This is rarely useful in practice, but it can be interesting for experimentation or generating unexpected ideas. On the other hand, setting temperature to 0.0 makes the model as deterministic as possible, always returning the most likely next word.

Here’s a simple example:

Run this code and notice: a low temperature gives a straightforward story, while a high temperature adds creative twists.

Exploring max_tokens

The max_tokens parameter sets the maximum length of the model’s response. A token is about a word or a few characters. Use max_tokens to keep responses short or allow longer outputs. For example, to get a brief answer, set max_tokens=20. For more detail, use max_tokens=100 or higher.

If you set max_tokens to 0, the model will not generate any output at all. This is generally not useful, but it’s good to know that setting this parameter too low can prevent the model from responding. Conversely, setting max_tokens very high allows for long, detailed responses, but may use more resources and take longer to generate.

The first response is concise; the second is more detailed. This is useful for fitting responses into a specific space or controlling verbosity.

Experimenting with Parameters in Code

Let’s combine these ideas with a code example that lets you try different temperature values and see the results.

Output example:

This code loops through three temperature values and prints the model’s response for each. Try running it and see how the story changes. A higher temperature means more creative stories. You can also change max_tokens to see how it affects response length. Experimenting like this helps you master completion parameters.

Lesson Summary and Practice Introduction

You learned what completion parameters are and why they matter. We focused on temperature (controls creativity) and max_tokens (controls length). You saw how to use them in code and how they affect the model’s output. Adjusting these lets you tailor AI responses for your needs, from concise answers to creative stories.

Now it’s your turn! Next, you’ll get hands-on practice with completion parameters. You’ll adjust temperature and max_tokens to see their effects. This will help you confidently control AI behavior in your own projects. Let’s begin!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal