Sub topic 8
Context window, temperature, and other parameters
This chapter assumes you can run a model with ollama run, read model tags, and call the REST API with JSON. Here you will learn which settings change how a model answers and how much text it can handle at once.
The context window
The context window is the maximum number of tokens the model can hold in view at one time. A token is a piece of text, roughly three-quarters of an English word. The window covers your system prompt, the whole chat history, and the reply being generated.
In Ollama the setting is num_ctx. If the conversation grows beyond it, the oldest tokens are dropped and the model forgets them. The default is small (often 2048 or 4096 tokens, depending on your Ollama version), so long documents can be silently truncated.
A larger window costs memory. The model stores a key-value cache for every token in the window, so doubling num_ctx roughly doubles that cache. This memory comes on top of the model weights you sized in the previous chapter.
Common generation parameters
| Parameter | What it controls | Typical range |
|---|---|---|
| `temperature` | Randomness of word choice. Low is focused, high is varied. | 0.0 to 1.5 |
| `top_p` | Samples only from the smallest set of tokens whose probabilities add up to this value. | 0.1 to 1.0 |
| `top_k` | Samples only from the k most likely tokens. | 1 to 100 |
| `num_predict` | Maximum tokens in the reply. | -1 (no limit) or a positive number |
| `seed` | Makes sampling repeatable when set to a fixed integer. | any integer |
| `stop` | A string that ends generation when the model produces it. | any text |
Temperature 0 gives the most predictable output. Use it for extraction and classification. Use 0.7 or higher for brainstorming and creative writing. Change temperature or top_p, but rarely both at once, because their effects overlap.
Setting parameters
You can set them in three places. Each later one overrides the earlier ones.
- A Modelfile, covered in a later chapter, which sets defaults for a model.
- The interactive prompt, with
/set parameter. - The
optionsobject in an API request.
In an interactive session:
>>> /set parameter num_ctx 8192
>>> /set parameter temperature 0.2In an API request:
curl http://localhost:11434/api/generate -d '{
"model": "llama3.2",
"prompt": "List three uses of a hash map.",
"stream": false,
"options": {
"num_ctx": 8192,
"temperature": 0.2,
"num_predict": 200,
"seed": 42
}
}'With a fixed seed and temperature of 0, the same prompt should give the same reply on the same setup.
Common mistakes
- Pasting a long document and getting a poor answer. The input probably exceeded
num_ctxand was cut off. Raisenum_ctx. - Raising
num_ctxpast the model's limit. Every model has a trained maximum. Check it withollama show. Going beyond it degrades quality. - Running out of memory after raising
num_ctx. The key-value cache grew. Lower the value or choose a smaller model. - Expecting identical output with default settings. Without a fixed
seedand a low temperature, replies vary between runs.
Exercise
You send a 10,000-token document to a model running with num_ctx of 4096, and the answer ignores the start of the document. Why, and what do you change?
Show answer
The input is longer than the context window, so the oldest tokens were dropped. Raise num_ctx to a value above 10,000 plus room for the reply (for example 12288), as long as it stays within the model's trained limit and your memory can hold the larger cache.
Exercise
You need a classifier that gives the same label every time for the same input. Which two options would you set?
Show answer
Set temperature to 0 and set a fixed seed.