Completion and Context

What the Model Sees When You Ask

Last Time

  1. An LLM predicts the next token.
  2. It learns its probabilities from vast text.
  3. A chat app is a client of an LLM’s API.
  4. Send a prompt from the shell or from R.

Key Ideas for Today

  1. Code completion predicts your next tokens.
  2. An LLM API is stateless.
  3. Context is all the text sent with a request.
  4. A system prompt leads every request.

Code Completion

Informal Next Code Prediction

  1. I will show you three commands, one at a time, each one unfinished.
  2. You will have 10 seconds to predict the characters that best complete it.
  3. Here, “best” means the characters most likely to appear next, given what came before, when you look at all code on GitHub.

my_list <- list(1:2, data.frame(v1 = c("a", "b"), v2 = c(T, F)))
second_el_as_df <- my_list[
00:10
library(palmerpenguins)
ggplot(penguins, aes(x = body_mass_g, y = species)) +
  geom_
00:10
sample(1:5, 3,
00:10

Code Completion

A feature of an IDE that sends the code around your cursor to an LLM and shows the predicted next tokens as a suggestion, which you can accept or ignore.

In Positron:

  • Suggestions appear in grey as you type.
  • Tab accepts, Esc dismisses, or just keep typing.
  • Works in R scripts, Quarto documents, and the console.

Before you press Tab

A suggestion is the likely next code. Whether it’s the right next code depends on what you’re trying to do.

# draw a 3-card hand from a 5-card deck
hand <- sample(1:5, 3, replace = TRUE)

Does this code run? Does it do what the comment says?

A Chat Has No Memory

Recall Paris…

library(ellmer)

claude <- chat_claude(model = "claude-haiku-5-5")
claude$chat("The capital of France is")
The capital of France is Paris.
claude$chat("What is the biggest city?")
The biggest city in France is Paris. It has about 2.1 million residents
within its city limits, and its metropolitan area has roughly 12 to 13
million people.

A new chat

claude2 <- chat_claude(model = "claude-haiku-5-5")
claude2$chat("What is the biggest city?")
It depends on how you measure "city":

- Largest metropolitan area: Tokyo, Japan, with about 37 million people
  in its greater metropolitan area.
- Largest city proper (administrative limits): Chongqing, China, with
  about 32 million people, though much of that population is rural.
- Largest by area: Chongqing (administrative area of about 82,000 km²).

If you meant a specific country or measure, let me know.

Same model, same prompt, different answer. What differed?

Where is the memory?

claude
<Chat Anthropic/claude-haiku-5-5 turns=4 input=52 output=128>
── user ──────────────────────────────────────────────────────────
The capital of France is
── assistant [input=14 output=14] ────────────────────────────────
The capital of France is **Paris**.
── user ──────────────────────────────────────────────────────────
What is the biggest city?
── assistant [input=38 output=114] ───────────────────────────────
The biggest city in France is **Paris**. It has about 2.1 million
residents within its city limits, and its metropolitan area has
roughly 12 to 13 million people.

The second request

{
  "model": "claude-haiku-5-5",
  "messages": [
    {"role": "user",
     "content": "The capital of France is"},
    {"role": "assistant",
     "content": "The capital of France is **Paris**."},
    {"role": "user",
     "content": "What is the biggest city?"}
  ]
}

Stateless

A server is stateless if it keeps nothing from one request to the next. An LLM API is stateless: to continue a conversation, the client sends the whole conversation again.

Your turn. I run a third line:

claude$chat("How far is it from Berkeley?")

Write the role of each message in the request, in order. How many are there?

01:00

Context

Context

All of the text that a model reads when it generates a response: the instructions, the earlier turns of the conversation, and any code, data, or documentation sent along with the prompt.

The model has two sources for an answer:

  1. what it learned in training (fixed, and the same for everyone);
  2. what is in the context (chosen by the client, for each request).

A question, with no instructions

plain <- chat_claude(model = "claude-haiku-5-5")
plain$chat(
  "How do I find the mean body mass for each species of penguin in R?"
)
The easiest way is with `dplyr`:

```r
penguins %>%
  group_by(species) %>%
  summarise(mean_body_mass = mean(body_mass_g, na.rm = TRUE))
```

**Base R alternative**:

```r
tapply(penguins$body_mass_g, penguins$species, mean, na.rm = TRUE)
```

The same question, with a system prompt

tutor <- chat_claude(
  model = "claude-haiku-5-5",
  system_prompt = "You are a tutor for Stat 133. Students use the
    tidyverse and the native pipe. Never write the code for them.
    Reply in two sentences: name one function to look at, then ask
    one question."
)
tutor$chat(
  "How do I find the mean body mass for each species of penguin in R?"
)
Look at `group_by()` and `summarize()` from dplyr, since grouping by
species and then summarizing the mean of body mass is the key idea. What
do you think the grouping variable should be called in the penguins
dataset, and which column holds body mass?

System Prompt

Instructions that the client places at the start of the context and sends with every request. It sets the model’s role, its conventions, and what it should and shouldn’t do.

{
  "model": "claude-haiku-5-5",
  "system": "You are a tutor for Stat 133. Students use the ...",
  "messages": [
    {"role": "user",
     "content": "How do I find the mean body mass for each ..."}
  ]
}

What about your data?

library(tibble)
bake_sale <- tibble(item  = c("cookie", "brownie", "scone"),
                    price = c(2, 3, 2.5),
                    sold  = c(40, 25, 12))
claude3 <- chat_claude(model = "claude-haiku-5-5")
claude3$chat("What was the total revenue from my bake sale?")

What will it say?

I don't have any information about your bake sale. I can't see sales
records, receipts, or files unless you share them here.

Put the data in the context

library(readr)
format_csv(bake_sale) |> cat()
item,price,sold
cookie,2,40
brownie,3,25
scone,2.5,12
claude3$chat(
  "Here is my bake sale data:",
  format_csv(bake_sale),
  "What was the total revenue from my bake sale?",
  "Answer in one sentence."
)
Your bake sale brought in a total revenue of $185 (cookies $80,
brownies $75, scones $30).

Is it right?

The model produced $185 the way it produces every token: by prediction.

Your turn. Compute the total revenue from bake_sale yourself, with a dplyr pipeline.

01:00
library(dplyr)
bake_sale |>
  summarize(revenue = sum(price * sold))
# A tibble: 1 × 1
  revenue
    <dbl>
1     185

Guess the Metaphor

An LLM is like a judge: it rules only on what’s entered into the record (the context). At every hearing, the clerk reads the whole record aloud again, starting with the law posted above the bench (the system prompt).

Key Ideas for Today

  1. Code completion predicts your next tokens.
  2. An LLM API is stateless.
  3. Context is all the text sent with a request.
  4. A system prompt leads every request.

For Next Time

  1. Quiz 6 is today and tomorrow.
  2. Project Studio 5 this week.
  3. Next time: how a model runs code.