Skip to contents

An AgentResult describes one finished run. Agent$run_sync(), $run_async() and $last_run() return one; you rarely need to create it yourself. It holds the final response, the conversation turns, every AgentEvent, usage and cost, and IDs that link the run to its agent and session.

Results are read-only. Read fields with $, for example result$response, or use result_n_turns(), result_tool_calls(), result_tool_results(), result_text_chunks() and result_is_success().

ending says how the run ended in terms of its own classes, so a host need not parse stop_reason; see RunEnding.

finish_reason is the provider's finish reason for the run's final response, such as "success" or "max_tokens", or NULL if there was none. cut_off is TRUE when the provider cut that response short (the output token limit, the context window or a content filter). Without another stop reason, the result has a RunEndingCutOff ending, its stop_reason is "complete", and result_is_success() is TRUE. A truncated wrap-up answer keeps its RunEndingLimit ending and also has cut_off = TRUE. Check cut_off whenever you need a whole answer.

gathered is set when the run ended without an answer: it was stopped, hit a limit with no wrap-up, or its wrap-up answer was cut off. It is what the run had gathered, unsummarised, as one text for whoever asked for the work: a line saying it was stopped before it wrote its answer, what it wrote as it worked (and any wrap-up answer cut off part way), then the results of its tool calls that finished, the latest first, each labelled with its tool and arguments, and the calls that returned nothing. Results a ContextPolicy kept outside the conversation are given as their text, not as references. It is at most four times the policy's max_tool_result_bytes characters (at least 64 KiB), the latest results first. A lead's delegation outcome gives it as the answer (see DelegationOutcome); a host whose own tool runs a subagent with $continue_agent_async() should hand it to its model when response is not an answer. NULL when the run answered, is waiting for an approval, or gathered nothing.

usage covers this run only, but cost covers the whole conversation, including earlier runs and turns that compaction removed from the model context.

Usage

AgentResult(
  response = NULL,
  turns = list(),
  cost = list(input = 0, output = 0, cached = 0, total = 0, complete = TRUE, missing =
    0L),
  events = list(),
  duration = NULL,
  stop_reason = "complete",
  structured_output = NULL,
  session_id = NULL,
  run_id = NULL,
  usage = AgentUsage(),
  agent_id = NULL,
  agent_name = NULL,
  parent_agent_id = NULL,
  parent_run_id = NULL,
  delegation_id = NULL,
  run_context = list(),
  ending = NULL,
  finish_reason = NULL,
  gathered = NULL
)

Arguments

response

Final text response, or NULL.

turns

List of ellmer turns in the model context when the run ended.

cost

Cost summary (as from Agent$cost()), or NULL. total is NA if some responses had no cost.

events

List of AgentEvent objects.

duration

Run time in seconds, or NULL.

stop_reason

Why the run stopped: "complete" if the model finished, otherwise a reason such as "request_limit", "interrupted" or "error".

structured_output

Extracted structured data, if any.

session_id, run_id, agent_id, agent_name, parent_agent_id, parent_run_id, delegation_id

Optional ID strings linking the run to its session and agent and, for a subagent, to the parent run and delegation.

usage

AgentUsage for this run only, or NULL.

run_context

The run's run_context, a named list.

ending

How the run ended, as a RunEnding object, or NULL to derive it from stop_reason and finish_reason. The five classes are RunEndingAnswered(), RunEndingStopped(), RunEndingLimit(), RunEndingCutOff() and RunEndingFailed().

finish_reason

The provider's finish reason for the final response, or NULL. With no ending, a stop_reason of "complete" and a cut-off finish_reason give a RunEndingCutOff ending.

gathered

What a run that ended without an answer had gathered, as one string, or NULL.

Value

An AgentResult object.

Examples

result <- AgentResult(response = "Done", events = list(
  AgentEvent("text", text = "Done")
))
result_is_success(result)
#> [1] TRUE
result_text_chunks(result)
#> [1] "Done"