Skip to contents

Constructs an LLM-based agent for generating, debugging, explaining, or optimizing R code using structured prompts. The agent handles retries and provides comprehensive code assistance.

Arguments

llm

A function that accepts a character prompt and returns an LLM response.

system_prompt

Optional system-level instructions for the agent's behavior.

user_input

The user's task/query (e.g., "Write function to filter NAs").

max_tries

Maximum number of attempts for LLM calls (default: 3).

backoff

Seconds to wait between retries (default: 2).

verbose

Logical controlling progress messages (default: TRUE).

Value

A list containing:

  • input - The user's original query

  • llm_response - The processed LLM response

  • system_prompt - The system instructions used

  • success - Logical indicating if call succeeded

  • attempts - Number of tries made

Examples

if (FALSE) { # \dontrun{
coder_agent <- build_code_agent(
  llm = my_llm_wrapper,
  user_input = "Write an R function to standardize numeric columns in a data frame using dplyr.",
  max_tries = 3,
  backoff = 2,
  verbose = FALSE
)
} # }