ClaudeWrapper.Result (ClaudeWrapper v0.10.0)

Copy Markdown View Source

Result from a completed query execution.

Maps to the Rust QueryResult -- the parsed JSON output from --output-format json.

Summary

Types

t()

Normalized token usage from usage/1. total is throughput (input + output + cache_creation) and excludes cache reads.

Functions

Parse a result from the JSON output of a query command.

The turn's stop reason (e.g. "end_turn", "max_tokens"), read from extra["stop_reason"]. Returns nil when absent.

The schema-validated structured output, read from extra["structured_output"].

Token usage for the turn, read from extra["usage"].

Types

t()

@type t() :: %ClaudeWrapper.Result{
  cost_usd: float() | nil,
  duration_ms: non_neg_integer() | nil,
  extra: map(),
  is_error: boolean(),
  num_turns: non_neg_integer() | nil,
  result: String.t(),
  session_id: String.t() | nil
}

usage()

@type usage() :: %{
  input: non_neg_integer(),
  output: non_neg_integer(),
  cache_creation: non_neg_integer(),
  cache_read: non_neg_integer(),
  total: non_neg_integer()
}

Normalized token usage from usage/1. total is throughput (input + output + cache_creation) and excludes cache reads.

Functions

from_json(data)

@spec from_json(map()) :: t()

Parse a result from the JSON output of a query command.

stop_reason(result)

@spec stop_reason(t()) :: String.t() | nil

The turn's stop reason (e.g. "end_turn", "max_tokens"), read from extra["stop_reason"]. Returns nil when absent.

structured_output(result)

@spec structured_output(t()) :: map() | list() | nil

The schema-validated structured output, read from extra["structured_output"].

Present (a JSON object or array) when the turn ran with a JSON schema (claude's --json-schema); nil otherwise.

usage(result)

@spec usage(t()) :: usage() | nil

Token usage for the turn, read from extra["usage"].

Returns nil when the result carries no usage (e.g. some error results). total is input + output + cache_creation -- it excludes cache reads, which re-count context already tallied when it was first cached (consistent with ClaudeWrapper.History.SessionSummary's total_tokens).

iex> ClaudeWrapper.Result.usage(%ClaudeWrapper.Result{result: "", extra: %{}})
nil