ExAgent.RunContext (ExAgent v3.0.0)

View Source

The context object threaded through dynamic instructions, tool calls and output validators during a single run.

Stability — Stable.

It carries the user-supplied deps, the model, the full message history, the accumulated token usage, per-tool retry counters and the current step/position info. The :deps value is application-defined; document its expected shape with @spec in your tools.

execution_scope is an opaque, ephemeral handle for ExAgent.run_child/4. Pass the tool's context to that function so the child shares its ancestors' limits and accounting. Run/parent/root identities and model_request_id correlate work without treating model request counts as local loop steps. Never serialize the handle. Within a tool call call_ref identifies the logical call across attempts. The idempotency_key is available from the first dispatch and equals that reference by default. An authorized retry can carry a historical key. Pass the key to services that accept it and use it for reconciliation. agent_name is the running agent's :name (nil when unnamed). deadline is the run's absolute deadline in monotonic milliseconds (System.monotonic_time(:millisecond)), or nil without one. observability and trace_context are likewise ephemeral; framework delegation explicitly carries them across task boundaries and restores prior context. events is a private handle of the run's event sink used by emit/4 and ExAgent.run_child/4; never read or serialize it.

Summary

Types

Ephemeral handle used by the runtime when a tool starts a child run.

t()

Functions

Emits application data from a tool call to the run's stream.

Return the cassette inherited by this run, or nil when none is attached.

Build a fresh context for a run.

Types

execution_scope()

@opaque execution_scope()

Ephemeral handle used by the runtime when a tool starts a child run.

t()

@type t() :: %ExAgent.RunContext{
  agent_name: String.t() | nil,
  call_ref: String.t() | nil,
  continuation: term(),
  deadline: integer() | nil,
  deps: term(),
  events: map() | nil,
  execution_scope: execution_scope() | nil,
  idempotency_key: String.t() | nil,
  max_retries: non_neg_integer(),
  messages: [ExAgent.Message.t()],
  metadata: map(),
  model: ExAgent.Model.model(),
  model_request_id: String.t() | nil,
  observability: ExAgent.Observability.OpenTelemetry.t() | nil,
  parent_run_id: String.t() | nil,
  prompt: String.t() | nil,
  restored_working_data: term(),
  retries: %{required(String.t()) => non_neg_integer()},
  retry: non_neg_integer(),
  root_run_id: String.t() | nil,
  run_id: String.t() | nil,
  run_step: non_neg_integer(),
  tool_call_id: String.t() | nil,
  tool_name: String.t() | nil,
  trace_context: term(),
  usage: ExAgent.Message.Usage.t()
}

Functions

emit(ctx, name, data, opts \\ [])

@spec emit(t(), String.t(), map(), keyword()) ::
  :ok | {:error, :invalid_tool_data | :tool_data_limit | :not_in_tool_call}

Emits application data from a tool call to the run's stream.

Call it from a tool (or a before_tool_call/3 hook) with the tool's context. The element {:tool_data, %{step, call_ref, tool, part_id, name, data, transient}} reaches ExAgent.run_stream/3 and, as an ExAgent.Event, a Server's subscribers. name is a label of 1 to 128 bytes of UTF-8 and data a JSON object (atom keys become strings) of at most 64 KiB encoded. Options:

  • :id — the part_id (1 to 128 bytes of UTF-8) by which a UI reconciles successive emissions of the same part. Without it each emission gets a fresh id.
  • :transient — true to keep the element out of the Server's event buffer (ExAgent.Server.events_since/2). Default false.

Delivery is synchronous: a slow consumer spends the tool's timeout. Data never enters the history, results or persisted records and never changes the call's outcome. Without a stream it returns :ok. It returns {:error, :invalid_tool_data} for an invalid name, data or option, {:error, :tool_data_limit} after 256 emissions in one call and {:error, :not_in_tool_call} outside a tool call.

fixture(run_context)

@spec fixture(t()) :: ExAgent.Fixture.t() | nil

Return the cassette inherited by this run, or nil when none is attached.

In a tool, pass a view from ExAgent.Fixture.with_label/2 as the :fixture option to ExAgent.run_child/4 to distinguish sibling runs. The host that opened the cassette owns its lifetime.

new(opts \\ [])

@spec new(keyword()) :: t()

Build a fresh context for a run.