Table of Contents
LLMs
Love ’em or hate ’em…
What is an LLM?
- Weights file
- The trained neural network parameters.
- Stored in binary files: .bin, .safetensors, etc.
- https://huggingface.co/openai/gpt-oss-20b/tree/main
- https://huggingface.co/google/gemma-4-26B-A4B-it/tree/main
- Configuration and architecture files
- Defines how to interpret and connect the weights.
config.jsonspecifies the parameters and hyperparameters- Python (or whatever) files define how the weights are connected and how input is passed through the model to produce output.
- https://huggingface.co/nvidia/Nemotron-Cascade-2-30B-A3B/tree/main
- A runtime environment
- The framework for interacting with weights, linking to math libraries, tokenization, text interface, etc.
- https://pytorch.org/
- https://github.com/ggml-org/llama.cpp
- https://ollama.com/
- https://lmstudio.ai/
To use an analogy:
- weights -> regression model coefficients, \(\beta\)
- configuration/architecture -> Model formula, \(y = X\beta\)
- runtime environment -> Takes \(X=x\) and calculates \(\hat{y}\)
Context
A language model predicts the next token as a conditional probability:
\[P(x_{t+1} \mid x_1, x_2, \ldots, x_t; \theta) = \text{softmax}(f(x_1, x_2, \ldots, x_t; \theta))\]
where
- \(x_{t+1}\) is the predicted token
- \(x_1\) through \(x_t\) are all tokens currently in the context window
- \(\theta\) represents the fixed model parameters
- \(f\) represents the transformer network and softmax converts its output into a probability distribution.
Context tokens are weighted for importance through the attention mechanism. Tokens near the beginning and end of the window tend to receive higher attention than tokens in the middle. Tokens that fall outside the window are not used. For additional information, see https://bactra.org/notebooks/nn-attention-and-transformers.html
Persistent context
- https://code.claude.com/docs/en/memory#claude-md-files
- https://developers.openai.com/codex/guides/agents-md
- https://geminicli.com/docs/cli/gemini-md/
Global
Global context is the user-defined system prompt loaded into context for all sessions.
Typically a configuration file, such as CLAUDE.md.
See here for example.
Project
Project context is the user-defined system prompt loaded into context for all sessions of a particular project.
Place at root of project directory, named something like CLAUDE.md.
Claude can automatically generate a project specific CLAUDE.md file using /init.
Session Context
Session context is the user-defined system prompt and other supporting information loaded into context for a specific session.
- PDFs converted to markdown/latex using https://mathpix.com
- Documentation
?bkmisc::save_manual()?bkmisc::save_help()?bkmisc::save_vignette()?bkmisc::save_to_md()
- Code
MCP
Model Context Protocol (MCP) links local/external data/tools/systems to the session context of an LLM.
- https://posit-dev.github.io/btw/
Install in R
pak::pak("posit-dev/btw")Link to claude
claude mcp add -s "user" r-btw -- Rscript -e "btw::btw_mcp_server(tools = 'docs')"See tools: https://posit-dev.github.io/btw/reference/btw_tools.html
Skills
Skills are folders of instructions, scripts, and resources the LLM can load dynamically to improve performance on specialized tasks.
- https://github.com/vincentarelbundock/marginaleffects-SKILL
- https://github.com/harrelfe/skills
- https://github.com/scunning1975/MixtapeTools
- https://github.com/posit-dev/skills
- https://github.com/api2r/pkgskills
- https://github.com/ab604/claude-code-r-skills
- https://bitbucket.org/bklamer/claude/src/master/skills/
Configuration
Global and project level settings are typically saved in a configuration file such as settings.json.
See here for example.
Interactive Workflow
Spec-driven development. In summary:
- Research -> Review
- Plan -> Review
- Implement -> Review
- Test -> Review
Prerequisites
- Topic to implement:
<topic>. The basic discovery query on Google is “<topic>”. - Detailed description of what you want to implement:
<description>
Steps
- Research
Human: research the topic and save context to
research-human.md.Cleared session:
<description>Search the web for
<topic>from authoritative academic sources. Thoroughly examine the results so you fully comprehend the details. When finished, save a comprehensive report about your findings inresearch.md.Human: read and edit
research.md.For file in [
research-human.md,research.md]- For attempt in [1, …, N]
- For model in [claude, codex, gemini]
Cleared session:
Read each line of
<file>into context.Perform a detailed and skeptical review of this document to ensure accuracy and completeness. Ensure that no false assumptions or unsupported leaps in logic are present. Do not make assumptions. Validate by running or inspecting code, reading package documentation, or searching academic literature. If you discover issues within the document, diligently edit the document by creating a unified diff. Prompt me if you need clarification.
Human: read and edit
<file>.
- For model in [claude, codex, gemini]
- For attempt in [1, …, N]
- Plan
Cleared session:
Read each line of
research-human.mdandresearch.mdinto context.<description>Write a detailed implementation plan in
plan.md.Human: read and edit
plan.md.For attempt in [1, …, N]
- For model in [claude, codex, gemini]
Cleared session:
Read each line of
research-human.mdandresearch.mdinto context.<description>Read each line of
plan.mdinto context. Perform a thorough and detailed spec-confirmation review. Ensure that no false assumptions or unsupported leaps in logic are present inplan.md. Do not make assumptions. Validate by running or inspecting code, reading package documentation, or searching academic literature. If you discover issues withinplan.md, diligently edit the document by creating a unified diff. Prompt me if you need clarification.Human: read and edit
plan.md.
- For model in [claude, codex, gemini]
- Implement
Cleared session:
Carefully read every line of
plan.mdinto context. Implement this plan as documented. Prompt me if you need clarification. When finished, summarize any deviations from this plan.Human: read and edit
<edited files>.For attempt in [1, …, N]
- For model in [claude, codex, gemini]
Cleared session:
Perform a detailed and skeptical review of
<edited files>to ensure there are no bugs in the implementation of<topic>. Ensure that no false assumptions or unsupported leaps in logic are present. Do not make assumptions. Validate by running or inspecting code, reading package documentation, or searching academic literature. If you discover issues within<edited files>, diligently edit<edited files>by creating a unified diff. Prompt me if you need clarification.Human: read and edit
<edited files>.
- For model in [claude, codex, gemini]
- Test
Cleared session:
Read each line of
plan.mdand<edited files>into context.Create a thorough and detailed test file for function
<function>that follows the structure inAGENTS.md.Human: read and edit
<test file>.For attempt in [1, …, N]
- For model in [claude, codex, gemini]
Cleared session:
Perform a detailed and skeptical review of
<test file>to ensure the test cases are complete. If you discover issues or missing test cases within the file, diligently edit the file by creating a unified diff. Prompt me if you need clarification.Human: read and edit
<test file>.
- For model in [claude, codex, gemini]
Scripted workflow
Automated spec-development
Command line scripting
# Files to process
files <- list.files(".", pattern = "\\.txt$", full.names = TRUE)
# Session prompt
prompt <- "Review this file and extract ..."
# Process each file
# See https://code.claude.com/docs/en/cli-reference
results <- lapply(files, function(file) {
system2(
command = "claude",
args = c(
"--print", # print response to stdout
"--max-turns", "2", # help prevent runaway token use
shQuote(paste("For the file", file, prompt))
),
stdout = TRUE,
stderr = FALSE
)
})
# Inspect results
names(results) <- basename(files)
results
Last Updated: 2026-06-27