Anatomy of a conversation

Programming with LLMs in R and Python

posit::conf(2026)

2026-09-14

How to think about LLMs

Think Empirically, Not Theoretically

Think Empirically, Not Theoretically

  • It’s okay to (mostly) treat LLMs as black boxes.
    We’re not going to focus on how they work internally

  • Just try it! When wondering if an LLM can do something,
    experiment rather than theorize

  • You might think they could not possibly do things
    that they clearly can do today

  • And you might think surely they can do something
    that it turns out they’re terrible at

LLMs are jagged

Embrace the Experimental Process

Start Simple, Build Understanding

Start Simple, Build Understanding

  • We’re going to focus on the core building blocks.

  • All the incredible things you see AI do
    decompose to just a few key ingredients.

Anatomy of a Conversation

We can do this from R and Python!

R

Python

A complete example

R ellmer

library(ellmer)

chat <- chat_posit()

chat$chat("Tell me a fact about cats.")
#> **Cats can't taste sweetness.**
#> They lack the specific taste receptor gene
#> (Tas1r2) that allows most mammals to
#> detect sugary flavors.

A complete example

Python chatlas

import chatlas

chat = chatlas.ChatPosit()

chat.chat("Tell me a fact about cats.")
#> **Cats can't taste sweetness.**
#> They lack the specific taste receptor gene
#> (Tas1r2) that allows most mammals to
#> detect sugary flavors.

How does this work?

Most LLMs are accessible through HTTP APIs

R ellmer

library(ellmer)

Python chatlas

import chatlas

R ellmer

library(ellmer)

chat <- chat_posit()

Python chatlas

import chatlas

chat = chatlas.ChatPosit()

R ellmer

library(ellmer)

chat <- chat_posit()

chat$chat("Tell me a fact about cats.")

Python chatlas

import chatlas

chat = chatlas.ChatPosit()

chat.chat("Tell me a fact about cats.")

R ellmer

chat
<Chat Posit turns=2>
── user ─────────────────────────────────────────────
Tell me a fact about cats.
── assistant ────────────────────────────────────────
Cats have a special reflective layer behind their retinas that
helps them see in very low light.

Python chatlas

print(chat)
## User turn:

Tell me a fact about cats.

## Assistant turn:

Cats have a special reflective layer behind their retinas that
helps them see in very low light.

Messages have roles.

Messages have roles

Messages have roles

Messages have roles

Messages have roles

Message roles

Role Description
system Instructions that set the behavior of the assistant
user Messages from the person interacting with the assistant
assistant The AI model’s responses to the user

System prompts

R ellmer

library(ellmer)

chat <- chat_posit(
  system_prompt = "Answer in haikus."
)

chat$chat("Tell me a fact about sheep.")

Python chatlas

import chatlas

chat = chatlas.ChatPosit(
    system_prompt="Answer in haikus."
)

chat.chat("Tell me a fact about sheep.")

System prompts

R ellmer

library(ellmer)

chat <- chat_posit(
  # Set the response style
  system_prompt = "Answer in haikus."
)

chat$chat("Tell me a fact about sheep.")
#> Woolly white cloud grows
#> Sheep never sleep flat on ground
#> Fear keeps them standing.

Python chatlas

import chatlas

chat = chatlas.ChatPosit(
    # Set the response style
    system_prompt="Answer in haikus."
)

chat.chat("Tell me a fact about sheep.")
#> Woolly white cloud grows
#> Sheep never sleep flat on ground
#> Fear keeps them standing.

The chat object

R ellmer

chat
<Chat Posit turns=3>
── system ─────────────────────────
Answer in haikus.
── user ───────────────────────────
Tell me a fact about sheep.
── assistant ──────────────────────
Woolly white cloud grows
Sheep never sleep flat on ground
Fear keeps them standing.

Python chatlas

print(chat)
## System turn:
Answer in haikus.

## User turn:
Tell me a fact about sheep.

## Assistant turn:
Woolly white cloud grows
Sheep never sleep flat on ground
Fear keeps them standing.

Your Turn 02_conversation

  1. Set up a chat with this system prompt:

    Answer in as few words as possible.

  2. Ask: What creates an Anthropic chat in ellmer and chatlas?

  3. Ask: What about OpenAI?

  4. Then, create a new chat with no system prompt and ask only the second question: What about OpenAI?

  5. How do the answers to 3 and 4 differ? Why?

Demo: clearbot

👨‍💻 _demos/03_clearbot/app.py

System prompt:

Answer in as few words as possible.

First question:

What creates an Anthropic chat in ellmer and chatlas?

Second question:

What about OpenAI?

How to talk to robots

Is this actually a conversation?

LLMs are stateless

  • The LLM doesn’t remember anything between requests

  • For each response, the LLM receives the conversation history again

  • The LLM reconstructs the “conversation” from that history

ChatGPT

Chatting with a Generative Pre-trained Transformer

LLMLarge Language Model

How to make an LLM

If you read everything
ever written…

  • Books and stories

  • Websites and articles

  • Poems and jokes

  • Questions and answers


…then you could…

  • Answer questions
  • Write stories
  • Tell jokes
  • Explain things
  • Translate between languages

The cat sat in the ____

  • 🎩
  • 🛌
  • 📦
  • 🪟
  • 🛒
  • 👠

Actually: tokens, not words

  • Fundamental units of information for LLMs
  • Words, parts of words, or individual characters
    • “hello” → 1 token
    • “unconventional” → 3 tokens: un|con|ventional
  • Important for:
    • Model input/output limits
    • API pricing is usually by token
  • Not just words, but images can be tokenized too

Demo:
token-possibilities

👨‍💻 _demos/04_token-possibilities/app.R

Programming is fun, but I kind of like ChatGPT…

ellmer and chatlas can do that, too!

Console Browser
ellmer live_console(chat) live_browser(chat)
chatlas chat.console() chat.app()

Your Turn 05_live

  1. Your job: write a groan-worthy roast of Hadley Wickham

  2. Bonus points for puns, rhymes, and one-liners

  3. Don’t be mean

  4. Share your best with a neighbor.