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
@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 toChatModel.node/1(one is required):tools- list of%LangEx.Tool{}, or afn state -> [%LangEx.Tool{}] endresolved 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 (seeLangEx.Store):compaction- context compaction options passed toLangEx.ContextCompaction.compact_if_needed/2;falsedisables (default[]— compaction with defaults). This trims only the model's view each turn; the full history stays in state. For persisted compaction useLangEx.Middleware.Summarization(and passcompaction: false).:interrupt_before/:interrupt_after- static breakpoints, forwarded toGraph.compile/2(nodes::agent,:tools):tool_opts- options forLangEx.Tool.Node.node/2(:handle_tool_errors,:max_concurrency,:timeout, ...)- All other options (
:resilient,:temperature,:api_key, ...) are forwarded toChatModel.node/1
@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.