LLM.Context (LLM v0.1.2)

Copy Markdown View Source

Request context carrying the system prompt, messages, tools, and provider state.

A context is built automatically by LLM.generate/2 and LLM.stream/2, but you can construct one directly for multi-turn conversations or advanced use cases.

Fields

  • :system — system prompt string (optional)
  • :messages — list of LLM.Message.t() structs
  • :tools — list of LLM.Tool.t() structs or tool modules
  • :provider_state — provider-specific state (e.g., previous_response_id for OpenAI Responses)

Examples

# From a string
LLM.Context.new("Hello, how are you?")

# From a keyword list
LLM.Context.new(
  system: "You are a helpful assistant",
  messages: ["What is 2+2?"]
)

# From a map
LLM.Context.new(%{
  system: "You are a pirate",
  messages: [user: "Ahoy!"]
})

Summary

Functions

Create a context from a string, list of messages, keyword list, or map.

Types

t()

@type t() :: %LLM.Context{
  messages: [LLM.Message.t()],
  provider_state: map(),
  system: String.t() | nil,
  tools: [LLM.Tool.t() | module()]
}

Functions

new(input)

Create a context from a string, list of messages, keyword list, or map.

String

Creates a context with a single user message:

LLM.Context.new("Hello")
#=> %LLM.Context{messages: [%LLM.Message{role: :user, content: "Hello"}]}

Keyword list

Accepts :system, :messages, :tools, and :provider_state keys:

LLM.Context.new(
  system: "You are helpful",
  messages: ["Hi"],
  tools: [MyTool]
)

Messages can be strings or LLM.Message.t() structs — strings are converted automatically.

Map

Same keys as the keyword list variant:

LLM.Context.new(%{system: "Be concise", messages: ["Hello"]})

List of messages

A plain list is treated as a list of messages:

LLM.Context.new(["Hello", "How are you?"])