Agents

Tools in a Loop

Last Time

  1. A tool is a function the client offers.
  2. The model requests a call; the client runs it.
  3. The result joins the context.
  4. A tool is described, like an endpoint.

Key Ideas for Today

  1. An agent calls tools in a loop.
  2. A mode sets which calls run.
  3. A trace backs up each number.
  4. Check a plot reading with numbers.

Review

Where we left off

chat3$chat(
  "I have a data frame called babynames. Which girl's name
   was given most often in 1995? Answer in one sentence."
)
◯ [tool call] run_r_code(code = "babynames %>% filter(year == 1995,
    sex == \"F\") %>% slice_max(n, n = 1) %>% select(name, n)")
● #> # A tibble: 1 × 2
  #>   name        n
  #>   <chr>   <int>
  #> 1 Jessica 27935
The most popular girls' name in 1995 was Jessica, given to 27,935
baby girls.

How many requests did that one $chat() send?

One more question

chat3$chat("Was Jessica still the top name the next year?")

With a neighbor: what does the model need to find out? Write down each tool call you expect, in order. How many requests will this send?

01:30

The Agent Loop

Around and around

◯ [tool call] run_r_code(code = "babynames %>% filter(year == 1996,
    sex == \"F\") %>% slice_max(n, n = 1) %>% select(name, n)")
● #> # A tibble: 1 × 2
  #>   name      n
  #>   <chr> <int>
  #> 1 Emily 25151
◯ [tool call] run_r_code(code = "babynames %>% filter(year == 1996,
    sex == \"F\", name == \"Jessica\") %>% select(name, n)")
● #> # A tibble: 1 × 2
  #>   name        n
  #>   <chr>   <int>
  #> 1 Jessica 24194
No. In 1996, Emily took the top spot with 25,151 births, and
Jessica dropped to second with 24,194.

How many requests this time? Who decided when to stop?

The loop, in R

Roughly what $chat() does each time you call it:

turns <- c(turns, user_message)
repeat {
  reply <- send_request(system_prompt, tools, turns)   # one API request
  turns <- c(turns, reply)
  if (!asks_for_tools(reply)) break                    # answered in text
  results <- run_tools(reply)                          # R runs them, here
  turns <- c(turns, results)
}

Agent

A model, a set of tools, and a loop. The client sends a request, runs any tools the model asks for, and sends back the results, until the model answers in text.

An Agent in Your IDE

Posit Assistant

Tool What it does
read, ls, search Reads and finds files in your folder
executeCode Runs R in your R session
bash Runs a shell command
edit, write Changes or creates a file
AskUser Asks you a question

What it sends, every time

  1. Posit’s system prompt
  2. Description of every tool
  3. AGENTS.md, if your folder has one
  4. Your R session: the names and types of your objects, not their values
  5. Files you attach with @
  6. The whole conversation so far, every tool call and result included

AGENTS.md

A file of instructions in your project folder, sent with every request:

# Stat 133 conventions

- Use the tidyverse and the native pipe, |>.
- Before summarizing a data frame, say what one row represents.
- Don't change files outside this folder.

Which line would have fixed Wednesday’s %>%?

Who approves each call?

Mode Tool calls
Ask Reads and searches only. Shows you code to run yourself.
Normal Asks you before it runs code or edits a file.
Auto A second model rates each call’s risk, and asks you only when it judges it should.
Plan Reads only, writes a plan, and waits for your OK.
Cleaning Audits a data set and asks you each cleaning decision. Monday.

Mode

The client’s rule for which tool calls run without asking you. The model only requests calls; the mode decides which ones the client runs on its own.

Live: the same question in Positron

Which girl's name was given most often in 1995,
and was it still the top name in 1996?

Normal mode. Before each Approve:

  • What will this call do?
  • What do you expect it to print?

Reading a Trace

Trace

The record of an agent’s work: each request, the tool calls the model asked for with their arguments, and the results the client sent back.

In ellmer, print the chat object. In Posit Assistant, export the conversation as JSON.

Follow the numbers

◯ [tool call] run_r_code(code = "babynames %>% filter(year == 1996,
    sex == \"F\") %>% slice_max(n, n = 1) %>% select(name, n)")
● #> 1 Emily 25151
◯ [tool call] run_r_code(code = "babynames %>% filter(year == 1996,
    sex == \"F\", name == \"Jessica\") %>% select(name, n)")
● #> 1 Jessica 24194
No. In 1996, Emily took the top spot with 25,151 births, and
Jessica dropped to second with 24,194.

Question: for each claim in the last sentence, find the tool result behind it. Is there a claim with no result behind it?

02:00

Does it see the plot?

Paste into Posit Assistant, in Normal mode:

Make a scatterplot of mpg against hp in mtcars with ggplot2.
What relationship does the plot show?

Before it answers: what do you expect the plot to show?

What we didn’t tell it

# before class: flip hp
library(dplyr)
mtcars <- mtcars |>
  mutate(hp = max(hp) - hp)

Working Safely with Agents

  1. Commit before it edits. git diff shows every change.
  2. Read each call before you approve it.
  3. Follow each number in the answer to a tool result.
  4. Restart the chat when it wanders.

Guess the Metaphor

An agent is like a giant in the forest: it covers ground in strides you can’t keep up with, but every step leaves a footprint. Follow the footprints (the trace) to see where it really went.

Project 6

Second Opinion

Hand a question from a previous project to Posit Assistant, then check its work:

  1. Provide it the exact prompt from a project assignment, in Normal mode.
  2. Follow one of its numbers through the trace.
  3. Test its code against your own.
  4. Change one thing (the prompt or the model) and run it again.
Step Tool call Result Claim it supports
1 run_r_code: top girls’ name, 1996 Emily, 25,151 “Emily took the top spot with 25,151”
2

Key Ideas for Today

  1. An agent calls tools in a loop.
  2. A mode sets which calls run.
  3. A trace backs up each number.
  4. Check a plot reading with numbers.

For Next Time

  1. Sign up for Quiz 7 (12/1-12/4).
  2. PS 7 due 12/5.
  3. Monday: cleaning data with an agent.