LLM.Response (LLM v0.2.0)

Copy Markdown View Source

Normalized response returned by LLM.generate/2 and LLM.Stream.collect/2.

Fields

  • :message — the model's final response turn as an LLM.Message.t()
  • :messages — the messages produced in this session: the user prompt that started the loop plus every assistant turn and tool-result message generated during the auto-tool loop. Does not include prior conversation passed in via the :messages option — append these to that prior list to continue the conversation
  • :usage — accumulated token usage across all turns as an LLM.Usage.t(). Each assistant message in :messages also carries its per-turn usage.
  • :stop_reason — why generation stopped (:stop, :length, :tool_calls, etc.)
  • :provider_state — provider-specific state for multi-turn conversations (e.g., OpenAI Responses previous_response_id)
  • :raw — the original provider response body for debugging
  • :parsed — decoded structured output map when schema: was used, nil otherwise. Also set on the last assistant message's :parsed field.

Examples

{:ok, response} = LLM.generate("Hello", provider: :openai, model: "gpt-4")

response.message.content
#=> "Hi there!"

response.usage
#=> %LLM.Usage{input_tokens: 10, output_tokens: 8, total_tokens: 18}

response.stop_reason
#=> :stop

Summary

Types

t()

@type t() :: %LLM.Response{
  message: LLM.Message.t(),
  messages: [LLM.Message.t()] | nil,
  parsed: map() | nil,
  provider_state: map() | nil,
  raw: map() | nil,
  stop_reason: atom() | nil,
  usage: LLM.Usage.t()
}