Skip to contents

The main class for creating AI agents that can use tools to accomplish tasks. Agent wraps an ellmer Chat object and adds agentic capabilities including multi-turn execution, permission enforcement, and streaming output.

Skill Methods

The following methods manage skills:

$load_skill(skill, allow_conflicts = FALSE)

Load a Skill into the agent. The skill parameter can be a Skill object or path to a skill directory. If allow_conflicts is FALSE (default), an error is thrown when skill tools conflict with existing tools. Set to TRUE to allow overwriting. Returns invisible self.

$skills()

Get a named list of loaded Skill objects.

MCP Methods

The following methods manage MCP (Model Context Protocol) server tools:

$load_mcp(config = NULL, servers = NULL)

Load tools from MCP servers. The config parameter specifies the path to the MCP config file (defaults to ~/.config/mcptools/config.json). The servers parameter optionally filters to specific server names. Requires the mcptools package. Returns invisible self.

$mcp_tools()

Get names of loaded MCP tools.

Public fields

chat

The wrapped ellmer Chat object

permissions

Permission policy for the agent

working_dir

Working directory for file operations

hooks

Hook registry for lifecycle events

Methods


Method new()

Create a new Agent.

Usage

Agent$new(
  chat,
  tools = list(),
  system_prompt = NULL,
  permissions = NULL,
  working_dir = getwd()
)

Arguments

chat

An ellmer Chat object created by ellmer::chat() or provider-specific functions like ellmer::chat_openai().

tools

A list of tools created with ellmer::tool(). See tools_file() and tools_code() for built-in tool bundles.

system_prompt

Optional system prompt. If provided, overrides the chat object's existing system prompt.

permissions

A Permissions object controlling what the agent can do. Defaults to permissions_standard().

working_dir

Working directory for file operations. Defaults to current directory.

Returns

A new Agent object


Method run()

Run an agentic task with streaming output.

Returns a generator that yields AgentEvent objects as the agent works. The agent will continue until the task is complete, max_turns is reached, or the cost limit is exceeded.

Usage

Agent$run(task, max_turns = NULL)

Arguments

task

The task for the agent to perform

max_turns

Maximum number of turns (default: from permissions)

Returns

A generator yielding AgentEvent objects


Method run_sync()

Run an agentic task and block until completion.

Convenience wrapper around run() that collects all events and returns an AgentResult.

Usage

Agent$run_sync(task, max_turns = NULL)

Arguments

task

The task for the agent to perform

max_turns

Maximum number of turns (default: from permissions)

Returns

An AgentResult object


Method register_tool()

Register a tool with the agent.

Usage

Agent$register_tool(tool)

Arguments

tool

A tool created with ellmer::tool()

Returns

Invisible self for chaining


Method register_tools()

Register multiple tools with the agent.

Usage

Agent$register_tools(tools)

Arguments

tools

A list of tools created with ellmer::tool()

Returns

Invisible self for chaining


Method add_hook()

Add a hook to the agent.

Hooks are called at specific points during agent execution and can modify behavior (e.g., deny tool calls, log events).

Usage

Agent$add_hook(hook)

Arguments

hook

A HookMatcher object

Returns

Invisible self for chaining

Examples

\dontrun{
# Add a hook to block dangerous bash commands
agent$add_hook(hook_block_dangerous_bash())

# Add a custom PreToolUse hook
agent$add_hook(HookMatcher$new(
  event = "PreToolUse",
  pattern = "^write_file$",
  callback = function(tool_name, tool_input, context) {
    cli::cli_alert_info("Writing to: {tool_input$path}")
    HookResultPreToolUse(permission = "allow")
  }
))
}


Method turns()

Get the conversation history.

Usage

Agent$turns()

Returns

A list of Turn objects


Method last_turn()

Get the last turn in the conversation.

Usage

Agent$last_turn(role = "assistant")

Arguments

role

Role to filter by ("assistant", "user", or "system")

Returns

A Turn object or NULL


Method cost()

Get cost information for the conversation.

Usage

Agent$cost()

Returns

A list with input, output, cached, and total token costs


Method provider()

Get provider information.

Usage

Agent$provider()

Returns

A list with provider name and model


Method save_session()

Save the current session to an RDS file.

Usage

Agent$save_session(path)

Arguments

path

Path to save the session

Details

The session file contains:

  • Conversation turns

  • System prompt

  • Tool definitions (serialized)

  • Permissions configuration

  • Working directory

  • Metadata (timestamp, version, provider info)

Returns

Invisible path


Method load_session()

Load a session from an RDS file.

Usage

Agent$load_session(path, restore_tools = TRUE)

Arguments

path

Path to the session file

restore_tools

If TRUE (default), restore tools from session

Details

Note: Hooks are NOT restored from sessions as they contain function closures that may not serialize correctly.

Returns

Invisible self


Method compact()

Compact the conversation history to reduce context size.

This method uses the LLM to generate a meaningful summary of older conversation turns, then replaces them with the summary appended to the system prompt. This preserves important context while reducing token usage.

Usage

Agent$compact(keep_last = 4, summary = NULL)

Arguments

keep_last

Number of recent turns to keep uncompacted (default: 4)

summary

Optional custom summary to use instead of auto-generating. If NULL, the LLM will generate a summary focusing on key decisions, findings, files discussed, and task progress.

Details

The compaction process:

  1. Fires the PreCompact hook (can cancel or provide custom summary)

  2. If no custom summary, uses LLM to summarize compacted turns

  3. Appends summary to system prompt under "Previous Conversation Summary"

  4. Keeps only the most recent keep_last turns

If LLM summarization fails (e.g., no API key), falls back to a simple text-based summary with truncated turn contents.

Returns

Invisible self


Method print()

Print the agent configuration.

Usage

Agent$print()


Method load_mcp()

Load tools from MCP (Model Context Protocol) servers.

Requires the mcptools package. Issues a warning if not installed or if tool fetching fails.

Usage

Agent$load_mcp(config = NULL, servers = NULL)

Arguments

config

Path to MCP configuration file. If NULL (default), uses the mcptools default location (~/.config/mcptools/config.json).

servers

Optional character vector of server names to load from. If NULL, loads from all configured servers.

Returns

Invisible self for chaining


Method mcp_tools()

Get names of loaded MCP tools.

Usage

Agent$mcp_tools()

Returns

Character vector of MCP tool names


Method load_skill()

Usage

Agent$load_skill(skill, allow_conflicts = FALSE)


Method skills()

Usage

Agent$skills()


Method clone()

The objects of this class are cloneable with this method.

Usage

Agent$clone(deep = FALSE)

Arguments

deep

Whether to make a deep clone.

Examples

if (FALSE) { # \dontrun{
# Create an agent with file tools
agent <- Agent$new(
  chat = ellmer::chat("openai/gpt-4o"),
  tools = tools_file()
)

# Run a task with streaming output
for (event in agent$run("List files in the current directory")) {
  if (event$type == "text") cat(event$text)
}

# Or use the blocking convenience method
result <- agent$run_sync("List files")
print(result$response)
} # }

## ------------------------------------------------
## Method `Agent$add_hook`
## ------------------------------------------------

if (FALSE) { # \dontrun{
# Add a hook to block dangerous bash commands
agent$add_hook(hook_block_dangerous_bash())

# Add a custom PreToolUse hook
agent$add_hook(HookMatcher$new(
  event = "PreToolUse",
  pattern = "^write_file$",
  callback = function(tool_name, tool_input, context) {
    cli::cli_alert_info("Writing to: {tool_input$path}")
    HookResultPreToolUse(permission = "allow")
  }
))
} # }