Brett Klamer

Table of Contents

LLMs

Love ’em or hate ’em…

What is an LLM?

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

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.

MCP

Model Context Protocol (MCP) links local/external data/tools/systems to the session context of an LLM.

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:

  1. Research -> Review
  2. Plan -> Review
  3. Implement -> Review
  4. 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

  1. 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 in research.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>.

  2. Plan
    • Cleared session:

      Read each line of research-human.md and research.md into 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.md and research.md into context.

          <description>

          Read each line of plan.md into context. Perform a thorough and detailed spec-confirmation review. Ensure that no false assumptions or unsupported leaps in logic are present in plan.md. Do not make assumptions. Validate by running or inspecting code, reading package documentation, or searching academic literature. If you discover issues within plan.md, diligently edit the document by creating a unified diff. Prompt me if you need clarification.

        • Human: read and edit plan.md.

  3. Implement
    • Cleared session:

      Carefully read every line of plan.md into 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>.

  4. Test
    • Cleared session:

      Read each line of plan.md and <edited files> into context.

      Create a thorough and detailed test file for function <function> that follows the structure in AGENTS.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>.

Scripted workflow

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
Published: 2026-04-01
Last Updated: 2026-06-27