LangEx.Prebuilt.Reflect (LangEx v0.11.3)

Copy Markdown View Source

A generate → critique → revise loop (the reflection / reflexion pattern).

create/1 compiles a graph that drafts an answer, has a critic evaluate it against :reflect_prompt, and loops back to revise while the critic withholds approval — up to :max_iterations. The critic's verdict is obtained with LangEx.LLM.ChatModel.structured/2, so approval is a validated boolean rather than parsed prose.

graph =
  LangEx.Prebuilt.Reflect.create(
    model: "claude-opus-4-20250514",
    generate_prompt: "Draft a runbook step for the incident.",
    reflect_prompt: "Critique the draft. Approve only if it is correct and complete.",
    max_iterations: 3
  )

{:ok, %{messages: messages}} =
  LangEx.invoke(graph, %{messages: [LangEx.Message.human("DB CPU at 100%")]})

Graph state adds :reflect_iteration, :reflect_approved, and :reflect_feedback to the standard :messages / :llm_usage channels.

Summary

Functions

Builds and compiles a reflection graph.

Functions

create(opts)

@spec create(keyword()) :: LangEx.Graph.Compiled.t()

Builds and compiles a reflection graph.

Options

  • :model / :provider - forwarded to the LLM (one is required)
  • :generate_prompt - system prompt for the drafting step
  • :reflect_prompt - system prompt for the critic (has a sensible default)
  • :max_iterations - hard cap on revise rounds (default 3)
  • :name - graph name for telemetry (default :reflect)
  • :checkpointer - enables persistence and resume
  • :compaction - context compaction options; false disables
  • All other options (:resilient, :temperature, :api_key, ...) are forwarded to the underlying LLM calls