LLM Integration: Apache Camel & OpenAI Component
Large Language Models (LLMs) are increasingly becoming part of enterprise applications. However, integrating an LLM into an existing application is not only about invoking an API. Enterprise applications often need routing, transformation, error handling, logging, retries, message enrichment, validation, and integration with multiple backend systems. Apache Camel’s OpenAI component provides a lightweight way to bring LLM capabilities directly into Camel routes. The component was introduced in Apache Camel 4.17 and supports chat completion and embeddings through the official openai-java SDK. It also supports conversation memory, structured output, streaming, and OpenAI-compatible endpoints. This article demonstrates how to build a Java application using Apache Camel and Spring Boot, integrate it with an OpenAI-compatible LLM endpoint, maintain conversation context across multiple LLM calls within the same Camel exchange, and request structured output from the model.
1. Understanding Apache Camel
Apache Camel is an integration framework based on Enterprise Integration Patterns (EIPs) that provides a consistent way to connect applications, APIs, databases, messaging systems, and external services. Instead of embedding integration logic directly into application services, developers can express the flow of messages using Camel routes. A route defines where a message comes from, how it is processed, and where it should go next. This makes Camel particularly useful for applications that need to combine multiple systems as part of a single business workflow.
An LLM can be treated as another endpoint in that integration flow. For example, a Camel route can receive a customer request, enrich it with data from another service, construct the required prompt, send it to an LLM through the OpenAI component, process the structured response, and then forward the result to another system. In a typical LLM-enabled Camel route, the client request first enters a Camel route, where the prompt is prepared and any required information is added. The request is then sent to the OpenAI component, which communicates with the LLM and returns the response. Camel can subsequently validate, transform, or enrich that response before passing it to a downstream enterprise application.
This approach keeps the LLM as one step within the integration workflow rather than making it the center of the application architecture. Camel continues to handle responsibilities such as routing, message transformation, error handling, and communication with downstream systems, while the OpenAI component provides the LLM capability. As a result, existing Camel-based applications can add LLM functionality without introducing a completely separate integration layer.
1.1 Why use the Camel OpenAI component
The Camel OpenAI component provides several capabilities that make this integration straightforward:
- It integrates LLM calls directly into Camel routes.
- It avoids manually creating and managing an LLM client for simple integration scenarios.
- It supports chat completion and embedding operations.
- It supports structured output through
outputClassandjsonSchema. - It provides conversation memory at the Camel exchange level.
- It supports OpenAI-compatible endpoints through the
baseUrloption. - It works naturally with Camel’s routing, transformation, error-handling, and integration patterns.
The main advantage is that developers can use familiar Camel concepts to build LLM-powered integration flows. For example, a route can combine an HTTP request, data enrichment, an LLM call, response validation, and a downstream API call without requiring a separate orchestration framework.
Apache Camel positions the OpenAI component as a lightweight option for straightforward LLM integration rather than as a complete agent framework. It is therefore a good fit when an application primarily needs to invoke an LLM as part of an existing integration route. More advanced agentic requirements, such as autonomous workflows, complex tool orchestration, or function calling, may require a dedicated AI framework such as Spring AI or LangChain4j.
1.2 Why Use the Camel OpenAI Component
The Camel OpenAI component provides a simple way to add LLM capabilities to existing Camel routes without introducing a separate AI integration layer. It allows developers to invoke an LLM using the same routing and integration concepts already used throughout a Camel application. The component supports chat completion and embeddings, structured responses through outputClass and jsonSchema, conversation memory at the exchange level, and OpenAI-compatible endpoints through the baseUrl option. It can also work with Camel’s existing message transformation, logging, error-handling, and routing capabilities, making it suitable for straightforward LLM-powered integration workflows.
Another advantage is that the LLM remains a part of the integration flow rather than becoming responsible for application orchestration. A Camel route can receive a request, enrich it with enterprise data, invoke the LLM, process its response, and continue the workflow to another service or system. This makes the OpenAI component a lightweight choice when an application needs direct LLM integration but does not require a full agent framework.
2. LLM Integration with Apache Camel OpenAI Component
2.1 Maven Dependencies
To integrate Apache Camel with OpenAI in a Spring Boot application, the project needs the standard Spring Boot web dependency along with Camel’s Spring Boot integration and OpenAI components. The Platform HTTP starter is also included so that the Camel route can expose a simple HTTP endpoint for receiving requests. The following Maven dependencies are sufficient for this example:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-openai-starter</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-platform-http-starter</artifactId>
</dependency>
</dependencies>
The spring-boot-starter-web dependency provides the Spring Boot web infrastructure, while camel-spring-boot-starter integrates Apache Camel with the Spring Boot application context. The camel-openai-starter adds Camel’s OpenAI component, which is responsible for communicating with the LLM, and camel-platform-http-starter allows the application to expose the Camel route as an HTTP endpoint. Together, these dependencies provide everything required for the simple Spring Boot example without introducing additional AI or orchestration frameworks.
2.2 Configuring the OpenAI API Key
The application configuration contains the basic Spring Boot settings and the OpenAI API key configuration. The API key is read from the OPENAI_API_KEY environment variable instead of being hardcoded in the application, which is a safer approach for local development and deployment.
spring.application.name=camel-openai-demo
server.port=8080
camel.component.openai.api-key=${OPENAI_API_KEY}
The spring.application.name property sets the name of the Spring Boot application, while server.port configures the application to listen on port 8080. The camel.component.openai.api-key property supplies the OpenAI API key to Camel’s OpenAI component. The ${OPENAI_API_KEY} placeholder tells Spring Boot to obtain the value from an environment variable, keeping the credential outside the source code.
2.3 Defining the Structured Response
The SupportResponse class represents the structured response expected from the LLM. Instead of returning an unstructured text response, the application asks the model to provide specific fields that can be easily consumed by the rest of the application. The class contains fields for the issue category, priority, summary, and recommended action, along with their corresponding getters and setters.
package com.example.demo;
public class SupportResponse {
private String category;
private String priority;
private String summary;
private String recommendedAction;
// getters and setters
}
The outputClass option in the Camel OpenAI endpoint can reference this class to describe the expected structure of the LLM response. For example, the model can return an authentication-related issue with a priority, a short summary, and a recommended action. This makes the LLM response easier to process in subsequent Camel route steps because the application can work with a predictable structure instead of parsing arbitrary text.
2.4 Creating the Camel Route
The SupportRoute class contains the main Apache Camel route that connects the HTTP endpoint with the OpenAI component. The route accepts a POST request at /support, defines the system instructions for the LLM, and then sends the request to the OpenAI chat completion endpoint. The route also enables conversation memory and specifies SupportResponse as the expected output structure.
package com.example.demo;
import org.apache.camel.builder.RouteBuilder;
import org.springframework.stereotype.Component;
@Component
public class SupportRoute extends RouteBuilder {
@Override
public void configure() {
from("platform-http:/support?httpMethodRestrict=POST")
.routeId("support-route")
.setHeader("CamelOpenAISystemMessage")
.constant("""
You are a customer support assistant.
Analyze the customer's problem and return:
- category
- priority
- summary
- recommendedAction
Keep the response concise.
""")
.to("openai:chat-completion"
+ "?model=gpt-4.1-mini"
+ "&conversationMemory=true"
+ "&outputClass=com.example.demo.SupportResponse")
.log("LLM response: ${body}");
}
}
The from() statement exposes the /support HTTP endpoint and accepts only POST requests. The setHeader() step adds the system message that instructs the LLM how to analyze the customer request and structure its response. The to() statement invokes Camel’s openai:chat-completion endpoint and specifies the model, enables conversationMemory, and uses outputClass to define the expected structured response. Finally, the log() statement logs the response returned by the LLM, making it easy to observe the result while running the application.
2.5 Creating the Spring Boot Application
The CamelOpenAiApplication class is the entry point of the Spring Boot application. The @SpringBootApplication annotation enables Spring Boot auto-configuration and component scanning, allowing Spring to discover the Camel route defined in SupportRoute. The main() method starts the application and initializes the embedded web server and Camel context.
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class CamelOpenAiApplication {
public static void main(String[] args) {
SpringApplication.run(
CamelOpenAiApplication.class,
args
);
}
}
The application does not require a separate controller because the HTTP endpoint is defined directly in the Camel route. When the application starts, Spring Boot discovers SupportRoute through its @Component annotation, registers the route with Camel, and makes the /support endpoint available for incoming requests.
2.6 Running the Application and Viewing the Output
Start the Spring Boot application using Maven. Make sure the OPENAI_API_KEY environment variable is configured before starting the application.
export OPENAI_API_KEY="your-api-key" mvn spring-boot:run
Once the application starts successfully, send a POST request to the Camel /support endpoint with a customer support question.
curl -X POST http://localhost:8080/support \ -H "Content-Type: text/plain" \ -d "I cannot log in to my travel portal. My password is correct, but I get an authentication error after login."
The request is received by the Camel route, passed to the OpenAI component, and processed according to the system instructions defined in SupportRoute. Because the route uses outputClass, the LLM is instructed to return the response using the structure defined by SupportResponse. An illustrative response is shown below.
{
"category": "Authentication",
"priority": "Medium",
"summary": "The user can provide valid credentials but receives an authentication error after login.",
"recommendedAction": "Check the application's authentication flow, SSO configuration, and authentication logs."
}
The exact response can vary because it is generated by the LLM, but the important point is that the response follows the SupportResponse structure. The Camel route also logs the returned response, making it possible to verify the LLM interaction from the application logs.
3. Conclusion
Apache Camel provides a natural integration layer for applications that want to incorporate LLM capabilities without restructuring their entire architecture around an AI framework. The OpenAI component allows an LLM invocation to become another step in a Camel route. The example demonstrated three particularly useful capabilities: direct LLM invocation, per-exchange conversation memory, and structured output. With conversationMemory=true, multiple OpenAI calls in the same Camel exchange can share conversational context, while outputClass or jsonSchema allows applications to move away from unpredictable free-form text toward machine-readable responses. This approach is particularly useful for enterprise integration scenarios where an event or API request can enter an Apache Camel route, go through transformation and enrichment, invoke an LLM through the OpenAI component, and then pass through validation and business processing before reaching a downstream enterprise system. The main architectural advantage is simplicity: Camel remains responsible for integration and orchestration, while the OpenAI component provides the LLM capability. For straightforward LLM-powered integration flows, this can be lighter than introducing a full AI or agent framework. For complex agentic workflows involving tools, autonomous planning, or function calling, a framework such as Spring AI or LangChain4j may be a better fit because the Camel OpenAI component is primarily focused on direct LLM integration. In short, the Camel OpenAI component makes it possible to treat an LLM as a first-class integration endpoint: receive a message, build context, call the model, preserve conversation state, obtain structured output, validate it, and continue the enterprise workflow using familiar Apache Camel patterns.




