LangEx.Middleware.Subagent (LangEx v0.12.0)

Copy Markdown View Source

Middleware that lets the agent delegate work to ephemeral subagents.

Contributes a task tool through which the main agent spawns a context-isolated child agent: the child sees nothing of the parent conversation, works through its own tool-calling loop, and hands back only its final report as the tool reply. This keeps deep-dive work (log mining, research, drafting) out of the parent's context window.

Children are ephemeral: each call compiles a fresh child graph with LangEx.Prebuilt.agent/1 — no checkpointer, no middleware (so a child cannot spawn subagents of its own) — and the child's conversation is discarded when it finishes. Only the report survives, in the tool reply; the child's token usage is folded into the parent's :llm_usage.

Options

  • :subagents (required) - list of subagent spec maps:
    • :name (required) - the subagent_type value the model passes
    • :description (required) - shown to the model in the tool description
    • :system_prompt (required) - the child agent's system prompt
    • :tools - tools for the child, a list or fn state -> tools end (default [])
    • :inherit_keys - parent-state keys copied into the child's input state (default []) — lets state-derived child tools (sessions, serialized tool specs, time windows) materialize inside the child without exposing the parent conversation
    • :provider / :model - override the middleware-level defaults
    • :recursion_limit - max child super-steps (default 24)
  • :provider / :model - defaults for children without their own

Example

LangEx.Prebuilt.agent(
  model: "claude-sonnet-5",
  middleware: [
    LangEx.Middleware.Subagent.new(
      subagents: [
        %{
          name: "log-miner",
          description: "Deep-dives logs to find root causes.",
          system_prompt: "You are a log analysis expert...",
          tools: [logs_tool]
        }
      ],
      provider: LangEx.LLM.Anthropic,
      model: "claude-sonnet-5"
    )
  ]
)

Summary

Functions

Builds a subagent-spawning middleware. See the module doc for options.

Functions

new(opts)

@spec new(keyword()) :: LangEx.Middleware.t()

Builds a subagent-spawning middleware. See the module doc for options.