Interface AgentBuilderWithSchema<T, M>

AgentBuilder with typed output schema

interface AgentBuilderWithSchema<T, M> {
    build(): Promise<BuiltAgent<T, M>>;
    buildWithSchema<U>(): Promise<BuiltAgent<U, M>>;
    ask(message: string | FullMessage): Promise<RunnerAskReturn<T, M>>;
    withModel(model:
        | string
        | BaseLlm
        | string & {}
        | LanguageModelV2): this;
    withDescription(description: string): this;
    withInstruction(instruction: string): this;
    withInputSchema(schema: ZodType<unknown, unknown, $ZodTypeInternals<unknown, unknown>>): this;
    withTools(...tools: BaseTool[]): this;
    withPlanner(planner: BasePlanner): this;
    withCodeExecutor(codeExecutor: BaseCodeExecutor): this;
    withOutputKey(outputKey: string): this;
    withSubAgents(subAgents: BaseAgent[]): this;
    withBeforeAgentCallback(callback: BeforeAgentCallback): this;
    withAfterAgentCallback(callback: AfterAgentCallback): this;
    withBeforeModelCallback(callback: BeforeModelCallback): this;
    withAfterModelCallback(callback: AfterModelCallback): this;
    withBeforeToolCallback(callback: BeforeToolCallback): this;
    withAfterToolCallback(callback: AfterToolCallback): this;
    withPlugins(...plugins: BasePlugin[]): this;
    withAgent(agent: BaseAgent): this;
    asSequential(subAgents: BaseAgent[]): AgentBuilder<any, true>;
    asParallel(subAgents: BaseAgent[]): AgentBuilder<any, true>;
    asLoop(subAgents: BaseAgent[], maxIterations?: number): this;
    asLangGraph(nodes: LangGraphNode[], rootNode: string): this;
    withSessionService(service: BaseSessionService, options?: SessionOptions): this;
    withSession(session: Session): this;
    withMemory(memoryService: BaseMemoryService): this;
    withArtifactService(artifactService: BaseArtifactService): this;
    withRunConfig(config: RunConfig | Partial<RunConfig>): this;
    withEventsCompaction(config: EventsCompactionConfig): this;
    withQuickSession(options?: SessionOptions): this;
}

Type Parameters

  • T
  • M extends boolean = false

Hierarchy

  • Omit<AgentBuilder<any, any>, "build" | "ask" | "withOutputSchema">
    • AgentBuilderWithSchema

Methods

  • Set the model for the agent

    Parameters

    • model:
          | string
          | BaseLlm
          | string & {}
          | LanguageModelV2

      The model identifier (e.g., "gemini-2.5-flash")

    Returns this

    This builder instance for chaining

  • Set the description for the agent

    Parameters

    • description: string

      Agent description

    Returns this

    This builder instance for chaining

  • Set the instruction for the agent

    Parameters

    • instruction: string

      System instruction for the agent

    Returns this

    This builder instance for chaining

  • Set the output key for the agent

    Parameters

    • outputKey: string

      The output key in session state to store the output of the agent

    Returns this

    This builder instance for chaining

  • Set the before model callback for LLM interaction

    Parameters

    Returns this

    This builder instance for chaining

  • Set the after model callback for LLM interaction

    Parameters

    Returns this

    This builder instance for chaining

  • Add plugins to the agent for lifecycle hooks and behavior extensions

    Parameters

    • Rest...plugins: BasePlugin[]

      Plugin instances to add to the agent

    Returns this

    This builder instance for chaining

    const { runner } = await AgentBuilder
    .create("monitored-agent")
    .withModel("gemini-2.5-flash")
    .withPlugins(
    new LoggingPlugin(),
    new MetricsPlugin(),
    new RateLimitPlugin()
    )
    .build();
  • Provide an already constructed agent instance. Further definition-mutating calls (model/tools/instruction/etc.) will be ignored with a dev warning.

    Parameters

    Returns this

  • Configure as a loop agent

    Parameters

    • subAgents: BaseAgent[]

      Sub-agents to execute iteratively

    • maxIterations: number = 3

      Maximum number of iterations

    Returns this

    This builder instance for chaining

  • Configure as a LangGraph agent

    Parameters

    • nodes: LangGraphNode[]

      Graph nodes defining the workflow

    • rootNode: string

      The starting node name

    Returns this

    This builder instance for chaining

  • Configure session management with optional smart defaults

    Parameters

    Returns this

    This builder instance for chaining

  • Configure with an existing session instance

    Parameters

    • session: Session

      Existing session to use

    Returns this

    This builder instance for chaining

    Error if no session service has been configured via withSessionService()

  • Configure artifact service for the agent

    Parameters

    • artifactService: BaseArtifactService

      Artifact service to use for managing generated artifacts

    Returns this

    This builder instance for chaining

  • Configure event compaction for automatic history management

    Parameters

    Returns this

    This builder instance for chaining

    const { runner } = await AgentBuilder
    .create("assistant")
    .withModel("gemini-2.5-flash")
    .withEventsCompaction({
    compactionInterval: 10, // Compact every 10 invocations
    overlapSize: 2, // Include 2 prior invocations
    })
    .build();
  • Configure with an in-memory session with custom IDs Note: In-memory sessions are created automatically by default, use this only if you need custom appName/userId

    Parameters

    • options: SessionOptions = {}

      Session configuration options (userId and appName)

    Returns this

    This builder instance for chaining