LangEx.Prebuilt (LangEx v0.7.0)

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]
  )

Summary

Functions

Builds and compiles a tool-calling agent 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).

Options

  • :model / :provider - forwarded to ChatModel.node/1 (one is required)
  • :tools - list of %LangEx.Tool{} (default []; without tools the graph is a single LLM turn)
  • :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)
  • :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