Part 01›
✎

04. Using LLMs: Tokens, Context and Sampling#

Level: Foundation · Reading time: 25 min · Exercise: 60 min Prerequisites: How Large Language Models Are Trained (tokens and the generate-one-token loop)

In one sentence: After this chapter you can read a model's API parameters, predict what a call will cost and how long it will take, and get output that your code can parse.

Why this matters#

You add an LLM call to a service. It works in the demo. Then the questions start:

All four come from the same handful of settings. None of them is visible in the code that calls the API.

The core idea#

An LLM API is a function with one input, the prompt (everything you send: the system prompt with instructions for the whole conversation, the earlier turns, and the new request), and a few knobs. It returns text produced one token at a time by sampling from a probability distribution (Chapter 02).

A good analogy is a function with a very small stack and a very large code segment. The code segment is the model's parameters, where its knowledge lives, and it is fixed. The stack is the context window, the only thing the call can see while it runs, and it is cleared when the call returns. The analogy breaks on size. A context window holds tens of thousands to millions of tokens. But, as with a stack, anything not placed there this time does not exist.

How it works, step by step#

1. Count everything in tokens#

Yann Dubois, in his Fall 2025 lecture, suggests thinking of tokens as words or sub-words (0:00). His appendix slides explain why they sit between the two: they make shorter sequences than characters, and they cope with typos and new words better than whole words do.

Three practical rules follow:

Context windows are extended after the main pre-training run. Dubois's slides give DeepSeek v3 as an example, extended to 128,000 tokens. Limits differ by model, so read the model's page as of the day you integrate it.

A large window is not a license to fill it. Each token costs money and time on every call, and accuracy on a detail buried in a very long input is not guaranteed.

2. The call does not remember#

Each API call is independent. The provider does not keep your previous turn. For a conversation, you send the whole history every time. Turn nine costs more than turn one because turn nine includes turns one to eight in its input.

Figure 1. A three-turn conversation: what your service sends on each call.

sequenceDiagram
    participant U as User
    participant A as Your service
    participant M as Model API
    U->>A: First message
    A->>M: System prompt and first message
    M-->>A: First answer
    U->>A: Second message
    A->>M: System prompt, first message, first answer, second message
    M-->>A: Second answer
    U->>A: Third message
    A->>M: System prompt and the whole history so far
    M-->>A: Third answer

The API keeps nothing between calls, so the input grows with every turn, while the system prompt at the top stays the same.

3. Control randomness with temperature and top-p#

At each step the model has a probability for every token in its vocabulary, and it picks one. Two parameters shape that choice:

For classification, extraction and JSON, use a low temperature. For brainstorming, or for the multiple samples that self-consistency needs (Chapter 06), use a higher one. Change one of the two parameters and leave the other at its default.

4. Cap the output with max tokens#

The maximum-tokens parameter stops generation after a set number of output tokens. Anthropic's Messages API reference defines it as "The maximum number of tokens to generate before stopping". The model may stop earlier, the limit depends on the model, and as of November 24, 2026 the page also documents that setting it to 0 fills the prompt cache without generating a response (reference).

If the limit is reached, the text is cut, often in the middle of a sentence or a JSON object. Always check the response's stop reason. The same reference documents stop sequences: strings that make the model stop as soon as it writes them.

5. Put stable instructions in the system prompt#

The system prompt is a separate input field for instructions that apply to the whole conversation. The reference describes it: "A system prompt is a way of providing context and instructions to Claude, such as specifying a particular goal or role" (reference). Put the role, the rules and the output format there. Put the user's data in the message.

6. Get structured output#

You want data, not prose. The procedure:

  1. Describe the exact format in the system prompt, or pass a schema if your provider's API offers one.
  2. Ask for one object and nothing else.
  3. Parse and validate the answer in your own code.
  4. On failure, retry once or twice, including the validation error in the retry. Then fall back to a safe default.

Many providers also offer a mode that constrains the output to a schema. Names and guarantees differ by provider, so check the current documentation. Even then, keep your validator. A valid JSON object can still be an incorrect one.

7. Reuse work with prompt caching#

Because each call resends the history, the model reprocesses the same prefix, the unchanged beginning of the prompt, again and again. Prompt caching stores the processed form of a prefix on the provider's side, so a later request that starts with the same prefix is cheaper and quicker to start. In the Messages API, you mark the cacheable prefix with cache_control. The cached entry lives for 5 minutes by default, or 1 hour in a longer option, as of November 24, 2026 (reference).

To benefit, order the prompt so that the stable part comes first: system prompt, long reference document, then the varying user message. If something changes near the top, everything after it must be processed again.

8. A cost and latency mental model#

Worked example#

An estimator for one call. The constants are illustrative. Replace them with your provider's current prices and with speeds you measure yourself.

PRICE_IN, PRICE_OUT = 0.50, 2.00     # dollars per million tokens (example prices)
TOKENS_PER_SECOND = 60               # example speed once generation has started
SECONDS_PER_1K_INPUT = 0.4           # example processing time before the first token

def estimate(prompt_tokens, output_tokens, history_tokens=0):
    """Cost in dollars and latency in seconds for one call."""
    input_tokens = history_tokens + prompt_tokens
    cost = input_tokens / 1e6 * PRICE_IN + output_tokens / 1e6 * PRICE_OUT
    latency = input_tokens / 1000 * SECONDS_PER_1K_INPUT + output_tokens / TOKENS_PER_SECOND
    return round(cost, 5), round(latency, 1)

print(estimate(prompt_tokens=500, output_tokens=200))                        # (0.00065, 3.5)
print(estimate(prompt_tokens=500, output_tokens=200, history_tokens=8_000))  # (0.00465, 6.7)

This script calls no API and costs nothing. To calibrate it you will make real calls. At the example prices above, 20 short calls cost just over one cent.

What to observe:

The shape of the request, in illustrative pseudocode. Field names follow one provider's API and differ elsewhere:

{
  "system": "Classify the ticket as billing, bug or question. Answer with one lowercase word.",
  "messages": [{"role": "user", "content": "The export button does nothing when I click it."}],
  "max_tokens": 10,
  "temperature": 0
}

What the practitioners say#

The Berkeley lectures leave this ground to the API documentation. Dubois closes his with a list of topics he did not cover, and "decoding & inference" and "context size" are both on it (his outlook slide). What his lecture does supply is the mechanism behind the settings:

Evan Miller's paper, covered in Chapter 07, adds the rule for evaluations: measure at the settings you deploy. Do not lower the temperature only to make the numbers calm.

Pitfalls#

  1. Assuming the API remembers the last turn. Fix: resend the history, and budget for it.
  2. Treating the context window as the model's memory or knowledge. It is the working memory of one call. Fix: send what the call needs. For facts, use retrieval, a search that puts the matching documents into the prompt.
  3. Setting max tokens too low. Fix: allow enough for the largest valid answer, and check the stop reason.
  4. Expecting perfect determinism at temperature 0. Model updates and small numerical differences still change outputs. Fix: never compare raw output strings for equality. Testing generative and LLM components shows what to do instead.
  5. Changing temperature and top-p together. You cannot tell which one caused the change. Fix: adjust one.
  6. Filling the context because it fits. Fix: send only the relevant material, with the stable part first so caching applies.
  7. Parsing free text without validation. Fix: validate, retry with the error message, then fall back.
  8. Estimating the bill from input tokens only. Fix: count output tokens, which cost more and take the time.

Try it#

Deliverable: a calibration report for one model and one task.

  1. Write a request with a system prompt, a structured output requirement, a max-tokens value and a low temperature.
  2. Count the tokens of a typical request. Write down the provider's current input and output prices, with the date.
  3. Run the request 20 times. Measure time to first token and total time.
  4. Replace the constants in the estimator with what you measured. Predict the cost and latency of a call with a 5,000-token prompt and 500 output tokens. Run it and compare.
  5. Run the request with streaming on and off, and note when the first characters appear.

This makes about 25 paid calls, more with the stretch goal. At the example prices above they cost under two cents in total, and under ten with the stretch goal. Real prices depend on the model and the provider.

You are done when: your report has a two-column table, predicted against measured, for cost and for latency, and a sentence explaining any estimate that was off by more than a factor of two.

Stretch goal: put a 5,000-token document at the start of ten requests and enable prompt caching, if your provider offers it. Compare cost or latency against the same ten requests without caching.

Key takeaways#

Self-check#

  1. What does the context window limit? - A. The tokens of the input plus the output of one call - B. The tokens a model can generate over its whole lifetime - C. The number of API calls allowed per minute - D. The space the model's parameters take on disk

  2. What does lowering the temperature do? - A. It cuts the length of every answer in half - B. It removes the need for a system prompt - C. It makes the model pick more probable tokens more often - D. It makes the model pay less attention to the prompt

  3. What does top-p do? - A. It keeps exactly p tokens at every step - B. It keeps the smallest set of tokens whose probabilities add up to p - C. It stops the answer after p hundred words - D. It raises the price per token by p percent

  4. A response ends in the middle of a sentence. What do you check first? - A. The temperature, because it must be above zero - B. The system prompt, because it must be missing - C. The cache, because it must have expired - D. The stop reason, because the token limit may be reached

  5. Why does the ninth turn of a chat cost more than the first? - A. Providers charge more as a conversation goes on - B. Each call resends the whole conversation as input - C. The model becomes slower the more it has written - D. Later turns are set to produce longer answers

  6. What does prompt caching reuse? - A. The final answers given to earlier requests - B. A copy of the model's parameters on your machine - C. The processing of a prefix sent in an earlier request - D. The random choices made in earlier calls

Show answers 1. A. Input and output share one window. A long prompt leaves little room for the answer. 2. C. Low temperature concentrates probability on the likeliest tokens, which makes output more repeatable. 3. B. The tail of unlikely tokens is removed before the choice is made. 4. D. Generation stops at the maximum-tokens limit and the text is cut. Raise the limit or shorten the answer. 5. B. The API has no memory. Turn nine carries turns one to eight in its input. 6. C. A request that starts with the same cached prefix skips reprocessing that prefix, within the cache's lifetime.

Sources#

Next#

→ Prompting and In-Context Learning. You can now call the model. The next chapter is about what to put in the call.