LangEx.Prebuilt (LangEx v0.11.3)

Copy Markdown View Source

Ready-made graph constructors for common agent shapes.

agent/1 builds the canonical tool-calling loop — an LLM node, a tool-execution node, and the routing between them — with usage accounting and context compaction wired in:

graph =
  LangEx.Prebuilt.agent(
    model: "claude-opus-4-20250514",
    system_prompt: "You are a helpful DevOps assistant.",
    tools: [health_tool, logs_tool],
    checkpointer: LangEx.Checkpointer.Postgres
  )

{:ok, result} =
  LangEx.invoke(graph, %{messages: [Message.human("Is api-gateway healthy?")]},
    config: [thread_id: "ops-1", repo: MyApp.Repo]
  )

Middleware

Pass :middleware (a list of %LangEx.Middleware{}) to layer extra behaviour around the model call — summarisation, context editing, planning, tool pre-selection, completion gating — without changing the agent's shape. Built-in middleware lives under LangEx.Middleware.*:

LangEx.Prebuilt.agent(
  model: "claude-opus-4-20250514",
  tools: tools,
  middleware: [
    LangEx.Middleware.Summarization.new(model: "claude-haiku-4-5-20251001"),
    LangEx.Middleware.TodoList.new(),
    LangEx.Middleware.Rubric.new(rubric: "Cites logs and names a root cause.")
  ]
)

Summary

Functions

Builds and compiles a tool-calling agent graph.

Builds a generate → critique → revise reflection graph.

Functions

agent(opts)

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

Builds and compiles a tool-calling agent graph.

The graph state has :messages (with Message.add_messages/2) and :llm_usage (accumulating token usage via ChatModel.merge_usage/2), plus any keys contributed by :middleware.

Options

  • :model / :provider - forwarded to ChatModel.node/1 (one is required)
  • :tools - list of %LangEx.Tool{}, or a fn state -> [%LangEx.Tool{}] end resolved from state each turn (for tools discovered at runtime and kept out of checkpointed state). Default []; without tools the graph is a single LLM turn
  • :middleware - list of %LangEx.Middleware{} wrapping the model call (default []); their tools and state schema are merged in automatically
  • :system_prompt - prepended as a system message when the conversation does not already start with one
  • :name - graph name for telemetry (default :agent)
  • :checkpointer - enables persistence, interrupts, and resume
  • :store - long-term memory backend (see LangEx.Store)
  • :compaction - context compaction options passed to LangEx.ContextCompaction.compact_if_needed/2; false disables (default [] — compaction with defaults). This trims only the model's view each turn; the full history stays in state. For persisted compaction use LangEx.Middleware.Summarization (and pass compaction: false).
  • :interrupt_before / :interrupt_after - static breakpoints, forwarded to Graph.compile/2 (nodes: :agent, :tools)
  • :tool_opts - options for LangEx.Tool.Node.node/2 (:handle_tool_errors, :max_concurrency, :timeout, ...)
  • All other options (:resilient, :temperature, :api_key, ...) are forwarded to ChatModel.node/1

reflect(opts)

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

Builds a generate → critique → revise reflection graph.

Delegates to LangEx.Prebuilt.Reflect.create/1; see that module for options and state shape.