Getting Started with LLM APIs in R
2025-09-16

| Role | Description |
|---|---|
system_prompt |
Instructions from the developer (that’s you!) to set the behavior of the assistant |
user |
Messages from the person interacting with the assistant |
assistant |
The AI model’s responses to the user |

❓ What are the user and assistant roles in this example?
<Chat OpenAI/gpt-4.1 turns=2 tokens=15/23 $0.00>
── user [15] ───────────────────────────────────────────────────────────────────────────────────
tell me a quick fact about sheep
── assistant [23] ──────────────────────────────────────────────────────────────────────────────
Sheep have excellent memories—they can remember up to 50 individual sheep faces and even recognize human faces for years!❓ What about the system prompt?
ellmer
<Chat OpenAI/gpt-4.1 turns=3 tokens=22/22 $0.00>
── system [0] ───────────────────────────────────────────────────────────────────────────────────────────
Always answer in haikus.
── user [22] ────────────────────────────────────────────────────────────────────────────────────────────
What is chirality?
── assistant [22] ───────────────────────────────────────────────────────────────────────────────────────
Left and right differ—
mirrored, yet not the same thing.
Chirality's twist.02_word-gameSet up a chat with a system prompt:
You are playing a word guessing game. At each turn, guess the word and tell us what it is.
Ask: In British English, guess the word for the person who lives next door.
Ask: What helps a car move smoothly down the road?
Create a new, empty chat and ask the second question again.
How do the answers to 3 and 4 differ? Why?
05:00
clearbot👩💻 _demos/03_clearbot/app.py
System prompt:
First question:
Second question:

The LLM doesn’t remember anything between requests
You have to send the entire conversation history with every message
The LLM reconstructs the “conversation” from what you send
If you read everything
ever written…
Books and stories
Websites and articles
Poems and jokes
Questions and answers
…then you could…
un|con|ventionaltoken-possibilities👩💻 _demos/04_token-possibilities/app.R
It’s okay to (mostly) treat LLMs as black boxes.
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


Explore!
Focus on learning and engaging with the technology, not outcomes
Failure is valuable!
those are some of the most interesting conversations that we have
It doesn’t have to be a success.
Attempts that don’t work still provide insights
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.
Our goal is to have fun and build intuition
through hands-on experience.
| Console | Browser | |
|---|---|---|
![]() |
live_console(chat) |
live_browser(chat) |
live👩💻 _demos/05_live/05_live.R


Start with a basic shinyapp
Load {shinychat} and {ellmer}
Use the shinychat chat module
Create and hook up a chat client to use in the app
03_word-gamesI’ve set up the basic Shiny app snippet and a system prompt.
Your job: create a chatbot that plays the word guessing game with you.
The twist: this time, you’re guessing the word.
07:00