# Agent.Builder


```
public final class Agent.Builder
```

<br />

*** ** * ** ***

Builder for constructing `https://developers.google.com/android/reference/com/google/mlkit/genai/agent/Agent` instances.

## Summary

| ### Public constructors |
|---|
| `https://developers.google.com/android/reference/com/google/mlkit/genai/agent/Agent.Builder#Builder()()` |

| ### Public methods |
|---|---|
| `final @https://developer.android.com/reference/androidx/annotation/NonNull.html https://developers.google.com/android/reference/com/google/mlkit/genai/agent/Agent.Builder` | `@https://errorprone.info/api/latest/com/google/errorprone/annotations/CanIgnoreReturnValue.html https://developers.google.com/android/reference/com/google/mlkit/genai/agent/Agent.Builder#addAppFunctionId(kotlin.String)(@https://developer.android.com/reference/androidx/annotation/NonNull.html https://developer.android.com/reference/java/lang/String.html functionId)` Registers a specific AppFunction identifier (either a full ID such as `"com.example.MyAppFunctionService#createNote"` or a short method name such as `"createNote"`) to be made available to the agent. |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull.html https://developers.google.com/android/reference/com/google/mlkit/genai/agent/Agent.Builder` | `@https://errorprone.info/api/latest/com/google/errorprone/annotations/CanIgnoreReturnValue.html https://developers.google.com/android/reference/com/google/mlkit/genai/agent/Agent.Builder#addAppFunctionIds(kotlin.collections.List)(@https://developer.android.com/reference/androidx/annotation/NonNull.html https://developer.android.com/reference/java/util/List.html<@https://developer.android.com/reference/androidx/annotation/NonNull.html https://developer.android.com/reference/java/lang/String.html> functionIds)` Registers a list of AppFunction identifiers (either full IDs such as `"com.example.MyAppFunctionService#createNote"` or short method names such as `"createNote"`) to be made available to the agent. |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull.html https://developers.google.com/android/reference/com/google/mlkit/genai/agent/Agent.Builder` | `@https://errorprone.info/api/latest/com/google/errorprone/annotations/CanIgnoreReturnValue.html https://developers.google.com/android/reference/com/google/mlkit/genai/agent/Agent.Builder#addAppFunctionIds(kotlin.Array)(@https://developer.android.com/reference/androidx/annotation/NonNull.html https://developer.android.com/reference/java/lang/String.html... functionIds)` Registers AppFunction identifiers (either full IDs such as `"com.example.MyAppFunctionService#createNote"` or short method names such as `"createNote"`) to be made available to the agent. |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull.html https://developers.google.com/android/reference/com/google/mlkit/genai/agent/Agent.Builder` | `@https://errorprone.info/api/latest/com/google/errorprone/annotations/CanIgnoreReturnValue.html https://developers.google.com/android/reference/com/google/mlkit/genai/agent/Agent.Builder#addTool(com.google.mlkit.genai.agent.AgentTool)(@https://developer.android.com/reference/androidx/annotation/NonNull.html https://developers.google.com/android/reference/com/google/mlkit/genai/agent/AgentTool tool)` Adds a single AppFunction tool (or a decorator wrapping one) to the agent. |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull.html https://developers.google.com/android/reference/com/google/mlkit/genai/agent/Agent.Builder` | `@https://errorprone.info/api/latest/com/google/errorprone/annotations/CanIgnoreReturnValue.html https://developers.google.com/android/reference/com/google/mlkit/genai/agent/Agent.Builder#addTools(com.google.mlkit.genai.agent.AgentTool)(@https://developer.android.com/reference/androidx/annotation/NonNull.html https://developers.google.com/android/reference/com/google/mlkit/genai/agent/AgentTool tool)` Adds a single AppFunction tool (or a decorator wrapping one) to the agent. |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull.html https://developers.google.com/android/reference/com/google/mlkit/genai/agent/Agent.Builder` | `@https://errorprone.info/api/latest/com/google/errorprone/annotations/CanIgnoreReturnValue.html https://developers.google.com/android/reference/com/google/mlkit/genai/agent/Agent.Builder#addTools(kotlin.Array)(@https://developer.android.com/reference/androidx/annotation/NonNull.html https://developers.google.com/android/reference/com/google/mlkit/genai/agent/AgentTool... tools)` Adds multiple AppFunction tools (or decorators wrapping them) to the agent. |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull.html https://developers.google.com/android/reference/com/google/mlkit/genai/agent/Agent.Builder` | `@https://errorprone.info/api/latest/com/google/errorprone/annotations/CanIgnoreReturnValue.html https://developers.google.com/android/reference/com/google/mlkit/genai/agent/Agent.Builder#addTools(kotlin.collections.List)(@https://developer.android.com/reference/androidx/annotation/NonNull.html https://developer.android.com/reference/java/util/List.html<@https://developer.android.com/reference/androidx/annotation/NonNull.html https://developers.google.com/android/reference/com/google/mlkit/genai/agent/AgentTool> tools)` Adds a collection of AppFunction tools (or decorators wrapping them) to the agent. |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull.html https://developers.google.com/android/reference/com/google/mlkit/genai/agent/Agent` | `https://developers.google.com/android/reference/com/google/mlkit/genai/agent/Agent.Builder#build()()` Builds the `https://developers.google.com/android/reference/com/google/mlkit/genai/agent/Agent` instance. |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull.html https://developers.google.com/android/reference/com/google/mlkit/genai/agent/Agent.Builder` | `@https://errorprone.info/api/latest/com/google/errorprone/annotations/CanIgnoreReturnValue.html https://developers.google.com/android/reference/com/google/mlkit/genai/agent/Agent.Builder#enableAppFunctions(kotlin.Boolean)(boolean enabled)` Enables automatic discovery and execution of functions annotated with `@androidx.appfunctions.AppFunction` across the calling application. |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull.html https://developers.google.com/android/reference/com/google/mlkit/genai/agent/Agent.Builder` | `@https://errorprone.info/api/latest/com/google/errorprone/annotations/CanIgnoreReturnValue.html https://developers.google.com/android/reference/com/google/mlkit/genai/agent/Agent.Builder#setInstructions(kotlin.String)(@https://developer.android.com/reference/androidx/annotation/NonNull.html https://developer.android.com/reference/java/lang/String.html instructions)` Sets the system instructions for the agent. |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull.html https://developers.google.com/android/reference/com/google/mlkit/genai/agent/Agent.Builder` | `@https://errorprone.info/api/latest/com/google/errorprone/annotations/CanIgnoreReturnValue.html https://developers.google.com/android/reference/com/google/mlkit/genai/agent/Agent.Builder#setMaxTurns(kotlin.Int)(int maxTurns)` Sets the maximum execution steps allowed per turn before forced termination. |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull.html https://developers.google.com/android/reference/com/google/mlkit/genai/agent/Agent.Builder` | `@https://errorprone.info/api/latest/com/google/errorprone/annotations/CanIgnoreReturnValue.html https://developers.google.com/android/reference/com/google/mlkit/genai/agent/Agent.Builder#setName(kotlin.String)(@https://developer.android.com/reference/androidx/annotation/NonNull.html https://developer.android.com/reference/java/lang/String.html name)` Sets the name of the agent. |

## Public constructors

### Builder

```
public Builder()
```

## Public methods

### addAppFunctionId

```
@CanIgnoreReturnValue
public final @NonNull Agent.Builder addAppFunctionId(@NonNull String functionId)
```

Registers a specific AppFunction identifier (either a full ID such as `"com.example.MyAppFunctionService#createNote"` or a short method name such as `"createNote"`) to be made available to the agent.

Scoping to specific function IDs disables package-level metadata instructions injection.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull.html https://developer.android.com/reference/java/lang/String.html functionId` | The AppFunction identifier or method name to enable. |

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull.html https://developers.google.com/android/reference/com/google/mlkit/genai/agent/Agent.Builder` | This builder |

### addAppFunctionIds

```
@CanIgnoreReturnValue
public final @NonNull Agent.Builder addAppFunctionIds(@NonNull List<@NonNull String> functionIds)
```

Registers a list of AppFunction identifiers (either full IDs such as `"com.example.MyAppFunctionService#createNote"` or short method names such as `"createNote"`) to be made available to the agent.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull.html https://developer.android.com/reference/java/util/List.html<@https://developer.android.com/reference/androidx/annotation/NonNull.html https://developer.android.com/reference/java/lang/String.html> functionIds` | The AppFunction identifiers or method names to enable. |

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull.html https://developers.google.com/android/reference/com/google/mlkit/genai/agent/Agent.Builder` | This builder |

### addAppFunctionIds

```
@CanIgnoreReturnValue
public final @NonNull Agent.Builder addAppFunctionIds(@NonNull String... functionIds)
```

Registers AppFunction identifiers (either full IDs such as `"com.example.MyAppFunctionService#createNote"` or short method names such as `"createNote"`) to be made available to the agent.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull.html https://developer.android.com/reference/java/lang/String.html... functionIds` | The AppFunction identifiers or method names to enable. |

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull.html https://developers.google.com/android/reference/com/google/mlkit/genai/agent/Agent.Builder` | This builder |

### addTool

```
@CanIgnoreReturnValue
public final @NonNull Agent.Builder addTool(@NonNull AgentTool tool)
```

Adds a single AppFunction tool (or a decorator wrapping one) to the agent.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull.html https://developers.google.com/android/reference/com/google/mlkit/genai/agent/AgentTool tool` | Tool to add |

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull.html https://developers.google.com/android/reference/com/google/mlkit/genai/agent/Agent.Builder` | This builder |

| Throws |
|---|---|
| `https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-illegal-argument-exception/index.html` | if `https://developers.google.com/android/reference/com/google/mlkit/genai/agent/Agent.Builder#addTool(com.google.mlkit.genai.agent.AgentTool)` is not an AppFunction tool discovered via `https://developers.google.com/android/reference/com/google/mlkit/genai/agent/AppFunctionAgentTools#discover(kotlinx.coroutines.CoroutineDispatcher)` (or a decorator wrapping one). |

### addTools

```
@CanIgnoreReturnValue
public final @NonNull Agent.Builder addTools(@NonNull AgentTool tool)
```

Adds a single AppFunction tool (or a decorator wrapping one) to the agent.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull.html https://developers.google.com/android/reference/com/google/mlkit/genai/agent/AgentTool tool` | Tool to add |

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull.html https://developers.google.com/android/reference/com/google/mlkit/genai/agent/Agent.Builder` | This builder |

| Throws |
|---|---|
| `https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-illegal-argument-exception/index.html` | if `https://developers.google.com/android/reference/com/google/mlkit/genai/agent/Agent.Builder#addTools(com.google.mlkit.genai.agent.AgentTool)` is not an AppFunction tool discovered via `https://developers.google.com/android/reference/com/google/mlkit/genai/agent/AppFunctionAgentTools#discover(kotlinx.coroutines.CoroutineDispatcher)` (or a decorator wrapping one). |

### addTools

```
@CanIgnoreReturnValue
public final @NonNull Agent.Builder addTools(@NonNull AgentTool... tools)
```

Adds multiple AppFunction tools (or decorators wrapping them) to the agent.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull.html https://developers.google.com/android/reference/com/google/mlkit/genai/agent/AgentTool... tools` | Vararg tools to be made available to the agent |

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull.html https://developers.google.com/android/reference/com/google/mlkit/genai/agent/Agent.Builder` | This builder |

| Throws |
|---|---|
| `https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-illegal-argument-exception/index.html` | if any tool in `https://developers.google.com/android/reference/com/google/mlkit/genai/agent/Agent.Builder#addTools(kotlin.Array)` is not an AppFunction tool discovered via `https://developers.google.com/android/reference/com/google/mlkit/genai/agent/AppFunctionAgentTools#discover(kotlinx.coroutines.CoroutineDispatcher)` (or a decorator wrapping one). |

### addTools

```
@CanIgnoreReturnValue
public final @NonNull Agent.Builder addTools(@NonNull List<@NonNull AgentTool> tools)
```

Adds a collection of AppFunction tools (or decorators wrapping them) to the agent.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull.html https://developer.android.com/reference/java/util/List.html<@https://developer.android.com/reference/androidx/annotation/NonNull.html https://developers.google.com/android/reference/com/google/mlkit/genai/agent/AgentTool> tools` | Tools to be made available to the agent |

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull.html https://developers.google.com/android/reference/com/google/mlkit/genai/agent/Agent.Builder` | This builder |

| Throws |
|---|---|
| `https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-illegal-argument-exception/index.html` | if any tool in `https://developers.google.com/android/reference/com/google/mlkit/genai/agent/Agent.Builder#addTools(kotlin.collections.List)` is not an AppFunction tool discovered via `https://developers.google.com/android/reference/com/google/mlkit/genai/agent/AppFunctionAgentTools#discover(kotlinx.coroutines.CoroutineDispatcher)` (or a decorator wrapping one). |

### build

```
public final @NonNull Agent build()
```

Builds the `https://developers.google.com/android/reference/com/google/mlkit/genai/agent/Agent` instance.

### enableAppFunctions

```
@CanIgnoreReturnValue
public final @NonNull Agent.Builder enableAppFunctions(boolean enabled)
```

Enables automatic discovery and execution of functions annotated with `@androidx.appfunctions.AppFunction` across the calling application.

When enabled, discovered functions are invoked automatically by `https://developers.google.com/android/reference/com/google/mlkit/genai/agent/AgentRunner` during a turn whenever the model emits a matching tool call. If a function requires user confirmation (Human-in-the-Loop), runtime permissions, or custom validation before execution, use `https://developers.google.com/android/reference/com/google/mlkit/genai/agent/AppFunctionAgentTools#discover(kotlinx.coroutines.CoroutineDispatcher)` instead, wrap the sensitive `https://developers.google.com/android/reference/com/google/mlkit/genai/agent/AgentTool`'s `https://developers.google.com/android/reference/com/google/mlkit/genai/agent/AgentTool#execute(kotlin.collections.Map)` method to prompt the user before delegating, and register the wrapped tools via `https://developers.google.com/android/reference/com/google/mlkit/genai/agent/Agent.Builder#addTools(com.google.mlkit.genai.agent.AgentTool)`.

| Parameters |
|---|---|
| `boolean enabled` | Whether to enable AppFunctions discovery and execution. Defaults to true. |

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull.html https://developers.google.com/android/reference/com/google/mlkit/genai/agent/Agent.Builder` | This builder |

### setInstructions

```
@CanIgnoreReturnValue
public final @NonNull Agent.Builder setInstructions(@NonNull String instructions)
```

Sets the system instructions for the agent. Defaults to an empty string.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull.html https://developer.android.com/reference/java/lang/String.html instructions` | System prompt guiding agent behavior and personality |

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull.html https://developers.google.com/android/reference/com/google/mlkit/genai/agent/Agent.Builder` | This builder |

### setMaxTurns

```
@CanIgnoreReturnValue
public final @NonNull Agent.Builder setMaxTurns(int maxTurns)
```

Sets the maximum execution steps allowed per turn before forced termination. Defaults to 10.

| Parameters |
|---|---|
| `int maxTurns` | Maximum turns allowed. Must be greater than 0 |

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull.html https://developers.google.com/android/reference/com/google/mlkit/genai/agent/Agent.Builder` | This builder |

### setName

```
@CanIgnoreReturnValue
public final @NonNull Agent.Builder setName(@NonNull String name)
```

Sets the name of the agent. Defaults to "Assistant".

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull.html https://developer.android.com/reference/java/lang/String.html name` | Identifier name for the agent |

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull.html https://developers.google.com/android/reference/com/google/mlkit/genai/agent/Agent.Builder` | This builder |