generate_text: Generate Text

View source: R/core_api.R

generate_textR Documentation

Generate Text

Description

Generate text using a language model. This is the primary high-level function for non-streaming text generation.

When tools are provided, the function automatically executes tool calls and feeds results back to the LLM in a task-state driven runtime. max_steps controls one execution window; the runtime may open another window or finalize from tool observations instead of silently stopping at the boundary.

Usage

generate_text(
  model = NULL,
  prompt,
  system = NULL,
  temperature = 0.7,
  max_tokens = NULL,
  tools = NULL,
  max_steps = 1,
  max_tool_result_errors = 2,
  require_post_tool_protocol = FALSE,
  sandbox = FALSE,
  skills = NULL,
  session = NULL,
  hooks = NULL,
  registry = NULL,
  ...
)

Arguments

model

Either a LanguageModelV1 object, or a string ID like "openai:gpt-4o".

prompt

A character string prompt, or a list of messages.

system

Optional system prompt.

temperature

Sampling temperature (0-2). Default 0.7.

max_tokens

Maximum tokens to generate.

tools

Optional list of Tool objects for function calling.

max_steps

Number of model/tool steps in one execution window. Default 1. The runtime treats this as a budget checkpoint, not as a hard task stop.

max_tool_result_errors

Historical compatibility option. Tool result errors are recorded as task observations; runtime policy decides whether to continue, finalize, ask the user, or block.

require_post_tool_protocol

Logical. If TRUE, after any tool results are returned the model must either make another tool call or wrap its final answer in a ⁠<final_answer>...</final_answer>⁠ block. This is enabled automatically for text-based tool fallback.

sandbox

Logical. If TRUE, enables R-native programmatic sandbox mode. All tools are bound into an isolated R environment and replaced by a single execute_r_code meta-tool. The LLM writes R code to batch-invoke tools, filter data with dplyr/purrr, and return only summary results, dramatically reducing token usage and latency. Default FALSE.

skills

Optional path to skills directory, or a SkillRegistry object. When provided, skill tools are auto-injected and skill summaries are added to the system prompt.

session

Optional ChatSession object. When provided, tool executions run in the session's environment, enabling cross-agent data sharing.

hooks

Optional HookHandler object for intercepting events.

registry

Optional ProviderRegistry to use (defaults to global registry).

...

Additional arguments passed to the model.

Value

A GenerateResult object with text and optionally tool_calls. When max_steps > 1 and tools are used, the result includes:

  • steps: Number of steps taken

  • all_tool_calls: List of all tool calls made across all steps

Examples


if (interactive()) {
  # Using hooks
  my_hooks <- create_hooks(
    on_generation_start = function(model, prompt, tools) message("Starting..."),
    on_tool_start = function(tool, args) message("Calling tool ", tool$name)
  )
  result <- generate_text(model, "...", hooks = my_hooks)
}


aisdk documentation built on May 29, 2026, 9:07 a.m.