PrewarmingLiveModel

class PrewarmingLiveModel(model: Model, spares: Int = DEFAULT_SPARES, maxSetups: Int = DEFAULT_MAX_SETUPS, keepWarmFor: Duration? = DEFAULT_KEEP_WARM_FOR, minLifetimeForReplacement: Duration? = DEFAULT_MIN_LIFETIME_FOR_REPLACEMENT, maxSpareAge: Duration? = DEFAULT_MAX_SPARE_AGE, updateCheckWindow: Duration? = null, context: CoroutineContext = Dispatchers.Default) : Model, AutoCloseable

A Model that opens live connections to model ahead of time so connect can take an already-open connection (a spare) instead of waiting for a new session. Spares open without a system instruction or tools and receive them during connect via LiveConnection.sendContextUpdate, so prewarming works only with models whose live connections accept context updates, such as Gemini 3.x Live models. On other models, such as Gemini 2.5 Live models, a connect call that takes a spare fails when the server ends the session with an error (1007) after the update, unless updateCheckWindow is set, in which case the pool detects the refusal on Gemini spares, keeps no spares, and falls back to opening a new connection for every connect call.

Parameters

model

The model whose live connections this pool keeps open.

spares

Number of open connections to keep per setup (default 1); must be positive. Each connect takes a spare and starts opening a replacement, so with spares = 2, two calls that start within the few seconds it takes to open a replacement both get an open connection, while a third waits for a new connection to open.

maxSetups

Maximum number of setups that keep spares (default 8); must be positive. A setup covers everything a request uses to connect except its history, system instruction, and tools (such as the model, voice, and response modalities), so agents that differ only in instruction and tools share a setup. With maxSetups = 2, connecting with setups A, B, and then C closes the spares for A, the least recently requested setup.

keepWarmFor

How long after a setup's last connect or prewarm the pool keeps replacing spares that reach maxSpareAge or are ended by the server (default 30 minutes); must be positive, or null to never replace them. After that window, those spares are closed without replacement so an unused setup stops holding connections until its next connect or prewarm. With keepWarmFor = 10.minutes, a setup last used at 12:00 gets a replacement for a spare the server ends at 12:05, but not for one ended at 12:15.

minLifetimeForReplacement

Minimum time a spare must stay open for the pool to immediately replace it when the server ends it or it reaches maxSpareAge (default 1 minute); must be positive, or null to immediately replace every such spare while keepWarmFor allows, even one the server ends right after opening. A spare that ends sooner is not replaced right away, preventing a server that ends every new session or a short maxSpareAge from keeping the pool reconnecting; its setup refills on the next connect or prewarm. With the default, a spare ended by the server after 5 seconds is not replaced right away, while one ended after 2 minutes is replaced immediately if keepWarmFor allows.

maxSpareAge

How long a spare stays available after opening (default 5 minutes); must be positive, or null for no limit. An older spare is closed and, while keepWarmFor allows, replaced so connect never takes a connection near the server's time limit (about 10 minutes on Gemini, with a go-away up to 1 minute before). With the default, a spare is replaced every 5 minutes while keepWarmFor allows, so connect gets a connection at most 5 minutes old.

updateCheckWindow

How long the pool waits for the server to end a Gemini spare's session after sending a test context update (a check the pool runs until one spare passes), or null (the default) to skip the check for models known to accept context updates. Set it, for example to 1 second, when the model may not accept context updates, such as a Gemini 2.5 Live model, where null makes calls that take a spare fail; once the server ends a checked spare's session with an error (1007), the pool keeps no spares, while a spare that ends otherwise is dropped and the next one is checked. When set, it must be finite, positive, and longer than the server takes to end a refused session, and a connect that takes a spare before one passes can wait up to this long.

context

Coroutine context where the pool opens and watches spares (defaults to Dispatchers.Default), since spares outlive the connect call that requested them. After connect takes a spare, work the wrapped model tied to opening it (such as a Gemini spare's receive pump) keeps running in this context rather than the caller's. Any Job in this context is ignored, so only close ends the pool's work; because close blocks until that work finishes, this context must not be confined to the thread calling close.

Constructors

Link copied to clipboard
constructor(model: Model, spares: Int = DEFAULT_SPARES, maxSetups: Int = DEFAULT_MAX_SETUPS, keepWarmFor: Duration? = DEFAULT_KEEP_WARM_FOR, minLifetimeForReplacement: Duration? = DEFAULT_MIN_LIFETIME_FOR_REPLACEMENT, maxSpareAge: Duration? = DEFAULT_MAX_SPARE_AGE, updateCheckWindow: Duration? = null, context: CoroutineContext = Dispatchers.Default)

Types

Link copied to clipboard

Fluent builder for PrewarmingLiveModel, provided primarily for Java callers. Any property left unset falls back to the same default as the constructor.

Link copied to clipboard
object Companion

Properties

Link copied to clipboard
open override val capabilities: LlmCapabilities

The optional features this model supports, which change how ADK builds its requests. By default only a model whose name is a gemini-2.* name lacks LlmCapabilities.outputSchemaAndTools. Override this to choose, for example LlmCapabilities(outputSchemaAndTools = false) to get the final answer through set_model_response beside tools.

Link copied to clipboard
open override val name: String

The name of the model.

Functions

Link copied to clipboard
open override fun close()

Closes every spare and waits for spares already on their way out, blocking the calling thread until they are closed; a later connect opens its connection directly.

Link copied to clipboard
open suspend override fun connect(request: LlmRequest): LiveConnection

Opens a live (bidirectional streaming) connection, configured by LlmRequest.liveConnectConfig.

Link copied to clipboard
open override fun generateContent(request: LlmRequest, stream: Boolean = false): Flow<LlmResponse>

Generates content for the given LlmRequest. This returns a Flow of LlmResponses.

Link copied to clipboard
fun prewarm(liveRunConfig: LiveRunConfig, generateContentConfig: GenerateContentConfig? = null)

Starts opening spares for live agents using liveRunConfig and generateContentConfig, so even their first connect can take one. An agent whose request config differs (for example, when its output schema is applied) still opens its first connection directly.