| agent | R Documentation |
Send a prompt to an LLM with tools. Automatically handles tool calls in a loop until the model responds with text only.
agent(prompt, tools = list(), tool_handler = NULL, system = NULL, model = NULL,
provider = c("anthropic", "openai", "moonshot", "ollama"),
max_turns = 20L, verbose = TRUE, history = NULL, ...)
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. |
List with final response and conversation history.
## 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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.