LlmCore.Agent.Context (llm_core v0.9.4)

Copy Markdown View Source

Carries data through the agentic iteration pipeline.

One Context is created per LLM response and flows through all stages of the LlmCore.Agent.Pipeline.Iteration pipeline. The outer loop (LlmCore.Agent.Loop) creates this from the LLM response, sends it through the pipeline, and reads the decision field to determine the next action.

Fields

Input — set by the outer loop before pipeline entry

  • messages — Current message list (grows each iteration)
  • tools — Available tool definitions ([LlmToolkit.Tool.t()])
  • response — The LLM response for this iteration
  • resolve_tool — fn(%Call{}) -> {:ok, String.t()} | {:error, String.t()}

  • tool_timeout — maximum time in milliseconds for one tool dispatch, including all recipe sub-calls. When the caller passes none there is no deadline (:infinity).
  • resolver_module — Optional module implementing ToolResolver behaviour. When set, DispatchTools checks for dispatch recipes via resolver_module.dispatch_recipe/1.
  • terminal_tool — Optional tool name that terminates the loop when called.
  • terminal_required — When true (with terminal_tool), a no-tool-call response with prose is a {:nonterminal_stop, response} decision rather than completion; the outer loop owns the bounded recovery.
  • iteration — Current iteration count (0-based)
  • max_iterations — Hard iteration limit

Intermediate — populated by pipeline stages

  • tool_calls — Parsed tool call requests from the response
  • terminal_tool_call — Matching terminal tool call when configured
  • terminal_args — Raw arguments from the terminal tool call
  • tool_results — Results after tool execution
  • result_messages — Formatted messages for the next LLM turn
  • validation_errors — Errors from call validation

Output — read by the outer loop after pipeline exit

  • decision — {:continue, messages}, {:done, response}, {:blank_stop, response}, {:nonterminal_stop, response}, or {:error, reason}
  • status — :ok or :error (short-circuit flag for stages)
  • error — Error detail when status == :error
  • trace — Accumulated trace entries for observability

{:blank_stop, response} marks a no-tool-call response whose content is blank (nil/whitespace) — a degenerate provider completion that must not be accepted as final. The outer loop owns the separately bounded recovery policy for this decision.

{:nonterminal_stop, response} marks a no-tool-call response with prose while terminal_required is set — the model narrated instead of finishing through the terminal tool. Like a blank stop, it is never accepted as final; the outer loop owns its separately bounded recovery.

Summary

Types

decision()

@type decision() ::
  {:continue, [map()]}
  | {:done, LlmCore.LLM.Response.t()}
  | {:blank_stop, LlmCore.LLM.Response.t()}
  | {:nonterminal_stop, LlmCore.LLM.Response.t()}
  | {:error, term()}
  | nil

t()

@type t() :: %LlmCore.Agent.Context{
  decision: decision(),
  error: term() | nil,
  iteration: non_neg_integer(),
  max_iterations: pos_integer(),
  messages: [map()],
  resolve_tool:
    (LlmToolkit.Tool.Call.t() -> {:ok, String.t()} | {:error, String.t()}) | nil,
  resolver_module: module() | nil,
  response: LlmCore.LLM.Response.t() | nil,
  result_messages: [map()],
  status: :ok | :error,
  terminal_args: map() | nil,
  terminal_required: boolean(),
  terminal_tool: String.t() | nil,
  terminal_tool_call: LlmToolkit.Tool.Call.t() | nil,
  tool_calls: [LlmToolkit.Tool.Call.t()],
  tool_results: [LlmToolkit.Tool.Result.t()],
  tool_timeout: pos_integer() | :infinity,
  tools: [LlmToolkit.Tool.t()],
  trace: [term()],
  validation_errors: [term()]
}