Represents a single execution run within a session.
A run represents one complete interaction with the AI agent, including input, output, and any intermediate steps (turns).
Run Lifecycle
Runs follow this state machine:
pending -> running -> completed
-> failed
-> cancelled
-> timeoutFields
id- Unique run identifiersession_id- Parent session identifierstatus- Current run statusinput- Input data for the runoutput- Output data from the runerror- Error information if the run failedmetadata- Arbitrary metadataturn_count- Number of turns in this runtoken_usage- Token usage statisticsstarted_at- Run start timestampended_at- Run end timestamp
Usage
# Create a new run
{:ok, run} = Run.new(%{session_id: "session-123"})
# Start the run
{:ok, running} = Run.update_status(run, :running)
# Set output and complete
{:ok, completed} = Run.set_output(running, %{response: "Hello!"})
Summary
Functions
Reconstructs a run from a map.
Increments the turn count by 1.
Creates a new run with the given attributes.
Sets the error and marks the run as failed.
Sets the output and marks the run as completed.
Converts a run to a map suitable for JSON serialization.
Updates the run status.
Updates token usage statistics.
Types
@type status() :: :pending | :running | :completed | :failed | :cancelled | :timeout
@type t() :: %AgentSessionManager.Core.Run{ ended_at: DateTime.t() | nil, error: map() | nil, id: String.t() | nil, input: map() | nil, metadata: map(), output: map() | nil, session_id: String.t() | nil, started_at: DateTime.t() | nil, status: status(), token_usage: map(), turn_count: non_neg_integer() }
Functions
@spec from_map(map()) :: {:ok, t()} | {:error, AgentSessionManager.Core.Error.t()}
Reconstructs a run from a map.
Increments the turn count by 1.
@spec new(map()) :: {:ok, t()} | {:error, AgentSessionManager.Core.Error.t()}
Creates a new run with the given attributes.
Required
:session_id- The parent session identifier
Optional
:id- Custom ID (auto-generated if not provided):input- Input data for the run:metadata- Arbitrary metadata map
Examples
iex> Run.new(%{session_id: "session-123"})
{:ok, %Run{session_id: "session-123", status: :pending, ...}}
iex> Run.new(%{})
{:error, %Error{code: :validation_error, ...}}
Sets the error and marks the run as failed.
Sets the output and marks the run as completed.
Converts a run to a map suitable for JSON serialization.
@spec update_status(t(), status()) :: {:ok, t()} | {:error, AgentSessionManager.Core.Error.t()}
Updates the run status.
Valid statuses
:pending- Run created but not started:running- Run is currently executing:completed- Run completed successfully:failed- Run failed:cancelled- Run was cancelled:timeout- Run timed out
Terminal statuses (:completed, :failed, :cancelled, :timeout) will
automatically set the ended_at timestamp.
Examples
iex> {:ok, run} = Run.new(%{session_id: "session"})
iex> Run.update_status(run, :running)
{:ok, %Run{status: :running, ended_at: nil, ...}}
iex> Run.update_status(run, :completed)
{:ok, %Run{status: :completed, ended_at: %DateTime{}, ...}}
Updates token usage statistics.
Token usage is accumulated across multiple updates.