Class AgentBuilder<T, B extends AgentBuilder<T,?> >
java.lang.Object
dev.langchain4j.agentic.agent.AgentBuilder<T,B>
- Direct Known Subclasses:
UntypedAgentBuilder
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionagentInstanceFactory(Function<InternalAgent, Object> factory) Sets a custom factory for creating the agent proxy instance.async(boolean async) Controls whether this agent executes asynchronously within an agentic system.build()protected voidbuild(DefaultAgenticScope agenticScope, AiServiceContext context, AiServices<T> aiServices) chatMemory(ChatMemory chatMemory) Sets theChatMemoryused to maintain conversation history for this agent.chatMemoryProvider(ChatMemoryProvider chatMemoryProvider) Sets aChatMemoryProviderfor creating per-user or per-session chat memory instances.Sets theChatModelused by this agent.chatModel(Function<AgenticScope, ChatModel> chatModelProvider) Sets a provider that resolves theChatModelfrom theAgenticScopeat execution time.contentRetriever(ContentRetriever contentRetriever) Sets theContentRetrieverused for RAG (Retrieval-Augmented Generation).context(Function<AgenticScope, String> contextProvider) Sets a function that provides additional context from theAgenticScopeto this agent's prompt.<K> BdefaultKeyValue(Class<? extends TypedKey<K>> key, K value) Sets a default value for an agent argument identified by aTypedKeyclass.defaultKeyValue(String key, Object value) Sets a default value for an agent argument identified by key name.description(String description) Sets a human-readable description of what this agent does, used by planners and orchestrators.Enables concurrent execution of tool calls using the default executor.executeToolsConcurrently(Executor executor) Enables concurrent execution of tool calls using the provided executor.hallucinatedToolNameStrategy(Function<ToolExecutionRequest, ToolExecutionResultMessage> hallucinatedToolNameStrategy) Sets the strategy for handling tool calls when the model hallucinates a non-existent tool name.<I extends InputGuardrail>
BinputGuardrailClasses(Class<? extends I>... inputGuardrailClasses) Sets the input guardrail classes to be instantiated and applied before each request.<I extends InputGuardrail>
BinputGuardrails(I... inputGuardrails) Sets the input guardrail instances applied before each request.inputGuardrailsConfig(InputGuardrailsConfig inputGuardrailsConfig) Sets the configuration for input guardrails applied before sending requests to the model.static Class[]interfacesToImplement(Class clazz) listener(AgentListener agentListener) Adds anAgentListenerfor observability and lifecycle hooks.maxSequentialToolsInvocations(int maxSequentialToolsInvocations) Deprecated.maxToolCallingRoundTrips(int maxToolCallingRoundTrips) Sets the maximum number of tool-calling round trips the agent may perform in a single invocation.Sets the agent name used for identification in multi-agent systems.optional(boolean optional) Controls whether this agent is optional, allowing the system to skip it if it fails.<O extends OutputGuardrail>
BoutputGuardrailClasses(Class<? extends O>... outputGuardrailClasses) Sets the output guardrail classes to be instantiated and applied after each response.<O extends OutputGuardrail>
BoutputGuardrails(O... outputGuardrails) Sets the output guardrail instances applied after each response.outputGuardrailsConfig(OutputGuardrailsConfig outputGuardrailsConfig) Sets the configuration for output guardrails applied after receiving responses from the model.Sets the output key using aTypedKeyclass for type-safe scope access.Sets the key under which this agent's output is stored in theAgenticScope.retrievalAugmentor(RetrievalAugmentor retrievalAugmentor) Sets theRetrievalAugmentorfor advanced RAG pipelines.streamingChatModel(StreamingChatModel streamingChatModel) Sets theStreamingChatModelused by this agent.streamingChatModel(Function<AgenticScope, StreamingChatModel> streamingChatModelProvider) Sets a provider that resolves theStreamingChatModelfrom theAgenticScopeat execution time.summarizedContext(String... contextProvidingAgents) Sets the names of other agents whose outputs are summarized and injected as context into this agent's prompt.systemMessage(String systemMessage) Sets a static system message for this agent.systemMessageProvider(Function<Object, String> systemMessageProvider) Sets a function that dynamically provides the system message based on the user message object.systemMessageTransformer(BiFunction<String, InvocationContext, String> systemMessageTransformer) Sets a transformer that modifies the system message using both the message and the invocation context.systemMessageTransformer(UnaryOperator<String> systemMessageTransformer) Sets a transformer that modifies the system message before it is sent to the model.toolArgumentsErrorHandler(ToolArgumentsErrorHandler toolArgumentsErrorHandler) Sets the handler invoked when the model provides invalid arguments for a tool call.toolExecutionErrorHandler(ToolExecutionErrorHandler toolExecutionErrorHandler) Sets the handler invoked when a tool execution throws an exception.toolProvider(ToolProvider toolProvider) Adds aToolProviderthat dynamically supplies tools to this agent.toolProviders(ToolProvider... toolProviders) Adds multipleToolProviderinstances that dynamically supply tools to this agent (varargs overload).toolProviders(Collection<ToolProvider> toolProviders) Adds multipleToolProviderinstances that dynamically supply tools to this agent.Sets the tool objects whose@Tool-annotated methods will be available to this agent.tools(Map<ToolSpecification, ToolExecutor> toolsMap) Sets the tool specifications and their executors available to this agent.tools(Map<ToolSpecification, ToolExecutor> toolsMap, Set<String> immediateReturnToolNames) Sets the tool specifications and their executors, with a set of tool names that cause immediate return.userMessage(String userMessage) Sets a static user message for this agent.userMessageProvider(Function<Object, String> userMessageProvider) Sets a function that dynamically provides the user message based on the user message object.static <T> AgentBuilder<T, AgentBuilder<T, ?>> withoutDeclarativeConfiguration(Class<T> agentServiceClass)
-
Constructor Details
-
AgentBuilder
-
-
Method Details
-
withoutDeclarativeConfiguration
public static <T> AgentBuilder<T, AgentBuilder<T,?>> withoutDeclarativeConfiguration(Class<T> agentServiceClass) -
build
-
interfacesToImplement
-
build
protected void build(DefaultAgenticScope agenticScope, AiServiceContext context, AiServices<T> aiServices) -
chatModel
-
streamingChatModel
Sets theStreamingChatModelused by this agent.- Parameters:
streamingChatModel- the streaming chat model- Returns:
this
-
chatModel
Sets a provider that resolves theChatModelfrom theAgenticScopeat execution time.- Parameters:
chatModelProvider- the chat model provider function- Returns:
this
-
streamingChatModel
Sets a provider that resolves theStreamingChatModelfrom theAgenticScopeat execution time.- Parameters:
streamingChatModelProvider- the streaming chat model provider function- Returns:
this
-
chatMemory
Sets theChatMemoryused to maintain conversation history for this agent.- Parameters:
chatMemory- the chat memory- Returns:
this
-
chatMemoryProvider
Sets aChatMemoryProviderfor creating per-user or per-session chat memory instances.- Parameters:
chatMemoryProvider- the chat memory provider- Returns:
this
-
tools
-
tools
Sets the tool specifications and their executors available to this agent.- Parameters:
toolsMap- the map of tool specifications to executors- Returns:
this
-
tools
Sets the tool specifications and their executors, with a set of tool names that cause immediate return.- Parameters:
toolsMap- the map of tool specifications to executorsimmediateReturnToolNames- tool names whose results are returned immediately without further LLM calls- Returns:
this
-
toolProvider
Adds aToolProviderthat dynamically supplies tools to this agent.- Parameters:
toolProvider- the tool provider- Returns:
this
-
toolProviders
Adds multipleToolProviderinstances that dynamically supply tools to this agent.- Parameters:
toolProviders- the collection of tool providers- Returns:
this
-
toolProviders
Adds multipleToolProviderinstances that dynamically supply tools to this agent (varargs overload).- Parameters:
toolProviders- the tool providers- Returns:
this
-
maxToolCallingRoundTrips
Sets the maximum number of tool-calling round trips the agent may perform in a single invocation.- Parameters:
maxToolCallingRoundTrips- the maximum number of round trips- Returns:
this
-
maxSequentialToolsInvocations
@Deprecated(since="1.15.0") public B maxSequentialToolsInvocations(int maxSequentialToolsInvocations) Deprecated.UsemaxToolCallingRoundTrips(int)instead. -
hallucinatedToolNameStrategy
public B hallucinatedToolNameStrategy(Function<ToolExecutionRequest, ToolExecutionResultMessage> hallucinatedToolNameStrategy) Sets the strategy for handling tool calls when the model hallucinates a non-existent tool name.- Parameters:
hallucinatedToolNameStrategy- the function that produces a result message for the hallucinated tool call- Returns:
this
-
contentRetriever
Sets theContentRetrieverused for RAG (Retrieval-Augmented Generation).- Parameters:
contentRetriever- the content retriever- Returns:
this
-
retrievalAugmentor
Sets theRetrievalAugmentorfor advanced RAG pipelines.- Parameters:
retrievalAugmentor- the retrieval augmentor- Returns:
this
-
inputGuardrailsConfig
Sets the configuration for input guardrails applied before sending requests to the model.- Parameters:
inputGuardrailsConfig- the input guardrails configuration- Returns:
this
-
outputGuardrailsConfig
Sets the configuration for output guardrails applied after receiving responses from the model.- Parameters:
outputGuardrailsConfig- the output guardrails configuration- Returns:
this
-
inputGuardrailClasses
public <I extends InputGuardrail> B inputGuardrailClasses(Class<? extends I>... inputGuardrailClasses) Sets the input guardrail classes to be instantiated and applied before each request.- Type Parameters:
I- the guardrail type- Parameters:
inputGuardrailClasses- the input guardrail classes- Returns:
this
-
outputGuardrailClasses
public <O extends OutputGuardrail> B outputGuardrailClasses(Class<? extends O>... outputGuardrailClasses) Sets the output guardrail classes to be instantiated and applied after each response.- Type Parameters:
O- the guardrail type- Parameters:
outputGuardrailClasses- the output guardrail classes- Returns:
this
-
inputGuardrails
Sets the input guardrail instances applied before each request.- Type Parameters:
I- the guardrail type- Parameters:
inputGuardrails- the input guardrails- Returns:
this
-
outputGuardrails
Sets the output guardrail instances applied after each response.- Type Parameters:
O- the guardrail type- Parameters:
outputGuardrails- the output guardrails- Returns:
this
-
name
-
description
-
outputKey
Sets the key under which this agent's output is stored in theAgenticScope.- Parameters:
outputKey- the output key name- Returns:
this
-
outputKey
-
async
Controls whether this agent executes asynchronously within an agentic system.- Parameters:
async-trueto run asynchronously- Returns:
this
-
optional
Controls whether this agent is optional, allowing the system to skip it if it fails.- Parameters:
optional-trueto mark the agent as optional- Returns:
this
-
context
Sets a function that provides additional context from theAgenticScopeto this agent's prompt.- Parameters:
contextProvider- the context provider function- Returns:
this
-
summarizedContext
-
systemMessage
-
systemMessageProvider
-
userMessage
-
userMessageProvider
-
systemMessageTransformer
Sets a transformer that modifies the system message before it is sent to the model.- Parameters:
systemMessageTransformer- the system message transformer- Returns:
this
-
systemMessageTransformer
public B systemMessageTransformer(BiFunction<String, InvocationContext, String> systemMessageTransformer) Sets a transformer that modifies the system message using both the message and the invocation context.- Parameters:
systemMessageTransformer- the system message transformer with context- Returns:
this
-
executeToolsConcurrently
Enables concurrent execution of tool calls using the default executor.- Returns:
this
-
executeToolsConcurrently
-
toolArgumentsErrorHandler
Sets the handler invoked when the model provides invalid arguments for a tool call.- Parameters:
toolArgumentsErrorHandler- the tool arguments error handler- Returns:
this
-
toolExecutionErrorHandler
Sets the handler invoked when a tool execution throws an exception.- Parameters:
toolExecutionErrorHandler- the tool execution error handler- Returns:
this
-
defaultKeyValue
-
defaultKeyValue
-
agentInstanceFactory
Sets a custom factory for creating the agent proxy instance.- Parameters:
factory- the agent instance factory- Returns:
this
-
listener
Adds anAgentListenerfor observability and lifecycle hooks.- Parameters:
agentListener- the agent listener- Returns:
this
-
maxToolCallingRoundTrips(int)instead.