Class AgentBuilder<TOut, TMulti>

AgentBuilder - A fluent interface for creating AI agents with automatic session management

Provides a simple, chainable API for building different types of agents (LLM, Sequential, Parallel, Loop, LangGraph) with tools, custom instructions, and multi-agent workflows. Sessions are automatically created using in-memory storage by default.

// Simple usage
const response = await AgentBuilder.withModel("gemini-2.5-flash").ask("Hello");

// With tools and instructions
const { runner } = await AgentBuilder
.create("research-agent")
.withModel("gemini-2.5-flash")
.withTools(new GoogleSearch())
.withInstruction("You are a research assistant")
.build();

// With plugins
const { runner } = await AgentBuilder
.create("monitored-agent")
.withModel("gemini-2.5-flash")
.withPlugins(new LoggingPlugin(), new MetricsPlugin())
.build();

Type Parameters

  • TOut = string
  • TMulti extends boolean = false

Methods

  • Convenience method to start building with a model directly

    Parameters

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

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

    Returns AgentBuilder<string, false>

    New AgentBuilder instance with model set

  • 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 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

  • 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();
  • Configure fallback models to use when the primary model hits rate limits. Models will be tried in order when the primary model fails with a 429 error.

    Parameters

    • Rest...models: string[]

      Fallback model names in priority order

    Returns this

    This builder instance for chaining

    const response = await AgentBuilder
    .withModel("gpt-4o")
    .withFallbackModels("gpt-3.5-turbo", "gemini-2.0-flash")
    .ask("Hello");
  • 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 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