Talking with LLMs via code

Programming with LLMs for Data Practitioners

Julia Silge | Posit PBC

Hello!

@juliasilge

@juliasilge.com

juliasilge.com

Who are you?

  • You know some R and/or Python
  • You are curious about working with LLMs from code
  • You want hands-on practice, not just slides

Welcome and getting started

Welcome

  • Introductions
  • What to expect from these two days
    • Build on AI (Day 1)
    • Build with AI (Day 2)

Day 1: Build on AI

  1. Talking with LLMs via code (this session!)
  2. Programming with LLMs
  3. Prompt engineering and RAG
  4. Beyond prompts

Your turn

Activity

Introduce yourself to your neighbors!

05:00

Set-up and verify API access

  • How API keys work
  • Where to put them so the workshop code can find them
  • Put your Anthropic API key in either .env (Python) or .Renviron (R) in the working directory:
ANTHROPIC_API_KEY=your_api_key_here

Your turn

Activity

  • Open 01-hello-llm
  • Make your first API call by writing a short “I’m at SIAS 2026” poem or limerick
05:00

Think empirically, be pragmatic

  • The right mindset for working with LLMs
  • Models are not deterministic! Try things, iterate, compare

Anatomy of a conversation

To get a response, you send a message

  • Under the hood, an HTTP request is sent to a provider
  • Message roles: system, user, assistant
  • ellmer and chatlas hide the boilerplate

Your turn

Activity

Open 02-word-game and play a word guessing game with a system prompt like:

You are playing a word guessing game. At each turn, guess the word and tell us what it is.

  • Include a modifier in the first message (e.g. “In British English,” “In pirate,”)
  • Watch how it steers later answers
10:00

The conversation is stateless

  • The model has no memory between requests
  • The library replays history each turn

Demo

What happens across multiple turns, and what happens when you clear the chat?

# demo/01-clearbot/app.py
uv run shiny run app.py

How LLMs work

Tokens as the fundamental unit

  • What is a token?
  • How models predict the next token
  • Why this matters for cost and context length

Demo

What are the per-token probabilities, and where is the model confident vs. guessing?

# demo/01-token-possibilities/app.R
shiny::runApp()

Shinychat basics

Try a chat UI in seconds

  • live_console() / live_browser() in ellmer
  • chat.console() / chat.app() in chatlas

Your turn

Activity

Open 03-live and launch a live chat against your own chat object.

05:00

Thanks!

@juliasilge

@juliasilge.com

juliasilge.com