LangChain4j Agent
Since Camel 4.14
Only producer is supported
The LangChain4j Agent component provides comprehensive AI agent capabilities by integrating with the LangChain4j library. This component supports advanced AI agent patterns including tool calling, MCP (Model Context Protocol) integration, conversation memory, retrieval-augmented generation (RAG), and input/output guardrails.
Features
The LangChain4j Agent component offers the following key features:
-
Agent-Based Architecture: Flexible agent creation using the
AgentAPI interface -
Tool Integration: Seamless integration with Camel routes via the
langchain4j-toolscomponent -
MCP Tools: Integration with Model Context Protocol (MCP) tools for external system access
-
Conversation Memory: Persistent chat memory for maintaining conversation context
-
RAG Support: Integration with retrieval systems for naive and advanced RAG
-
Guardrails: Input and output validation and transformation
-
Configuration Flexibility: Centralized agent configuration using
AgentConfiguration -
Multimodal Content: Support for images, PDFs, audio, video, and text files from file-based Camel components
Component Options
The component has been simplified to use only two main options:
-
agent: Reference to an
Agentimplementation registered in the Camel registry -
tags: Tags for discovering and calling Camel route tools (optional)
All other configuration (chat models, memory, RAG, guardrails, custom tools, MCP clients) is now handled through the AgentConfiguration when creating agents.
URI format
langchain4j-agent:agentId[?options] Where agentId is a unique identifier for the agent instance.
Configuring Options
Camel components are configured on two separate levels:
-
component level
-
endpoint level
Configuring Component Options
At the component level, you set general and shared configurations that are, then, inherited by the endpoints. It is the highest configuration level.
For example, a component may have security settings, credentials for authentication, urls for network connection and so forth.
Some components only have a few options, and others may have many. Because components typically have pre-configured defaults that are commonly used, then you may often only need to configure a few options on a component; or none at all.
You can configure components using:
-
the Component DSL.
-
in a configuration file (
application.properties,*.yamlfiles, etc). -
directly in the Java code.
Configuring Endpoint Options
You usually spend more time setting up endpoints because they have many options. These options help you customize what you want the endpoint to do. The options are also categorized into whether the endpoint is used as a consumer (from), as a producer (to), or both.
Configuring endpoints is most often done directly in the endpoint URI as path and query parameters. You can also use the Endpoint DSL and DataFormat DSL as a type safe way of configuring endpoints and data formats in Java.
A good practice when configuring options is to use Property Placeholders.
Property placeholders provide a few benefits:
-
They help prevent using hardcoded urls, port numbers, sensitive information, and other settings.
-
They allow externalizing the configuration from the code.
-
They help the code to become more flexible and reusable.
The following two sections list all the options, firstly for the component followed by the endpoint.
Component Options
The LangChain4j Agent component supports 6 options, which are listed below.
| Name | Description | Default | Type |
|---|---|---|---|
Autowired The agent to use for the component. | Agent | ||
Autowired The agent factory to use for creating agents if no Agent is provided. | AgentFactory | ||
The configuration. | LangChain4jAgentConfiguration | ||
Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing. | false | boolean | |
Tags for discovering and calling Camel route tools. | String | ||
Whether autowiring is enabled. This is used for automatic autowiring options (the option must be marked as autowired) by looking up in the registry to find if there is a single instance of matching type, which then gets configured on the component. This can be used for automatic configuring JDBC data sources, JMS connection factories, AWS Clients, etc. | true | boolean |
Endpoint Options
The LangChain4j Agent endpoint is configured using URI syntax:
langchain4j-agent:agentId
With the following path and query parameters:
Query Parameters (4 parameters)
| Name | Description | Default | Type |
|---|---|---|---|
Autowired The agent to use for the component. | Agent | ||
Autowired The agent factory to use for creating agents if no Agent is provided. | AgentFactory | ||
Tags for discovering and calling Camel route tools. | String | ||
Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing. | false | boolean |
Message Headers
The LangChain4j Agent component supports 4 message header(s), which is/are listed below:
| Name | Description | Default | Type |
|---|---|---|---|
CamelLangChain4jAgentSystemMessage (producer) Constant: | The system prompt. | String | |
CamelLangChain4jAgentMemoryId (producer) Constant: | Memory ID. | Object | |
CamelLangChain4jAgentUserMessage (producer) Constant: | The user message to accompany file content when using WrappedFile as input. | String | |
CamelLangChain4jAgentMediaType (producer) Constant: | The media type (MIME type) of the file content. Overrides auto-detection from file extension. | String |
Spring Boot Auto-Configuration
When using langchain4j-agent with Spring Boot make sure to use the following Maven dependency to have support for auto configuration:
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-langchain4j-agent-starter</artifactId>
<version>x.x.x</version>
<!-- use the same version as your Camel core version -->
</dependency> The component supports 7 options, which are listed below.
| Name | Description | Default | Type |
|---|---|---|---|
The agent to use for the component. The option is a org.apache.camel.component.langchain4j.agent.api.Agent type. | Agent | ||
The agent factory to use for creating agents if no Agent is provided. The option is a org.apache.camel.component.langchain4j.agent.api.AgentFactory type. | AgentFactory | ||
Whether autowiring is enabled. This is used for automatic autowiring options (the option must be marked as autowired) by looking up in the registry to find if there is a single instance of matching type, which then gets configured on the component. This can be used for automatic configuring JDBC data sources, JMS connection factories, AWS Clients, etc. | true | Boolean | |
The configuration. The option is a org.apache.camel.component.langchain4j.agent.LangChain4jAgentConfiguration type. | LangChain4jAgentConfiguration | ||
Whether to enable auto configuration of the langchain4j-agent component. This is enabled by default. | Boolean | ||
Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing. | false | Boolean | |
Tags for discovering and calling Camel route tools. | String |
Usage
Creating Custom Agents
Starting from Camel 4.14, the LangChain4j Agent component uses an agent-based architecture where agents are created by implementing the org.apache.camel.component.langchain4j.agent.api.Agent interface. The component provides two built-in agent implementations:
-
AgentWithMemory- For chat interactions with conversation history -
AgentWithoutMemory- For stateless chat interactions
Agent Configuration
Agents are configured using the AgentConfiguration class which provides a fluent API for setting up:
-
Chat Model
-
Chat Memory Provider (for memory-enabled agents)
-
Retrieval Augmentor (for RAG functionality)
-
Input and Output Guardrails
Creating an Agent without Memory
// Create and configure the chat model
ChatModel chatModel = OpenAiChatModel.builder()
.apiKey(openApiKey)
.modelName(GPT_3_5_TURBO)
.temperature(0.3)
.timeout(ofSeconds(3000))
.build();
// Create agent configuration
AgentConfiguration configuration = new AgentConfiguration()
.withChatModel(chatModel)
.withInputGuardrailClasses(List.of())
.withOutputGuardrailClasses(List.of());
// Create the agent
Agent simpleAgent = new AgentWithoutMemory(configuration);
// Register the agent in the Camel context
context.getRegistry().bind("simpleAgent", simpleAgent); Use the agent in your Camel route:
from("direct:chat")
.to("langchain4j-agent:test?agent=#simpleAgent") Creating an Agent with Memory
// Create chat model (same as above)
ChatModel chatModel = OpenAiChatModel.builder()...
// Create memory provider
ChatMemoryProvider memoryProvider = memoryId -> MessageWindowChatMemory.builder()
.id(memoryId)
.maxMessages(10)
.chatMemoryStore(persistentStore) // Your persistent store implementation
.build();
// Create agent configuration with memory
AgentConfiguration configuration = new AgentConfiguration()
.withChatModel(chatModel)
.withChatMemoryProvider(memoryProvider)
.withInputGuardrailClasses(List.of())
.withOutputGuardrailClasses(List.of());
// Create the agent
Agent memoryAgent = new AgentWithMemory(configuration);
// Register the agent
context.getRegistry().bind("memoryAgent", memoryAgent); | Add the |
Basic Chat with only a userMessage
For simple chat interactions, you can use an agent without memory.
from("direct:chat")
.to("langchain4j-agent:simple?agent=#simpleAgent") The body can either contain the prompt as a String, or you can create an object of type org.apache.camel.component.langchain4j.agent.api.AiAgentBody containing the userMessage.
var prompt = "What is Apache Camel";
String response = template.requestBody("direct:chat", prompt, String.class); var prompt = "What is Apache Camel";
AiAgentBody body = new AiAgentBody(prompt);
String response = template.requestBody("direct:chat", body, String.class); Basic Chat with user and system messages
For chat interactions with system prompts, you can use an agent without memory.
from("direct:chat")
.to("langchain4j-agent:simple?agent=#simpleAgent") The body can either contain the user prompt as a String and specifying the CamelLangChain4jAgentSystemMessage header for the system prompt, or you can create an object of type org.apache.camel.component.langchain4j.agent.api.AiAgentBody containing both userMessage and systemMessage.
var userPrompt = "Write a short story about a lost cat.";
var systemPrompt = "You are a whimsical storyteller. Your responses should be imaginative, descriptive, and always include a touch of magic. Start every story with 'Once upon a starlit night...";
String response = template.requestBodyAndHeader("direct:chat",
userPrompt, "CamelLangChain4jAgentSystemMessage", systemPrompt , String.class); var userPrompt = "Write a short story about a lost cat.";
var systemPrompt = "You are a whimsical storyteller. Your responses should be imaginative, descriptive, and always include a touch of magic. Start every story with 'Once upon a starlit night...";
AiAgentBody body = new AiAgentBody()
.withUserMessage(userPrompt)
.withSystemMessage(systemPrompt);
String response = template.requestBody("direct:chat", body, String.class); Chat with Tools
Integrate with Camel routes as tools. The LangChain4j Agent component integrates with Camel Routes defined using the Camel LangChain4j Tools component via the tags parameter.
// Define tool routes
from("langchain4j-tools:userDb?tags=users&description=Query user database¶meter.userId=string")
.setBody(constant("{\"name\": \"John Doe\", \"id\": \"123\"}"));
from("langchain4j-tools:weather?tags=weather&description=Get weather information¶meter.city=string")
.setBody(constant("{\"weather\": \"sunny\", \"temperature\": \"22°C\"}"));
// Agent with tools (using the created agent)
from("direct:chat")
.to("langchain4j-agent:tools?agent=#simpleAgent&tags=users,weather"); var userPrompt = "Can you tell me the name of user 123 and the weather in New York?";
var systemPrompt = "You are a helpful assistant that can access user database and weather information. Use the available tools to provide accurate information.";
String response = template.requestBodyAndHeader("direct:chat",
userPrompt, "CamelLangChain4jAgentSystemMessage", systemPrompt , String.class); | There’s no need to add Camel LangChain4j Tools component as a dependency when using the tools with LangChain4j Agent component. |
Custom LangChain4j Tools
You can also add custom LangChain4j tools using the @Tool annotation. These tools are passed directly to the agent via the customTools configuration parameter.
Creating Custom LangChain4j Tools
Create a class with methods annotated with @Tool:
import dev.langchain4j.agent.tool.P;
import dev.langchain4j.agent.tool.Tool;
public class CalculatorTool {
@Tool("Adds two numbers")
public int add(@P("First number") int a, @P("Second number") int b) {
return a + b;
}
@Tool("Multiplies two numbers")
public int multiply(@P("First number") int a, @P("Second number") int b) {
return a * b;
}
@Tool("Gets the square root of a number")
public double sqrt(@P("Number") double x) {
return Math.sqrt(x);
}
} Using Custom LangChain4j Tools with Agent
Pass your custom tool instances to the agent configuration:
// Create tool instances
CalculatorTool calculator = new CalculatorTool();
WeatherTool weather = new WeatherTool();
// Create agent configuration with custom tools
AgentConfiguration config = new AgentConfiguration()
.withChatModel(chatModel)
.withCustomTools(Arrays.asList(calculator, weather));
// Create agent
Agent agent = new AgentWithoutMemory(config);
// Register agent in Camel context
context.getRegistry().bind("customToolsAgent", agent); Mixed Tools (Camel Routes + Custom LangChain4j Tools)
You can combine both Camel route tools (via tags) and custom LangChain4j tools (via customTools) in the same agent:
// Define Camel route tools
from("langchain4j-tools:weatherService?tags=weather&description=Get current weather information¶meter.location=string")
.setBody(constant("{\"weather\": \"sunny\", \"location\": \"Current Location\"}"));
// Create custom tool instances
CalculatorTool calculator = new CalculatorTool();
StringTool stringTool = new StringTool();
// Create agent configuration with both types of tools
AgentConfiguration config = new AgentConfiguration()
.withChatModel(chatModel)
.withCustomTools(Arrays.asList(calculator, stringTool));
// Create agent
Agent agent = new AgentWithMemory(config);
// Register agent in Camel context
context.getRegistry().bind("mixedToolsAgent", agent); Route Configuration with Mixed Tools
from("direct:chat")
.to("langchain4j-agent:assistant?agent=#mixedToolsAgent&tags=weather")
.to("mock:agent-response"); Usage Example
String response = template.requestBody("direct:chat",
"Calculate 10 * 5 and tell me the weather in London", String.class); | When using mixed tools, Camel route tools are discovered dynamically via the |
MCP (Model Context Protocol) Tools
The LangChain4j Agent component supports integration with MCP (Model Context Protocol) tools, allowing agents to interact with external systems and services through standardized protocols.
Configuring MCP Clients
To use MCP tools with your agent, you need to configure MCP clients in your AgentConfiguration:
// Create MCP transport for filesystem server
McpTransport filesystemTransport = new StdioMcpTransport.Builder()
.command(Arrays.asList("npx", "-y", "@modelcontextprotocol/server-filesystem", "/path/to/directory"))
.logEvents(true)
.build();
// Create MCP client
McpClient filesystemClient = new DefaultMcpClient.Builder()
.transport(filesystemTransport)
.build();
// Create agent configuration with MCP clients
AgentConfiguration config = new AgentConfiguration()
.withChatModel(chatModel)
.withMcpClients(Arrays.asList(filesystemClient));
// Create agent
Agent agent = new AgentWithoutMemory(config); MCP Tool Filtering
You can apply filters to control which MCP tools are available to your agent:
// Create security filter to only allow read operations
BiPredicate<McpClient, ToolSpecification> securityFilter = (client, toolSpec) -> {
String toolName = toolSpec.name().toLowerCase();
// Only allow read operations for safety
return toolName.contains("read") || toolName.contains("list") || toolName.contains("get");
};
AgentConfiguration config = new AgentConfiguration()
.withChatModel(chatModel)
.withMcpClients(Arrays.asList(filesystemClient))
.withMcpToolProviderFilter(securityFilter); Complete MCP Example
Here’s a complete example showing how to use MCP tools with an agent:
// Create MCP client for filesystem access
McpTransport transport = new StdioMcpTransport.Builder()
.command(Arrays.asList("npx", "-y", "@modelcontextprotocol/server-filesystem", "/tmp"))
.logEvents(true)
.build();
McpClient mcpClient = new DefaultMcpClient.Builder()
.transport(transport)
.build();
// Create agent with MCP tools
AgentConfiguration config = new AgentConfiguration()
.withChatModel(chatModel)
.withMcpClient(mcpClient);
Agent agent = new AgentWithoutMemory(config);
// Register agent in Camel context
context.getRegistry().bind("mcpAgent", agent); RAG Integration
RAG (Retrieval-Augmented Generation) is supported by configuring a RetrievalAugmentor in the AgentConfiguration. Create an agent with RAG capabilities:
// Create the retrieval augmentor (shown below)
RetrievalAugmentor retrievalAugmentor = createRetrievalAugmentor();
// Create agent configuration with RAG
AgentConfiguration configuration = new AgentConfiguration()
.withChatModel(chatModel)
.withRetrievalAugmentor(retrievalAugmentor)
.withInputGuardrailClasses(List.of())
.withOutputGuardrailClasses(List.of());
Agent ragAgent = new AgentWithoutMemory(configuration);
context.getRegistry().bind("ragAgent", ragAgent);
// Use the RAG agent
from("direct:chat")
.to("langchain4j-agent:rag?agent=#ragAgent") // creating the retrieval Augmentor
EmbeddingStoreContentRetriever contentRetriever = EmbeddingStoreContentRetriever.builder()
.embeddingStore(embeddingStore) // the embedding store should be defined
.embeddingModel(embeddingModel) // the embedding model should be defined
.maxResults(3)
.minScore(0.6)
.build();
RetrievalAugmentor retrievalAugmentor = DefaultRetrievalAugmentor.builder()
.contentRetriever(contentRetriever)
// other options or steps can be included for Advanced RAG
.build();
// bind the retrievalAugmentor in the context
context.getRegistry().bind("retrievalAugmentor", retrievalAugmentor);
// using the producer
String response = template.requestBody("direct:chat", body, String.class); Chat with Memory
Memory functionality is supported by using AgentWithMemory and configuring a ChatMemoryProvider in the AgentConfiguration. The memory works for multiple users/sessions.
| The component requires using a Chat Memory Provider that uses a persistent memory store. |
The memory works for multiple users/sessions. For each context window, the users needs to set the memory ID: - By setting the Header CamelLangChain4jAgentMemoryId. This supposes that user is using a body as String. - By setting the AiAgentBody.memoryId field. This supposes that that user is using a body as AiAgentBody.
from("direct:chat")
.to("langchain4j-agent:memory?agent=#memoryAgent") // Example of creating a Chat Memory Provider : Create a message window memory that keeps the last 10 messages
ChatMemoryProvider chatMemoryProvider = memoryId -> MessageWindowChatMemory.builder()
.id(memoryId)
.maxMessages(10)
.chatMemoryStore(store) // the Chat Memory store is previously created
.build();
// bind the chat memory provider in the context
context.getRegistry().bind("chatMemoryProvider", chatMemoryProvider);
AiAgentBody request = new AiAgentBody("Hello!", null, "session-123");
String response = template.requestBody("direct:chat", request, String.class); Input and Output Guardrails
Guardrails validate and filter AI interactions to ensure security and quality. The LangChain4j Agent component provides both custom guardrail support and a set of production-ready built-in guardrails.
Using Custom Guardrails
Create classes defining InputGuardrails and OutputGuardrails as defined in the LangChain4j Guardrails documentation page.
// Create agent configuration with custom guardrails
AgentConfiguration configuration = new AgentConfiguration()
.withChatModel(chatModel)
.withInputGuardrailClassesList("com.example.MyInputGuardrail")
.withOutputGuardrailClassesList("com.example.MyOutputGuardrail1,com.example.MyOutputGuardrail2");
Agent safeAgent = new AgentWithoutMemory(configuration);
context.getRegistry().bind("safeAgent", safeAgent);
// Use the agent with guardrails
from("direct:agent-with-guardrails")
.to("langchain4j-agent:safe?agent=#safeAgent") | The current version of the component returns a String as response. If the outputGuardrails extends JsonExtractorOutputGuardrail class, make sure to return a Json in String format. |
Built-in Guardrails
The camel-langchain4j-agent-api module provides production-ready guardrails in the org.apache.camel.component.langchain4j.agent.api.guardrails package.
Available Input Guardrails
| Guardrail Class | Description |
|---|---|
| Validates message length (min/max characters). Prevents excessively long inputs. |
| Detects Personally Identifiable Information (email, phone, SSN, credit cards, IP addresses). |
| Detects prompt injection attacks (role manipulation, jailbreaks, delimiter injection). |
| Blocks messages containing specific keywords or patterns. |
| Validates the language/script of user messages. Can allow or block specific languages (English, Cyrillic, Chinese, Japanese, Korean, Arabic, Hebrew, Greek, Thai, Devanagari). |
| Detects potential code injection attempts including shell commands, SQL injection, JavaScript, HTML/XSS, path traversal, command chaining, and template injection. |
| Flexible guardrail using custom regex patterns. Can define deny patterns (block if matched) and require patterns (must be present). |
Available Output Guardrails
| Guardrail Class | Description |
|---|---|
| Validates response length with optional truncation support. |
| Detects and optionally redacts sensitive data (API keys, passwords, JWT tokens, AWS keys). |
| Validates JSON format, extracts JSON from text, and checks for required fields. |
| Blocks or redacts specific content in AI responses. |
| Ensures AI responses are not empty or contain only whitespace. Can optionally detect refusal patterns (e.g., "I cannot", "I’m unable to"). |
| Validates the word count of AI responses. Can enforce minimum, maximum, or range constraints. |
Quick Start with Default Guardrails
The Guardrails helper class provides convenient factory methods:
import org.apache.camel.component.langchain4j.agent.api.guardrails.Guardrails;
// Create agent with default security guardrails
AgentConfiguration configuration = new AgentConfiguration()
.withChatModel(chatModel)
.withInputGuardrailClasses(Guardrails.defaultInputGuardrails())
.withOutputGuardrailClasses(Guardrails.defaultOutputGuardrails());
Agent secureAgent = new AgentWithoutMemory(configuration);
context.getRegistry().bind("secureAgent", secureAgent);
from("direct:secure-chat")
.to("langchain4j-agent:chat?agent=#secureAgent"); The default guardrails include:
-
Input:
InputLengthGuardrail,PiiDetectorGuardrail,PromptInjectionGuardrail -
Output:
OutputLengthGuardrail,SensitiveDataOutputGuardrail
Using the Fluent Builder
The Guardrails.configure() method provides a fluent API for building configurations:
import org.apache.camel.component.langchain4j.agent.api.guardrails.Guardrails;
AgentConfiguration configuration = Guardrails.configure()
.withPiiDetection()
.withPromptInjectionDetection()
.withInputLengthValidation()
.withSensitiveDataDetection()
.withJsonFormatValidation()
.build();
configuration.withChatModel(chatModel);
Agent agent = new AgentWithoutMemory(configuration); Using Specific Guardrail Classes
You can specify exactly which guardrails to use:
import org.apache.camel.component.langchain4j.agent.api.guardrails.*;
AgentConfiguration configuration = new AgentConfiguration()
.withChatModel(chatModel)
.withInputGuardrailClasses(List.of(
InputLengthGuardrail.class,
PiiDetectorGuardrail.class,
PromptInjectionGuardrail.class
))
.withOutputGuardrailClasses(List.of(
SensitiveDataOutputGuardrail.class,
JsonFormatGuardrail.class
)); Guardrail Presets
The Guardrails class provides preset configurations for different security levels:
// Minimal protection (just length validation)
configuration.withInputGuardrailClasses(Guardrails.minimalInputGuardrails());
// Standard protection (length, PII, prompt injection)
configuration.withInputGuardrailClasses(Guardrails.defaultInputGuardrails());
// Strict protection (all input guardrails)
configuration.withInputGuardrailClasses(Guardrails.strictInputGuardrails()); Factory Methods for Individual Guardrails
The Guardrails class provides static factory methods to create guardrail instances:
// Input guardrails
InputLengthGuardrail lengthGuard = Guardrails.inputLength(5000); // Max 5000 chars
PiiDetectorGuardrail piiGuard = Guardrails.piiDetector();
PromptInjectionGuardrail injectionGuard = Guardrails.promptInjectionStrict();
KeywordFilterGuardrail keywordGuard = Guardrails.keywordFilter("banned", "blocked");
// Output guardrails
OutputLengthGuardrail outputLength = Guardrails.outputLengthTruncating(10000);
SensitiveDataOutputGuardrail sensitiveData = Guardrails.sensitiveDataRedacting();
JsonFormatGuardrail jsonGuard = Guardrails.jsonFormatWithFields("name", "email"); Advanced Guardrail Configuration with Builders
Each guardrail provides a builder for custom configuration:
PII Detection Configuration
import org.apache.camel.component.langchain4j.agent.api.guardrails.PiiDetectorGuardrail;
import org.apache.camel.component.langchain4j.agent.api.guardrails.PiiDetectorGuardrail.PiiType;
// Detect only specific PII types
PiiDetectorGuardrail guardrail = PiiDetectorGuardrail.builder()
.detectTypes(PiiType.EMAIL, PiiType.PHONE, PiiType.SSN)
.blockOnDetection(true)
.build(); Prompt Injection Detection Configuration
import org.apache.camel.component.langchain4j.agent.api.guardrails.PromptInjectionGuardrail;
// Strict mode: fail on any single pattern match
PromptInjectionGuardrail strictGuardrail = PromptInjectionGuardrail.strict();
// Custom patterns
PromptInjectionGuardrail customGuardrail = PromptInjectionGuardrail.builder()
.strict(true)
.addPattern(
PromptInjectionGuardrail.InjectionCategory.JAILBREAK,
Pattern.compile("(?i)custom\\s+attack\\s+pattern"))
.build(); Sensitive Data Output Configuration
import org.apache.camel.component.langchain4j.agent.api.guardrails.SensitiveDataOutputGuardrail;
import org.apache.camel.component.langchain4j.agent.api.guardrails.SensitiveDataOutputGuardrail.*;
// Redact sensitive data instead of blocking
SensitiveDataOutputGuardrail guardrail = SensitiveDataOutputGuardrail.builder()
.detectTypes(SensitiveDataType.API_KEY, SensitiveDataType.SECRET, SensitiveDataType.JWT)
.action(Action.REDACT)
.redactionText("***REDACTED***")
.build(); JSON Format Validation Configuration
import org.apache.camel.component.langchain4j.agent.api.guardrails.JsonFormatGuardrail;
// Validate JSON with required fields
JsonFormatGuardrail guardrail = JsonFormatGuardrail.builder()
.requireFields("id", "name", "email")
.extractJson(true) // Extract JSON from surrounding text
.allowArray(false) // Only allow objects, not arrays
.build(); Keyword Filtering Configuration
import org.apache.camel.component.langchain4j.agent.api.guardrails.KeywordFilterGuardrail;
import org.apache.camel.component.langchain4j.agent.api.guardrails.KeywordOutputFilterGuardrail;
// Input keyword filter
KeywordFilterGuardrail inputFilter = KeywordFilterGuardrail.builder()
.blockedWords("spam", "inappropriate", "banned")
.blockedPattern("\\b(hack|exploit)\\b")
.caseSensitive(false)
.wholeWordMatch(true)
.build();
// Output keyword filter with redaction
KeywordOutputFilterGuardrail outputFilter = KeywordOutputFilterGuardrail.builder()
.blockedWords("confidential", "internal-only")
.action(KeywordOutputFilterGuardrail.Action.REDACT)
.redactionText("[REMOVED]")
.build(); Language Validation Configuration
import org.apache.camel.component.langchain4j.agent.api.guardrails.LanguageGuardrail;
import org.apache.camel.component.langchain4j.agent.api.guardrails.LanguageGuardrail.Language;
// Allow only English input
LanguageGuardrail englishOnly = LanguageGuardrail.allowOnly(Language.ENGLISH);
// Allow English and Latin script languages (Spanish, French, German, etc.)
LanguageGuardrail latinLanguages = LanguageGuardrail.allowOnly(
Language.ENGLISH, Language.LATIN_SCRIPT);
// Block specific languages
LanguageGuardrail blockCyrillic = LanguageGuardrail.block(Language.CYRILLIC);
// Custom configuration with mixed content control
LanguageGuardrail customLanguage = LanguageGuardrail.builder()
.allowedLanguages(Language.ENGLISH, Language.LATIN_SCRIPT)
.blockedLanguages(Language.CYRILLIC)
.allowMixed(false) // Don't allow mixed language content
.build(); Code Injection Detection Configuration
import org.apache.camel.component.langchain4j.agent.api.guardrails.CodeInjectionGuardrail;
import org.apache.camel.component.langchain4j.agent.api.guardrails.CodeInjectionGuardrail.InjectionType;
// Default: detect all code injection types
CodeInjectionGuardrail defaultGuard = new CodeInjectionGuardrail();
// Strict mode: fail on any single pattern match
CodeInjectionGuardrail strictGuard = CodeInjectionGuardrail.strict();
// Detect only specific injection types
CodeInjectionGuardrail sqlAndShellOnly = CodeInjectionGuardrail.forTypes(
InjectionType.SQL_INJECTION, InjectionType.SHELL_COMMAND);
// Custom configuration
CodeInjectionGuardrail customGuard = CodeInjectionGuardrail.builder()
.detectTypes(InjectionType.SQL_INJECTION, InjectionType.JAVASCRIPT, InjectionType.PATH_TRAVERSAL)
.strict(true)
.build(); Regex Pattern Guardrail Configuration
import org.apache.camel.component.langchain4j.agent.api.guardrails.RegexPatternGuardrail;
// Block messages containing URLs
RegexPatternGuardrail noUrls = RegexPatternGuardrail.blocking(
"https?://[^\\s]+", "URLs are not allowed in messages");
// Require messages to contain a ticket number
RegexPatternGuardrail requireTicket = RegexPatternGuardrail.requiring(
"TICKET-\\d+", "Please include a ticket number (e.g., TICKET-123)");
// Complex configuration with multiple patterns
RegexPatternGuardrail customPatterns = RegexPatternGuardrail.builder()
.denyPattern("https?://[^\\s]+", "URLs are not allowed")
.denyPattern("\\b(password|secret)\\b", "Sensitive keywords are not allowed")
.requirePattern("[A-Z]{2,4}-\\d+", "Please include a valid issue ID")
.failOnFirstMatch(true) // Stop checking after first failure
.build(); Not Empty Guardrail Configuration
import org.apache.camel.component.langchain4j.agent.api.guardrails.NotEmptyGuardrail;
// Default: just ensure response is not empty
NotEmptyGuardrail notEmpty = new NotEmptyGuardrail();
// Also detect refusal patterns like "I cannot", "I'm unable to"
NotEmptyGuardrail withRefusalDetection = NotEmptyGuardrail.withRefusalDetection();
// Require minimum meaningful length
NotEmptyGuardrail minLength = NotEmptyGuardrail.withMinLength(50);
// Custom configuration
NotEmptyGuardrail customNotEmpty = new NotEmptyGuardrail(
true, // detectRefusals
100 // minMeaningfulLength
); Word Count Guardrail Configuration
import org.apache.camel.component.langchain4j.agent.api.guardrails.WordCountGuardrail;
// Require at least 10 words
WordCountGuardrail atLeast10 = WordCountGuardrail.atLeast(10);
// Limit to maximum 500 words
WordCountGuardrail atMost500 = WordCountGuardrail.atMost(500);
// Require between 50 and 200 words
WordCountGuardrail between = WordCountGuardrail.between(50, 200);
// Custom configuration
WordCountGuardrail custom = WordCountGuardrail.builder()
.minWords(20)
.maxWords(1000)
.build(); Complete Example with Memory and Guardrails
import org.apache.camel.component.langchain4j.agent.api.*;
import org.apache.camel.component.langchain4j.agent.api.guardrails.*;
import dev.langchain4j.model.openai.OpenAiChatModel;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
public class SecureAgentRoute extends RouteBuilder {
@Override
public void configure() throws Exception {
// Create chat model
ChatModel chatModel = OpenAiChatModel.builder()
.apiKey("{{openai.api.key}}")
.modelName("gpt-4o")
.build();
// Create memory provider
ChatMemoryProvider memoryProvider = memoryId ->
MessageWindowChatMemory.withMaxMessages(20);
// Create secure agent configuration
AgentConfiguration configuration = new AgentConfiguration()
.withChatModel(chatModel)
.withChatMemoryProvider(memoryProvider)
.withInputGuardrailClasses(Guardrails.defaultInputGuardrails())
.withOutputGuardrailClasses(Guardrails.defaultOutputGuardrails());
Agent secureAgent = new AgentWithMemory(configuration);
getContext().getRegistry().bind("secureAgent", secureAgent);
// Secure chat route with error handling
from("direct:chat")
.setHeader("CamelLangChain4jMemoryId", simple("${header.userId}"))
.doTry()
.to("langchain4j-agent:chat?agent=#secureAgent")
.doCatch(dev.langchain4j.service.guardrail.GuardrailException.class)
.log("Guardrail blocked request: ${exception.message}")
.setBody(constant("Your message was blocked due to security policies."))
.end();
}
} Spring Boot Configuration Example
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AgentConfig {
@Bean
public ChatModel chatModel() {
return OpenAiChatModel.builder()
.apiKey(System.getenv("OPENAI_API_KEY"))
.modelName("gpt-4o")
.build();
}
@Bean("secureAgent")
public Agent secureAgent(ChatModel chatModel) {
AgentConfiguration config = new AgentConfiguration()
.withChatModel(chatModel)
.withInputGuardrailClasses(Guardrails.defaultInputGuardrails())
.withOutputGuardrailClasses(Guardrails.defaultOutputGuardrails());
return new AgentWithoutMemory(config);
}
} Guardrail Behavior Summary
| Guardrail | When Triggered | Result |
|---|---|---|
| Input too short/long | Blocks the request |
| PII detected | Blocks the request |
| Injection attempt detected | Blocks the request |
| Blocked keyword found | Blocks the request |
| Disallowed language/script detected | Blocks the request |
| Code injection pattern detected | Blocks the request |
| Deny pattern matched or require pattern missing | Blocks the request |
| Response too short/long | Retries LLM or truncates |
| Secrets in response | Blocks or redacts |
| Invalid JSON format | Retries LLM call |
| Blocked content in response | Blocks or redacts |
| Empty response or refusal detected | Retries LLM call |
| Word count outside allowed range | Retries LLM call |
Multimodal Content Support
The LangChain4j Agent component supports multimodal content, allowing you to send images, PDFs, audio, video, and text files to AI models that support vision and document understanding capabilities.
Sending Multimodal Content via AiAgentBody
You can explicitly create an AiAgentBody with multimodal content:
// Load an image and create ImageContent
byte[] imageBytes = Files.readAllBytes(Path.of("image.png"));
String base64Image = Base64.getEncoder().encodeToString(imageBytes);
Image image = Image.builder()
.base64Data(base64Image)
.mimeType("image/png")
.build();
ImageContent imageContent = ImageContent.from(image);
// Create request body with image content
AiAgentBody<ImageContent> body = new AiAgentBody<ImageContent>()
.withUserMessage("What do you see in this image?")
.withContent(imageContent);
String response = template.requestBody("direct:chat", body, String.class); Automatic File Conversion from Camel Components
The agent component automatically converts files from various Camel components to multimodal content. This enables seamless integration with file-based sources.
Supported Input Types
| Input Type | Source Components | MIME Type Detection |
|---|---|---|
|
| From file extension or headers |
|
| From content type headers (required) |
| Various streaming components | From content type headers (required) |
Example: Processing Images from File Component
from("file:inbox/images?noop=true&include=.*\\.png")
.setHeader("CamelLangChain4jAgentUserMessage", constant("Describe this image"))
.to("langchain4j-agent:vision?agent=#visionAgent")
.to("log:response"); Example: Processing Files from AWS S3
from("aws2-s3://my-bucket?prefix=images/&includeBody=true")
.setHeader("CamelLangChain4jAgentUserMessage", constant("What do you see in this image?"))
.to("langchain4j-agent:vision?agent=#visionAgent")
.to("log:response"); | When using
|
Complete Multimodal Route Example
Here’s a complete example showing how to process images from a file system and send them to an AI agent for analysis:
// Create a vision-capable chat model
ChatModel chatModel = OpenAiChatModel.builder()
.apiKey(apiKey)
.modelName("gpt-4o") // Vision-capable model
.build();
// Create agent configuration
AgentConfiguration configuration = new AgentConfiguration()
.withChatModel(chatModel);
Agent visionAgent = new AgentWithoutMemory(configuration);
context.getRegistry().bind("visionAgent", visionAgent);
// Route to process images
from("file:inbox/images?noop=true&include=.*\\.(png|jpg|jpeg)")
.setHeader("CamelLangChain4jAgentUserMessage",
constant("Describe what you see in this image. Be detailed but concise."))
.to("langchain4j-agent:vision?agent=#visionAgent")
.log("AI Response: ${body}")
.to("file:outbox/descriptions");