agent: Chat with tool use (agentic mode)

View source: R/agent.R

agentR Documentation

Chat with tool use (agentic mode)

Description

Send a prompt to an LLM with tools. Automatically handles tool calls in a loop until the model responds with text only.

Usage

agent(prompt, tools = list(), tool_handler = NULL, system = NULL, model = NULL,
      provider = c("anthropic", "openai", "moonshot", "ollama"),
      max_turns = 20L, verbose = TRUE, history = NULL, ...)

Arguments

prompt

Character. The user message.

tools

List. Tool definitions (from mcp_tools_for_claude or manual).

tool_handler

Function. Called with (name, args), returns result string.

system

Character. System prompt.

model

Character. Model name.

provider

Character. Provider: "anthropic", "openai", "moonshot", or "ollama".

max_turns

Integer. Maximum tool-use turns (default: 20).

verbose

Logical. Print tool calls and results.

history

List or NULL. Previous conversation history to continue from.

...

Additional parameters passed to the API.

Value

List with final response and conversation history.

Examples

## Not run: 
# With MCP server
conn <- mcp_connect("r", "mcp_server.R")
tools <- mcp_tools_for_claude(conn)

result <- agent(
  "What files are in the current directory?",
  tools = tools,
  tool_handler = function(name, args) {
    mcp_call(conn, name, args)$text
  }
)

## End(Not run)

llm.api documentation built on April 16, 2026, 5:08 p.m.