1919import static com .google .common .base .StandardSystemProperty .JAVA_VERSION ;
2020
2121import com .google .adk .Version ;
22+ import com .google .adk .internal .http .HttpClientFactory ;
2223import com .google .common .collect .ImmutableList ;
2324import com .google .common .collect .ImmutableMap ;
2425import com .google .errorprone .annotations .CanIgnoreReturnValue ;
2526import com .google .genai .Client ;
2627import com .google .genai .ResponseStream ;
2728import com .google .genai .types .Candidate ;
29+ import com .google .genai .types .ClientOptions ;
2830import com .google .genai .types .Content ;
2931import com .google .genai .types .FinishReason ;
3032import com .google .genai .types .FunctionCall ;
4244import java .util .Objects ;
4345import java .util .UUID ;
4446import java .util .concurrent .CompletableFuture ;
47+ import okhttp3 .OkHttpClient ;
4548import org .jspecify .annotations .Nullable ;
4649import org .slf4j .Logger ;
4750import org .slf4j .LoggerFactory ;
@@ -56,6 +59,8 @@ public class Gemini extends BaseLlm {
5659
5760 private static final Logger logger = LoggerFactory .getLogger (Gemini .class );
5861 private static final ImmutableMap <String , String > TRACKING_HEADERS ;
62+ private static final OkHttpClient SHARED_HTTP_CLIENT =
63+ HttpClientFactory .createSharedHttpClient ("GeminiApiClient" );
5964
6065 static {
6166 String frameworkLabel = "google-adk/" + Version .JAVA_ADK_VERSION ;
@@ -94,6 +99,7 @@ public Gemini(String modelName, String apiKey) {
9499 Client .builder ()
95100 .apiKey (apiKey )
96101 .httpOptions (HttpOptions .builder ().headers (TRACKING_HEADERS ).build ())
102+ .clientOptions (ClientOptions .builder ().customHttpClient (SHARED_HTTP_CLIENT ).build ())
97103 .build ();
98104 }
99105
@@ -107,7 +113,9 @@ public Gemini(String modelName, VertexCredentials vertexCredentials) {
107113 super (modelName );
108114 Objects .requireNonNull (vertexCredentials , "vertexCredentials cannot be null" );
109115 Client .Builder apiClientBuilder =
110- Client .builder ().httpOptions (HttpOptions .builder ().headers (TRACKING_HEADERS ).build ());
116+ Client .builder ()
117+ .httpOptions (HttpOptions .builder ().headers (TRACKING_HEADERS ).build ())
118+ .clientOptions (ClientOptions .builder ().customHttpClient (SHARED_HTTP_CLIENT ).build ());
111119 vertexCredentials .project ().ifPresent (apiClientBuilder ::project );
112120 vertexCredentials .location ().ifPresent (apiClientBuilder ::location );
113121 vertexCredentials .credentials ().ifPresent (apiClientBuilder ::credentials );
@@ -206,6 +214,7 @@ public Gemini build() {
206214 modelName ,
207215 Client .builder ()
208216 .httpOptions (HttpOptions .builder ().headers (TRACKING_HEADERS ).build ())
217+ .clientOptions (ClientOptions .builder ().customHttpClient (SHARED_HTTP_CLIENT ).build ())
209218 .build ());
210219 }
211220 }
0 commit comments