lloyal-agents API Reference
    Preparing search index...

    Function withSharedRoot

    • Scoped shared root branch with guaranteed cleanup

      Creates a root branch, prefills the system prompt, and passes it to the body function. The root is pruned via try/finally when the body returns or throws, regardless of whether children still exist.

      Use this for the cold-path pattern where multiple agents share a tokenized system prompt prefix. The sharedPrefixLength passed to the body enables KV savings calculation.

      Type Parameters

      • T

      Parameters

      • opts: SharedRootOptions

        System prompt, tools, and sampling parameters

      • body: (root: Branch, sharedPrefixLength: number) => Operation<T>

        Operation that receives the root branch and prefix length. Typically calls runAgents or useAgentPool inside.

      Returns Operation<T>

      The body's return value

      const { result, prefixLen } = yield* withSharedRoot(
      { systemPrompt: RESEARCH_PROMPT, tools: toolsJson },
      function*(root, prefixLen) {
      const result = yield* runAgents({
      tasks: questions.map(q => ({
      systemPrompt: RESEARCH_PROMPT,
      content: q,
      tools: toolsJson,
      parent: root,
      })),
      tools: toolMap,
      });
      return { result, prefixLen };
      },
      );